Skip to content

C# Feature – LINQ query expressions

Query expressions provide a declarative syntax for composing queries that the compiler translates to method calls.

C# 3 (2007)

var adults = people.Where(p => p.Age >= 18).Select(p => p.Name);
var adults = from p in people
where p.Age >= 18
select p.Name;
var grouped = from p in people
group p by p.City into g
orderby g.Key
select new { City = g.Key, Count = g.Count() };
  • Prefer method syntax for complex lambdas; mix and match as needed.
  • Be mindful of deferred execution.
  • Works with anonymous types and var.