14

I need a discussion regarding the Performance of LINQ and Lambda Expression.

Which one is better?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Thomas Anderson
  • 1,977
  • 7
  • 17
  • 22
  • 4
    You might want to look here http://stackoverflow.com/questions/1182922/what-is-the-efficiency-and-performance-of-linq-and-lambda-expression-in-net – Filip Ekberg Oct 12 '10 at 12:26

2 Answers2

37

I guess you mean query expression when talking about LINQ here.

They are equivalent. The compiler changes the query expression into the equivalent Lambda expression before compiling it, so the generated IL is exactly the same.

Example

var result = select s from intarray
             where s < 5
             select s + 1;

is exactly the same as

var result = intarray.Where( s => s < 5).Select( s => s+1);

Note that if you write the query expression like this:

var result = select s from intarray
             where s < 5
             select s;

It's converted to:

var result = intarray.Where( s => s < 5);

The final call to Select is omitted because it's redundant.

cori
  • 8,666
  • 7
  • 45
  • 81
Øyvind Bråthen
  • 59,338
  • 27
  • 124
  • 151
  • 4
    I would have thought the same, but, according to this site ( http://blog.thijssen.ch/2009/02/linq-vs-lambda-vs-loop-performance-test.html ), the Lambda expressions perform faster. I have copied the Test function into an app, to test it, and it verified the findings on that site. – Max v. Steiger Oct 17 '12 at 19:13
  • 2
    Would this mean to learn Lambda expressions only? – Zo Has Oct 25 '12 at 09:51
  • 2
    'query expression' is more readable especially for new ppl – om471987 Jan 07 '13 at 06:50
  • 9
    @Omkar - I actually find the chained lambdas much more readable ;) – Øyvind Bråthen Jan 17 '13 at 10:08
  • 3
    The test at the blog is totally wrong! first of all both of them LINQ one is query expression the other one is Lambda expression form. second just move the LAMBDA part upper than LINQ and re-run the test to see the result! then remove the int from the query (forces casting cost) then use the intlist in both (not array in one an List in the other one) the result will be the same because compiler translates them to the same thing as the above answer explained nicely. I use both of them: Lambda for simple expressions and query for more complex ones. – Fred Jand Jul 30 '14 at 16:15
  • 2
    The major difference comes when joins are involved - pretty trivial in query expressions, kind of monstrous when using the manual method calls. – Luaan Sep 07 '15 at 10:21
6

a quick comparison in reflector would probably do the trick. However, from a 'preference' standpoint, I find lambda statements easier to follow and write and use them across the board whether it be with objects, xml or whatever.

If performance is negligible, i'd go with the one that works best for you.

i actually started off a little topic looking at linq methods which may be of interest:

What's your favourite linq method or 'trick'

cheers..

Community
  • 1
  • 1
jim tollan
  • 22,305
  • 4
  • 49
  • 63