Using a LINQ Expression to Retrieve IEnumerable Properties from IEnumerable from POCO

I am trying to iterate an IEnumerable object using LINQ and pull out the specific property of each object in an IEnumerable type of this property.

I am currently doing this using the SelectMany LINQ expression, but instead of returning the IEnumerable<string> that I want (since this property is a string), it returns an IEnumerable<char>

 var propertyValues = objectCollection.SelectMany(o => o.Value); 

What am I missing?

+7
source share
3 answers

If it returns an IEnumerable<char> , then this is what it is. Suppose you want to use Select () rather than SelectMany (), I would suggest that it smooths a bunch of lines as an IEnumerable<char> . If you can post more code that shows what is in the objectCollection , we could help more.

EDIT

a bit more code to illustrate my point

 List<string> stringList = new List<string>(); stringList.Add("string1"); stringList.Add("string2"); IEnumerable<char> chars = stringList.SelectMany(x => x); 
+8
source

you want Select instead of SelectMany . SelectMany will smooth the sequence of sequences. You basically project onto a sequence of strings - and the string is a sequence of characters, so SelectMany smooths the sequence of strings to one sequence of characters.

Try:

 var propertyValues = objectCollection.Select(o => o.Value); 
+5
source

Use Select(o => o.Value) . SelectMany smoothes collections, so it selects all of your Value string properties, and then aligns each to a list of characters.

+2
source

All Articles