Newbie
Newbie

Reputation: 1111

How the C# compiler treats query expressions ?(Dotnet 3.5, C#3.0)

Going thru one of my favourite authors question What’s the hardest or most misunderstood aspect of LINQ? I am basically looking for the answer of the question:

How the C# compiler treats query expressions?

Upvotes: 1

Views: 311

Answers (2)

Thurein
Thurein

Reputation: 2566

The answer may vary between underlying LINQ providers.

Generally speaking, LINQ query expressions or method chains are transformed into Expression Tree before it goes to provider-specific implementation.

As for LINQ to Objects (IEnumerable), the expression tree is complied into a set of System.Func or System.Action delegates.

As for LINQ to SQL (IQueryable), the expression tree is transformed into T-SQL Statements.

Upvotes: 0

Anthony Pegram
Anthony Pegram

Reputation: 126952

The compiler will evaluate and transform your query expressions into the equivalent lambda syntax before compiling the code further. So code that starts as

 var query = from foo in foos 
             where foo.Bar == someString
             select new 
             {
                 Baz = foo.Baz,
                 Id = foo.Id
             };

Will be transformed into the lambda version

 var query = foos.Where(f => f.Bar == someString).Select(f => new { Baz = f.Baz, Id = f.Id });

The same will happen to your complicated joins, groupings, etc.

Upvotes: 0

Related Questions