Is it Linq or Lambda?
Asked Answered
C

2

128

I know that this is Linq:

var _Results = from item in _List
                where item.Value == 1
                select item;

And I know this is Lambda:

var _Results = _List.Where(x => x.Value == 1);

Editor's note: the above is not merely Lambda, it is Linq using the "Method Syntax" whose predicate is a Lambda. To be clear, both of the above samples are Linq (my original post was incorrect, but I left the error to illustrate the confusion prompting the question).

But is Linq a subset of Lambda or what?

Why are there two seemingly identical techs?

Is there a technical reason to choose one over the other?

Choosy answered 12/9, 2011 at 17:5 Comment(2)
Correction: lambdaexpression.netChoosy
This question is also the only correct answer : )Sihunn
M
174

This is LINQ (using query syntax):

var _Results = from item in _List
                where item.Value == 1
                select item;

This is also LINQ (using method syntax):

var _Results = _List.Where(x => x.Value == 1);

It's interesting to note that both of these flavors will end up producing the exact same code. The compiler offers you a service by allowing you to express your wishes in the manner that you prefer.

And this is a lambda:

x => x.Value == 1

When you choose to use method syntax, LINQ is almost always seen around lambda expressions. But LINQ and lambdas are two totally different things, both of which can be used by themselves.

Update: As svick rightly points out, LINQ with query syntax is also implemented using lambda expressions (as mentioned earlier, the compiler allows you to write in query syntax but effectively transforms it to method syntax behind your back). This is just piling on the fact that both flavors are totally equivalent and will behave the same way (e.g. lambda expressions may cause closures to be created).

Mellman answered 12/9, 2011 at 17:7 Comment(1)
I think it's worth mentioning that query syntax uses lambdas behind the scenes too. This can be important because of closures.Lola
T
41

Both are Linq. The second one is using Lambdas.

Lambdas are the inline method type things that you are passing as a parameter to the Where function in the second example.

The difference between those two syntaxes is purely syntactic. The second linq style using method calls is how it works under the hood. The first is meant to be more user friendly/easier and the compiler converts it to method calls behind the scenes. They should work the same for any given query though of course the compiler may choose a sligthly different interpretation of a complicated linq query than you would when converting to method style.

This msdn article may be of interest too: LINQ Query Syntax versus Method Syntax. Of particular relevance is: "In general, we recommend query syntax because it is usually simpler and more readable; however there is no semantic difference between method syntax and query syntax."

Transceiver answered 12/9, 2011 at 17:8 Comment(5)
Personally, I find method syntax more readable - perhaps because most of my code is of the "LINQ to Objects" variety. But if you have a lot of SQL experience, perhaps query syntax will be easier to understand at first.Pervasive
@Tom Bushell, even JOIN syntax? Seriously?Choosy
@Tom Bushell: me too. I was paraphrasing something on that MSDN page which presumably explains why they bothered to develop that syntax rather than having just the method style. I am usually just doing relatively basic stuff rather than joins or anything else more complicated (ie mostly filtering or one to one mapping operations).Transceiver
@Jerry - like Chris, my LINQ work has been fairly simple so far. I've read that Query Syntax is usually preferable when doing SelectMany, Join, or GroupJoin - I just haven't needed to do anything like that - yet!Pervasive
Internally "query syntax" was referred to on LINQ to SQL and LINQ to Entities teams as "comprehension syntax".Allomorph

© 2022 - 2024 — McMap. All rights reserved.