Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comparison : LINQ vs LAMBDA Expression [closed]

Tags:

c#

lambda

linq

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

Which one is better?

like image 487
Thomas Anderson Avatar asked Oct 12 '10 12:10

Thomas Anderson


2 Answers

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.

like image 125
Øyvind Bråthen Avatar answered Sep 22 '22 20:09

Øyvind Bråthen


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..

like image 34
jim tollan Avatar answered Sep 18 '22 20:09

jim tollan