Reputation: 3659
This must be simple but I failed to understand why this is not allowed:
var testList = new List<int> { 2, 3, 400, 304, 50, 41 };
testList.Select(x => Console.WriteLine(x));
But this is fine:
testList.Select(x => x * 2);
Where do I misunderstand LINQ to cause this confusion?
Upvotes: 1
Views: 158
Reputation: 126042
.Select
takes a Func<TSource, TResult>
(a lambda that takes a TSource
and returns TResult
).
Since Console.WriteLine
is void
, your lambda does not return anything and therefore doesn't meet the requirements.
Upvotes: 8
Reputation: 168948
The problem is that the function you pass to Select()
must return a value, because the purpose of Select()
is to project the elements of a sequence into some other value. But Console.WriteLine(x)
returns void (no value).
Upvotes: 9