C# LINQ query and method syntax

By Tan Lee Published on Mar 27, 2025  16
LINQ is its flexibility, offering two distinct ways to write queries: query syntax and method syntax.

In LINQ, the query syntax is more declarative and closely resembles SQL, while the method syntax is more functional and leverages LINQ extension methods to chain operations. Both syntaxes ultimately perform the same tasks, but some methods, such as Append or Concat, are only available in method syntax and do not have equivalents in query syntax.

Query Syntax

Query syntax looks similar to SQL and is ideal for those who are already familiar with SQL queries.

For example:

var numbers = new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

var evenNumbers = from num in numbers
                  where num % 2 == 0
                  select num;

foreach (var num in evenNumbers)
{
    Console.WriteLine(num);
}

In this example:

  • from specifies the data source (numbers).

  • where filters the data based on a condition (num % 2 == 0).

  • select specifies what data to retrieve (num).

Method Syntax

Method syntax uses LINQ extension methods to perform queries. It’s more flexible and often used when writing complex queries.

var numbers = new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

var evenNumbers = numbers.Where(num => num % 2 == 0);

foreach (var num in evenNumbers)
{
    Console.WriteLine(num);
}

In this case, Where() is a LINQ extension method that filters the numbers, and the => symbol represents a lambda expression.

Suppose we have a list of words, and we want to find all the words that contain the character 'o'. We will write the solution using both query syntax and method syntax.

using System;
using System.Linq;

class Program
{
    static void Main()
    {
        var words = new string[] { "mountain", "river", "forest", "cloud", "ocean", "desert" };

        // Query syntax
        var queryResult = from word in words
                          where word.Contains('o')
                          select word;

        // Output results of query syntax
        Console.WriteLine("Query Syntax:");
        foreach (var word in queryResult)
        {
            Console.WriteLine(word);
        }

        Console.WriteLine("-----------");

        // Method syntax
        var methodResult = words.Where(word => word.Contains('o'));

        // Output results of method syntax
        Console.WriteLine("Method Syntax:");
        foreach (var word in methodResult)
        {
            Console.WriteLine(word);
        }
    }
}

Query Syntax

var queryResult = from word in words
                  where word.Contains('o')
                  select word;

This LINQ query filters the words array to select all the words that contain the letter 'o'. The from keyword specifies the data source (words), and the where clause filters based on the condition word.Contains('o'). The select clause returns each word that meets this condition.

Method Syntax

var methodResult = words.Where(word => word.Contains('o'));

In the method syntax, we use the Where() extension method, passing a lambda expression (word => word.Contains('o')) that specifies the same condition. The Where() method filters the array, and it works in the same way as the where clause in query syntax.

When you run the program using dotnet run, the output will be:

Query Syntax:
mountain
forest
cloud
ocean
-----------
Method Syntax:
mountain
forest
cloud
ocean

In this case, the words "mountain", "forest", "cloud", and "ocean" all contain the letter 'o', so they are printed. The result is the same for both query and method syntax; the difference lies in how the query is written.

Key Differences Between Query and Method Syntax

  • Readability: Query syntax is more declarative and may feel more familiar to developers who are used to SQL. It’s often easier to understand for simple queries.

  • Flexibility: Method syntax is more versatile and allows for method chaining. This is useful when you want to apply multiple operations, like sorting or grouping, in a single query.

  • Performance: There is no performance difference between query and method syntax. Both are compiled into similar intermediate code by the compiler.

  • Functionality: Some LINQ methods, like Append, Concat, and ToList, are only available in method syntax.

Both syntaxes are powerful, and the choice between them often comes down to personal preference or the complexity of the query. For simple queries, query syntax might feel more natural, while method syntax provides more flexibility, especially when working with chained operations.