Query vs method syntax
Write the same LINQ with query or method syntax; see compiler translation and when to choose each style.
Query vs method syntax is a free C# Academy lesson on CoddyKit. This is lesson 3 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C# Academy learning path, and your progress syncs across the web and the CoddyKit app. The C# Academy course includes 3 lessons in total.
Two syntaxes
Two faces of LINQ:
- Method syntax: xs.Where(...).Select(...)
- Query syntax: from x in xs where ... select ...
- They are equivalent for most operators.
Method syntax
Method syntax chains extension methods like Where and Select.
using System;
using System.Linq;
public class Program
{
public static void Main(string[] args)
{
string[] words = new string[] { "dog", "tiger", "ant", "eagle" };
var result = words
.Where(w => w.Length >= 4)
.Select(w => w.ToUpper());
foreach (string w in result) Console.WriteLine(w);
}
}
Query syntax
Query syntax reads like SQL: from, where, select. The compiler turns it into method calls.
using System;
using System.Linq;
public class Program
{
public static void Main(string[] args)
{
string[] words = new string[] { "dog", "tiger", "ant", "eagle" };
var query =
from w in words
where w.Length >= 4
select w.ToUpper();
foreach (string w in query) Console.WriteLine(w);
}
}
Ordering both ways
Both styles support filtering, ordering, and selecting. They produce the same results.
using System;
using System.Linq;
public sealed class City
{
public string Name;
public int Pop;
public City(string name, int pop) { Name = name; Pop = pop; }
}
public class Program
{
public static void Main(string[] args)
{
City[] cities = new City[]
{
new City("Rome", 2873000),
new City("Oslo", 634000),
new City("Riga", 605000),
new City("Rio", 6748000)
};
// Method
var m = cities
.Where(c => c.Pop >= 700000)
.OrderBy(c => c.Name)
.Select(c => c.Name);
// Query
var q =
from c in cities
where c.Pop >= 700000
orderby c.Name ascending
select c.Name;
foreach (string name in m) Console.WriteLine(name);
Console.WriteLine("---");
foreach (string name in q) Console.WriteLine(name);
}
}
Mixing styles
Not every operator has a query keyword. It is common to mix styles: write a query then call Count, Skip, Take, or ThenBy.
using System;
using System.Linq;
public class Program
{
public static void Main(string[] args)
{
int[] data = new int[] { 5, 3, 9, 1, 7, 2, 8 };
// Query + method mix: query part, then Count() method
var q =
from n in data
where n > 3
select n;
int howMany = q.Count(); // no query keyword for Count
Console.WriteLine("Count >3 = " + howMany);
// Paging usually uses method syntax
var page = data.OrderBy(n => n).Skip(2).Take(3);
foreach (int x in page) Console.WriteLine(x);
}
}
When to use which
Guidelines:
- Use method syntax for short chains and when operators lack query keywords.
- Use query syntax when it reads better for multi-step filtering/ordering.
- Mix if needed; they are equivalent.
Query vs method truth
Recap
Recap: Query and method syntax are two views of the same LINQ. Pick the style that reads best and mix when operators lack query keywords.
Learn C# with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 93
- Lessons
- 346
Frequently Asked Questions
Is the “Query vs method syntax” lesson free?
Yes — the full text of “Query vs method syntax” is free to read here on the web. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C# Academy course, upgrade to CoddyKit PRO. The C# Academy course includes 3 lessons in total.
What will I learn in “Query vs method syntax”?
Write the same LINQ with query or method syntax; see compiler translation and when to choose each style. You practise C# Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start C# Academy?
No prior experience is required. C# Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 3 of 3.
How long does the “Query vs method syntax” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this C# Academy lesson?
Yes. Every C# Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- IEnumerable pipelines; deferred execution
- Select, Where, OrderBy, Take/Skip
- Query vs method syntax