0Pricing
C# Academy · Lesson

Query vs method syntax

Write the same LINQ with query or method syntax; see compiler translation and when to choose each style.

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);
  }
}

All lessons in this course

  1. IEnumerable pipelines; deferred execution
  2. Select, Where, OrderBy, Take/Skip
  3. Query vs method syntax
← Back to C# Academy