LINQ vs Lambda Expressions

Director, Sitecore Practice
  • Twitter
  • LinkedIn

LINQ vs Lambda Expressions

I was wondering what the difference is, internally, between LINQ (query syntax) and Lambda Expressions (method syntax) and are there any performance difference? So I decided to create the same queries in both query syntax and method syntax. Then, I examine the compiled code.

LINQ

var subset = from type1 in ListOfType1
             where ListOfType2.Any(type2 => type2.ID.Equals(type1.ID))
             select type1

Lambda Expressions

var subset = ListOfType1
              .Where(type1 => ListOfType2.Any(type2 => type2.ID.Equals(type1.ID)));

Result

They both compile down to the same thing

IEnumerable subset = 
   ListOfType1.Where(delegate(Type1 type1) {
      return ListOfType2.Any(type2 => type2.ID.Equals(type1.ID));
   });

Related Blogs

Latest Blogs