Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
string a=list.Where(x => x.GalleyLocation == "G1").Select(x => x.LengthOfArm).ToString();


error:System.Linq.Enumerable+WhereSelectListIterator`2



please need help to know reason for error and also provide the solution for same
Posted
Updated 2-May-21 22:12pm
v3

Your query does not return a single item: it returns an IEnumerable of items which match your condition. So when you take the ToString of the collection, you get the name of the collection - and that's the "message" you get.

Generally speaking, trying to convert any collection into a single string is a poor idea, but you might get what you want this way:
C#
string a=string.Join("", list.Where(x => x.GalleyLocation == "G1").Select(x => x.LengthOfArm.ToString()));
 
Share this answer
 
As already said, Select will return a collection, even if that collection only has a single item in it. If you do the ToString inside the Select then it will return an IEnumerable of string, and you can use FirstOrDefault on that collection to get the first one in the collection.

C#
string a=list.Where(x => x.GalleyLocation == "G1").Select(x => x.LengthOfArm.ToString()).FirstOrDefault();


If the collection is empty you'll get the default value for that type, so it'll return null for strings. If the collection has only one item in it then you get that item, if it has multiple items you get the first one and the rest are ignored.

If you want to ensure there was only one item in the list then you can use SingleOrDefault and that will raise an exception if there was more than one item in the list. This is usually a bad idea though.
 
Share this answer
 
Comments
Member 12198049 10-Dec-15 6:47am    
thanks its working
Thanks a lot!! That was saving me from dispear. Very good explanation, easy when understood.
 
Share this answer
 
Comments
CHill60 4-May-21 8:29am    
If you want to comment on a solution, then please use the "Have a Question or Comment?" link next to it. That poster will be notified, and the original post will not be dragged back into the Active Questions list.
This is not a "Solution"

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900