What you call “LINQ”, is actually called LINQ query syntax. And what you cal “anonymous lambda” is actually called LINQ method syntax. The important thing is that the expression:
from n in numbers
where isOddAndNotDivisibleBy(n)
select n
is actually transformed to this before compiling:
numbers.Where(n => isOddAndNotDivisibleBy(n))
This means that if you use the former or the latter directly, you will get the same IL code.
So the difference has to be somewhere else. And it's the ToArray()
call. Where()
is lazy. That means it does almost nothing, unless you actually iterate over the resulting sequence in one way or another. That means that just calling Where()
is going to be nigh instantaneous. But if you call ToArray()
, the collection is actually iterated and the results are computed immediately. That's why you see such a huge difference.
EDIT:
To the modified question: why use one syntax over the other? Primarily choose the one that is more readable.
There are some methods and overloads that you can't express using query syntax (First()
, Aggregate()
, Concat()
Zip()
, overloads that introduce index, …). But you can use one syntax for one part of the query and another for the rest.
There is also one powerful feature of query syntax that can't be easily expressed using method syntax: transparent identifiers, e.g. when using the let
clause.
But there are no performance differences between the two syntaxes, at least when used correctly.