Consider this:
string test = "";
somestring.ToList().Take(50).Select(
delegate(char x)
{
test += x;
return x;
}
);
Why is test empty after that? I don’t care about the return of that actually (I know its IEnumerable<string>).
If this is all seems a mess then how can I convert the IEnumerable<char>
returned by Select() to string ?
Because you didn’t execute the query. Linq is lazy. It will be executed when you do either
foreachorToList/ToArray/ToDictionary.and i advice to do it like that
or even using
More on that.
Selectis intended to apply some kind of transformation to every element in sequence. This operation is also known asmap. If you want to produce single element from sequence it isAggregate, akareduce. It is not lazy, it forces execution of query.