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)
Before
Section titled “Before”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
.