The LINQ queries you’ve seen and written so far use QUERY SYNTAX, which expresses your query as a series of clauses (from
, where
, etc.) in a statement. LINQ also provides another way of expressing queries, called METHOD SYNTAX, which, as you might expect, expresses the query using method calls rather than statements.
Instead of expressing a query as a statement:
You can express it as a method call:
Query syntax is the recommended format (does anybody actually enjoy writing lambdas?), but some LINQ methods, like Count()
and Average()
, can only be expressed using method syntax. Fortunately, you can mix query and method syntax if you wrap the portion of the query that’s written as a statement in parentheses: