Is it Link or Lambda?

I know 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: Not only Lambda is above, it is Linq using the "Syntax Method", whose predicate is lambda. To be clear, both of the above Linq samples (my original post was wrong, but I left an error to illustrate the confusion by raising a question).

But is Linq a subset of Lambda or what?

Why are there two seemingly identical techies?

Is there a technical reason for choosing one over the other?

+77
c # lambda linq
Sep 12 '11 at 17:05
source share
2 answers

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 is interesting to note that both of these flavors ultimately produce the same code. The compiler offers you a service, allowing you to express your wishes as you prefer.

And this one is lambda:

 x => x.Value == 1 

When you decide to use the method syntax, LINQ almost always occurs around lambda expressions. But LINQ and lambdas are two completely different things that can be used on their own.

Update:. As you can see from the line, LINQ with query syntax is also implemented using lambda expressions (as mentioned earlier, the compiler allows you to write in the query syntax, but effectively converts it to the method syntax behind your back). It is simply an imposition that both tastes are completely equivalent and will behave the same (for example, lambda expressions can cause closures ).

+109
Sep 12 '11 at 17:07
source share

Both are Linq. The second uses Lambdas .

Lambdas is the type of built-in method that you pass as a parameter to the Where function in the second example.

The difference between the two syntaxes is purely syntactic. The second linq style using method calls is how it works under the hood. The first is intended to be more user-friendly / simpler, and the compiler will convert it to method calls behind the scenes. They should work the same for any given request, although, of course, the compiler can choose a weak interpretation of a complex linq request than when converting to a method style.

This msdn article may also be of interest: LINQ query syntax and method syntax . Of particular importance is the following: "In general, we recommend query syntax because it is usually simpler and more readable, but there is no semantic difference between the method syntax and the query syntax."

+27
Sep 12 2018-11-17T00:
source share



All Articles