Is Lambda (=>) the Linq equivalent of a When/Where clause?

  • Thread starter Thread starter Rich P
  • Start date Start date
R

Rich P

I have been practicing with the Linq queries at

http://msdn.microsoft.com/en-us/vcsharp/aa336760.aspx

and I have noticed that some queries use Lambdas (=>) and some don't. I
am trying to figure out the logic of when to use/apply Lambdas in a Linq
Query. It looks like a Lambda is used like a Where/When clause. Is
this the case? What is the logic of a Lambda?

Thanks,

Rich
 
Do you mean compared with the "from.. where... select..." syntax ? This
later form is just a higher level notation which is translated into lambdas
by the compiler. So this is likely more a matter of preference...

If you meant more generally basically :
- a lambda allows to treat inline code as data (for example to pass "code"
as an argument to another method).

For example to call the Where method you'll need to pass as an argument the
function that will allows when it runs, to find out if the item is to be
selected in the final result...
 
I have been practicing with the Linq queries at

http://msdn.microsoft.com/en-us/vcsharp/aa336760.aspx

and I have noticed that some queries use Lambdas (=>) and some don't. I
am trying to figure out the logic of when to use/apply Lambdas in a Linq
Query. It looks like a Lambda is used like a Where/When clause. Is
this the case?

No, not really.
What is the logic of a Lambda?

In terms of LINQ, a lambda expression is just a compact way to express an
anonymous method (they can also represent expression trees, but that's not
the use you're asking about here). Likewise, the LINQ expression syntax
is just a compact way to express method calls (e.g. "where" translates to
an explicit call to Enumerable.Where(), with the compiler performing
various inferences to do the right things with the LINQ expression).

You can find a more detailed discussion here:
http://msdn.microsoft.com/en-us/library/bb397687.aspx

Pete
 
In terms of LINQ, a lambda expression is just a compact way to express an
anonymous method (they can also represent expression trees, but that's not
the use you're asking about here). Likewise, the LINQ expression syntax
is just a compact way to express method calls (e.g. "where" translates to
an explicit call to Enumerable.Where(), with the compiler performing
various inferences to do the right things with the LINQ expression).

For LINQ to Objects, the lambdas are compiled into delegates.

For LINQ to SQL, they are compiled into expression trees and used to build
the provider-specific SQL query, to be evaluated server-side. That's pretty
much the whole point.
 
Back
Top