C# Feature β LINQ query expressions
Overview
Section titled βOverviewβQuery expressions provide a declarative syntax for composing queries that the compiler translates to method calls.
Introduced In
Section titled βIntroduced Inβ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;Annotated Example
Section titled βAnnotated Exampleβvar grouped = from p in people group p by p.City into g orderby g.Key select new { City = g.Key, Count = g.Count() };Gotchas & Best Practices
Section titled βGotchas & Best Practicesβ- Prefer method syntax for complex lambdas; mix and match as needed.
- Be mindful of deferred execution.
Related Features
Section titled βRelated FeaturesβVersion Notes
Section titled βVersion Notesβ- Works with anonymous types and
var.