When operating with Enumerables, which technique is better? Are there any performance differences between Linq's query syntax and extension methods?
For example:
Lista.OrderBy(Function(n) n.apellidos).ToList()
(From n In Lista Order By n.apellidos).ToList()
None in terms of performance, they are exactly the same. We can check this by making a simple program and checking the IL * output it produces.
First, using the extension methods:
This, after being compiled, gives us this IL * :
Now using LINQ:
What does it give us after being compiled:
As you can see, it is exactly the same. .Net provides both ways of doing the query, but performance-wise it's the same.
There are certain differences in terms of using one or another syntax, but they are based more on readability and depending on the case, sometimes one or the other is better. But the performance is exactly the same.
*IL refers to Common Intermediate Language
Although the issue of advantages/disadvantages is subjective (for example, 99% of the time I use extension methods), here are two Stack Overflow questions that discuss the issue: LINQ - Fluent and Query Expression - Is there any benefit(s) of one over another? and Extension methods syntax vs query syntax