I've encountered an issue with the SelectMany expression that I just can't wrap my head around.
Consider this: I have a collection of objects of this class
class Tag
{
string DisplayText { get; set; }
string Key { get; set; }
int Value { get; set; }
}
Now I'm trying to get all my display texts (actually part of a much more complex expression):
var texts = AvailableTags.SelectMany(t => t.DisplayText);
Now why does this return me an IEnumerable<char> instead of an IEnumerable<string>??? Am I missing something?
If AvailableTags is a list (an IEnumerable) then you should simply use
var texts = AvailableTags.Select(t => t.DisplayText);
The "strange" result you have using SelectMany is due (exactly as said from @derloopkat) to the fact that a string is a collection of char.
So you can imagine your code like this:
class Tag
{
List<char> DisplayText { get; set; }
string Key { get; set; }
int Value { get; set; }
}
When you use SelectMany you're getting all the chars contained in every DisplayText and then the result is flattened.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With