Why is the "Select" of a Method Syntax is in another parenthesis?

var sample = db.Database.OrderByDescending(x => x.RecordId).Select(y => y.RecordId).FirstOrDefault();

I don't know if my title is correct / right. Just want to ask why this query the select is in another ( )?. As for the example .Select(y => y.RecordId) unlike the query I use to be

var sample = (from s in db.Databse where s.RecordId == id select s) I know this is the same right?. Then what is the why it is in another parenthesis?. Anyone has an idea or can anyone explain it why?. Thanks a lot.

Jon Skeet
people
quotationmark

In your first example, you're using "regular" C# syntax to call a bunch of extension methods:

var sample = db.Database
               .OrderByDescending(x => x.RecordId)
               .Select(y => y.RecordId)
               .FirstOrDefault();

(They happen to be extension methods here, but of course they don't have to be...)

You use lambda expressions to express how you want the ordering and projection to be performed, and the compiler converts those into expression trees (assuming this is EF or similar; it would be delegates for LINQ to Objects).

The second example is a query expression, although it doesn't actually match your first example. A query expression corresponding to your original query would be:

var sample = (from x in db.Database
              orderby x.RecordId descending
              select x.RecordId)
             .FirstOrDefault();

Query expressions are very much syntactic sugar. The compiler effectively converts them into the first form, then compiles that. The range variable declared in the from clause (x in this case) is used as the parameter name for the lambda expression, so select x.RecordId becomes .Select(x => x.RecordId).

Things become a bit more complicated with joins and multiple from clauses, as then the compiler introduces transparent identifiers to allow you to work with all the range variables that are in scope, even though you've really only got a single parameter. For example, if you had:

var query = from person in people
            from job in person.Jobs
            order by person.Name
            select new { Person = person, Job = job };

that would be translated into the equivalent of

var query = people.SelectMany(person => person.Jobs, (person, job) => new { person, job } )
                  .OrderBy(t => t.person.Name)
                  .Select(t => new { Person = t.person, Job = t.job });

Note how the compiler introduces an anonymous type to combine the person and job range variables into a single object, which is used later on.

Basically, query expression syntax makes LINQ easier to work with - but it's just a translation into other C# code, and is neatly wrapped up in a single section of the C# specification. (Section 7.16.2 of the C# 5 spec.)

See my Edulinq blog post on query expressions for more detail on the precise translation from query expressions to "regular" C#.

people

See more on this question at Stackoverflow