Royi Namir
Royi Namir

Reputation: 148714

C# compiler sees Fluent syntax or Query Expression?

I'm always confusing about this stuff.

I have this query :

string[] names = { "Tom", "Dick", "Harry", "Mary", "Jay" };

IEnumerable<string> query = names.Where(n => n.Contains("a"))
.OrderBy(n => n.Length)
.Select(n => n.ToUpper());

I've read in a book that:

The compiler processes a query expression by translating it into fluent syntax

But in Reflector I see the opposite: Reflector Code

This is not fluent syntax.

So what does the compiler sees?

Upvotes: 4

Views: 1105

Answers (5)

Eric Lippert
Eric Lippert

Reputation: 660463

A "compiler" is by definition a device which translates a text written in one language into another language.

The C# compiler logically translates C# programs that contain query expressions into C#-without-query-expressions, and then translates those programs into IL. (Note that it need not actually do that middle stage of translation; it must behave as though it does so, but if the compiler writers are clever enough to skip that intermediate step and still get the right output then we certainly can do so.)

Reflector is also a compiler. It translates IL into C#. How it does so is it's business.

You can't make any conclusions about what the C# compiler does based on the output of Reflector; they are completely different programs written by different people to solve different problems.

Upvotes: 19

user7116
user7116

Reputation: 64128

The stock csc compiler converts any and all syntax into IL, fluent or otherwise. What you're looking at is the reconstruction chosen by Reflector.

Certain syntax is simply sugar for more complicated constructs under the hood, and the query expressions are one of these examples. Other examples includes foreach loops and lambda expressions.

Now if this is LINQ to SQL or Entities, the query syntax and fluent syntax is realized as Expressions and translated by the provider under the hood. This is not the same as in L2O.

Upvotes: 3

Tigran
Tigran

Reputation: 62265

I think saying "fluent syntax" book means the Deferred Execution of LINQ queires. When only on actual request of the data the query is generated based on all queries done before (if there is any). All queries done before are analyzed and one single (if it's possible) optimized LINQ query generated to get a data.

From the final result point of view, like others mantioned, it always ends up into the set of IL instructions.

Upvotes: 0

Justin Pihony
Justin Pihony

Reputation: 67135

I believe that you are reading this backwards (but I could be mistaken)

What it is saying is that

from n in names
where n.Contains("a")
orderby n.Length
select n.ToUpper()

Translates into

names
.Where(n => n.Contains("a"))
.OrderBy(n => n.Length)
.Select(n => n.ToUpper());

Upvotes: 1

SLaks
SLaks

Reputation: 888087

Query comprehension syntax is compiled directly into method calls; they produce indistinguishable IL.

However, many decompilers will always translate LINQ calls into query comprehension syntax.
YOu can probably change that behavior in Options.

Upvotes: 6

Related Questions