473,387 Members | 1,512 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

How to typecast the first object from an IEnumerable?

I'm working on someone else code I don't quite understand it.

It has a class with a method like :
private IEnumerable<ProductGetProducts(int count)
{
blah, blah
}

And I need to get an instance of the first object of that list:

Product prod = GetProducts(1);

I get a compile time error:
**************
"Cannot implicitly convert type
'System.Collections.Generic.IEnumerable<Products>' to 'Products'. An
explicit conversion exists (are you missing a
cast?) C:\Websites\BlogSK\Admin\Login.aspx.cs 24 18 C:\Websites\BlogSK\"
**************
I tried to:
Product prod = (Product)GetProducts(1);

And got it to compile, but then I get a runtime error:
"Unable to cast object of type
'System.Collections.Generic.List`1[Products]' to type 'Products'."

Any kind of help is appreciated.

Sep 19 '06 #1
1 1631
If the result was IList<Product>, you could use

Product prod = GetProducts(1)[0];

Since the result is IEnumerable / IEnumerable<Product(and doesn't
provide an indexer), you have to enumerate it...

Product prod = null;
foreach(Product p in GetProducts(1)) {
prod = p;
break; // short-circuit in case multiple returned items
}

If you are going to be doing a lot more with the enumerable set (which
I doubt, as it probably only 1 item), the following can be a handy way
to make more functionality available:

List<Productproducts = new List<Product>(GetProducts(count));
Product firstProduct = products[0];
// lost of other things involving "products"...

Any use?

Marc

Sep 20 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Nimmi Srivastav | last post by:
There's a rather nondescript book called "Using Borland C++" by Lee and Mark Atkinson (Que Corporation) which presents a rather good discussion of typecast operator overloading. I am presenting...
1
by: masood.iqbal | last post by:
I have a few questions regarding overloaded typecast operators and copy constructors that I would like an answer for. Thanks in advance. Masood (1) In some examples that I have seen...
8
by: Trishia Rose | last post by:
this is something ive always wondered, does it take cpu time at run time to typecast or just at compile time? for example consider the two little bits of code: int a = 5; int b = a; and: ...
6
by: nick | last post by:
I have a function must return an one-row IEnumerable object. Any lesser cost way than the following code? static IEnumerable MyUDF() { DataTable dt; dt.Columns.Add("Test"); dt.Rows.Add(1);...
3
by: nick | last post by:
The function must return a IEnumerable object with one row (any data). I have the following code. Any less cost way? IEnumerable^ udf() { DataTable dt; dt.Columns->Add("Test");...
8
by: akolsen | last post by:
Hello there. This should be simple, but im having trouble anyway of getting it to work. I have a boxed object that i want to cast to its native type, but I would like to use reflection to do...
11
by: Manikandan | last post by:
Hi, I am using the following snippet to compare an object with integer in my script. if ( $forecast < 4 ) { I got the "segmentation fault" error message when i executed this script in CLI....
0
by: David C | last post by:
I am getting this error in the Databound event of a GridView and I am having a difficult time debugging. I do not get the error on every record that I edit in the GridView. The error is occurring...
6
by: fbooyens | last post by:
I'm new to C# and come from an embedded C background so I hope I'm not embarassing myself not too much. I need to write a simulator for a variable frequency drive which sends device parameters to a...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.