Query Syntax vs Method Syntax

LINQ can also be written in query syntax, from ... where ... orderby ... select, which reads like SQL. The compiler turns it into the same method calls. Method syntax is more common, and some methods, such as Count and First, have no query keyword.

Journeyman · C# · Updated

After this lesson you can

  • Rewrite a LINQ query between query syntax (from, where, orderby, select) and method syntax

Key takeaways

  • Query syntax, from name in names ... select ..., compiles to the same method calls as method syntax, so the two run exactly the same way.
  • Each keyword matches a method; where is Where, orderby is OrderBy, select is Select and group ... by is GroupBy.
  • Unlike SQL, query syntax puts select last, and a query must end with select (or group).
  • Methods with no keyword, such as Count or ToList, are called on the query wrapped in parentheses.
  • Write method syntax by default, and be ready to read query syntax in someone else's code.

Everything so far has been method syntax: a chain of method calls with lambdas. LINQ has a second way to write the same query, query syntax, which looks a lot like SQL.

The same query, two ways

List<string> names = new List<string> { "Olivia", "Ethan", "Sophia", "Liam", "Emma", "Noah", "Ava", "Jackson" };

List<string> methodSyntax = names
    .Where(name => name.Length > 4)
    .OrderBy(name => name)
    .Select(name => name.ToUpper())
    .ToList();

List<string> querySyntax =
    (from name in names
     where name.Length > 4
     orderby name
     select name.ToUpper())
    .ToList();

Console.WriteLine(string.Join(", ", methodSyntax));
Console.WriteLine(string.Join(", ", querySyntax));
ETHAN, JACKSON, OLIVIA, SOPHIA
ETHAN, JACKSON, OLIVIA, SOPHIA

Query syntax:

  • starts with from name in names: for each name in names. name plays the part of the lambda's parameter, for the whole query.
  • uses keywords instead of methods: where, orderby, select, with no lambdas or arrows.
  • must end with select (or group).

The compiler turns query syntax into the very same method calls, so the two run exactly the same way.

Side by side

Query syntax Method syntax
from p in menu menu
where p.Price < 3.00m .Where(p => p.Price < 3.00m)
orderby p.Price .OrderBy(p => p.Price)
orderby p.Price descending .OrderByDescending(p => p.Price)
orderby p.Category, p.Name .OrderBy(p => p.Category).ThenBy(p => p.Name)
select p.Name .Select(p => p.Name)
group p by p.Category .GroupBy(p => p.Category)

One difference from SQL: in query syntax, select comes last, after from. That's so that by the time you type p., Visual Studio already knows what p is and can suggest its properties.

Mixing the two

Many methods have no keyword: Count, Sum, First, Take, Distinct, ToList. To use them with query syntax, put the query in parentheses and call the method on it, as .ToList() was above:

List<int> scores = new List<int> { 72, 95, 48, 88, 61 };

int passes = (from score in scores
              where score >= 60
              select score).Count();

int passesToo = scores.Count(score => score >= 60);

Console.WriteLine(quot;{passes} {passesToo}");
4 4

Which to use?

Both are correct, and you'll meet both. Most C# code, and this course, uses method syntax:

  • every LINQ method works in it, so there's no switching between styles,
  • a short query is often a single line: scores.Count(score => score >= 60),
  • the lambdas are the same ones you pass to FindAll, Sort and, later, to ASP.NET Core and Entity Framework Core.

Query syntax shines for long queries with joins and grouping, where it can read more clearly. Choose one style per query, and be ready to read the other.

Check yourself

1 question to try on the lesson page.

So what

from ... where ... orderby ... select and .Where().OrderBy().Select() are two spellings of one query. Write method syntax by default, and when you meet query syntax in someone else's code, translate it in your head keyword by keyword.

Key terms

Cheat sheets