tags:

views:

88

answers:

2

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

Which one is better??

+4  A: 

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);

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

Øyvind Bråthen
A: 

Hi there,

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:

http://stackoverflow.com/questions/3466536/whats-your-favourite-linq-method-or-trick

cheers..

jim