Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Expressions vs Lambdas

I know what a Lambda Expression is.

But I am not sure if that is the same thing as an Expression. There seems to be more to know here than I know.

I am looking at wrapping IQueryable and that uses Expressions a lot. So, for example, is there more to the 'Expression' parameter here than can be thought of in a Lambda?

public InterceptedQuery(InterceptingProvider provider, Expression expression) 
{ 
    this._provider = provider; 
    this._expression = expression; 
}  
like image 587
Vaccano Avatar asked Aug 22 '11 15:08

Vaccano


People also ask

Is lambda an expression?

A lambda expression is a short block of code which takes in parameters and returns a value. Lambda expressions are similar to methods, but they do not need a name and they can be implemented right in the body of a method.

What is the difference between statement lambda and expression lambda?

The difference between a statement and an expression lambda is that the statement lambda has a statement block on the right side of the lambda operator, whereas the expression lambda has only an expression (no return statement or curly braces, for example).

What is the difference between lambda expression and LINQ?

Language Integrated Query (LINQ) is feature of Visual Studio that gives you the capabilities yo query on the language syntax of C#, so you will get SQL kind of queries. And Lambda expression is an anonymous function and is more of a like delegate type.


2 Answers

A lambda expression is a compiler feature that is compiled, depending on context, into one of two things:

  • A (hidden) function and a delegate to it
  • An Expression

Once the application has been compiled, the concept of a lambda expression doesn't exist, as it's been turned into one of the above two options.

I'm not sure what you mean by

is there more to the 'Expression' parameter here than can be thought of in a Lambda

An expression encapsulates and expressed application logic in an inspectable form (in other words, it lets you see what the developer wrote in terms of properties and functions invoked, constants included, comparisons, etc.). This is how query providers (for the most part, object-relational mappers like the Entity Framework) take code and turn it into SQL.

like image 197
Adam Robinson Avatar answered Sep 29 '22 11:09

Adam Robinson


Lambdas are generally compiled code, where Expression represents "abstract syntax trees" (AST), ie. a data structure that represents code, and can be compiled to code. IQueryable generally operates on Expression because it's supposed to compile the AST to code that runs in different environments, like an SQL server, instead of just the host machine. There are IQueryableProviders that compile to SQL (Linq2Sql), to JavaScript, to OpenGL shaders (Bling), and more.

The C# compiler can sometimes turn a lambda into an expression, if the method parameter expects an expression of the right type:

void Foo(Expression<Func<int>>) { ... }
...
Foo(() => 3);
like image 41
naasking Avatar answered Sep 29 '22 09:09

naasking