Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
XML
IList<ResponseDTO> MyList = new List<ResponseDTO>();
var myValue = MyList.Select(T => T.KeyId);
var myValue = from myListValue in MyList
              select myListValue.KeyId;


In the above code the first one is a Lambda Expression & second is LINQ and both give me the same result.

I want to know the internal difference between these two and their performance.
Posted
Updated 21-May-12 19:26pm
v2

Both syntax compile to the same thing to IL as can be seen from the below code. ResponseDTO is replaced with string and ToUpper method is used to compile the code.
C#
IList<string> MyList = new List<string>();
    var myValue = MyList.Select(T => T.ToUpper());

//IL_0001:  newobj      System.Collections.Generic.List<System.String>..ctor
//IL_0006:  stloc.0
//IL_0007:  ldloc.0
//IL_0008:  ldsfld      UserQuery.CS$<>9__CachedAnonymousMethodDelegate1
//IL_000D:  brtrue.s    IL_0022
//IL_000F:  ldnull
//IL_0010:  ldftn       UserQuery.<Main>b__0
//IL_0016:  newobj      System.Func<System.String,System.String>..ctor
//IL_001B:  stsfld      UserQuery.CS$<>9__CachedAnonymousMethodDelegate1
//IL_0020:  br.s        IL_0022
//IL_0022:  ldsfld      UserQuery.CS$<>9__CachedAnonymousMethodDelegate1
//IL_0027:  call        System.Linq.Enumerable.Select
//IL_002C:  stloc.1
//
//<Main>b__0:
//IL_0000:  ldarg.0
//IL_0001:  callvirt    System.String.ToUpper
//IL_0006:  stloc.0
//IL_0007:  br.s        IL_0009
//IL_0009:  ldloc.0
//IL_000A:  ret


C#
IList<string> MyList = new List<string>();
    var myValue = from myListValue in MyList
                select myListValue.ToUpper();
//IL_0001:  newobj      System.Collections.Generic.List<System.String>..ctor
//IL_0006:  stloc.0
//IL_0007:  ldloc.0
//IL_0008:  ldsfld      UserQuery.CS$<>9__CachedAnonymousMethodDelegate1
//IL_000D:  brtrue.s    IL_0022
//IL_000F:  ldnull
//IL_0010:  ldftn       UserQuery.<Main>b__0
//IL_0016:  newobj      System.Func<System.String,System.String>..ctor
//IL_001B:  stsfld      UserQuery.CS$<>9__CachedAnonymousMethodDelegate1
//IL_0020:  br.s        IL_0022
//IL_0022:  ldsfld      UserQuery.CS$<>9__CachedAnonymousMethodDelegate1
//IL_0027:  call        System.Linq.Enumerable.Select
//IL_002C:  stloc.1
//
//<Main>b__0:
//IL_0000:  ldarg.0
//IL_0001:  callvirt    System.String.ToUpper
//IL_0006:  stloc.0
//IL_0007:  br.s        IL_0009
//IL_0009:  ldloc.0
//IL_000A:  ret

In the second syntax called as query syntax some of the extension methods of IEnumerable are not available.
Each syntax is useful in certain conditions and sometimes both syntax are used in a mix.

A good discussion on this topic is available here
http://stackoverflow.com/questions/214500/which-linq-syntax-do-you-prefer-fluent-or-query-expression[^]
 
Share this answer
 
Comments
Mehdi Gholam 22-May-12 2:01am    
My 5!
VJ Reddy 22-May-12 2:36am    
Thank you, Mehdi :)
Prasad_Kulkarni 22-May-12 2:11am    
Good one VJ! +5!
VJ Reddy 22-May-12 2:36am    
Thank you, Prasad :)
Renju Vinod 16-Jan-13 5:30am    
+5
Linq is always associated with Lambda Expressions.
In .NET 2.0 we have the concept of Annonymous Methods that allows you to write function body inline without the need of writing a delegate function.
Lambda Expression of .NET 3.5 is to consize the concept of Annonymous function writing.
This CP article explains you about: Basics of LINQ & Lamda Expressions[^]

In addition,
You can use Lambda Expressions when you want to create a delegate or an expression tree, basically.
Here are some examples outside LINQ:

  • The old-style List<T>.ConvertAll/FindAll etc methods
  • Starting new threads / tasks
  • Attaching event handlers
  • Providing an action in unit tests (e.g. "this action should throw an exception")
  • Providing a value on request for Lazy<T>


Things which are only be achieved by Lambda expression & not by LINQ queries.
There are some LINQ extension methods which do not have counterparts in LINQ query expressions, and will require the use of Lambda Expressions.

A good example is Enumerable.ToLookup[^] - if you want to create an ILookup, you need to use lambdas to generate this.
 
Share this answer
 
Comments
VJ Reddy 22-May-12 2:38am    
Good answer. 5!
Prasad_Kulkarni 22-May-12 2:46am    
Thank you VJ!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900