Skip to content

Query syntax

Context: Query syntax resembles SQL and is often more readable for complex queries involving joins, grouping, and let clauses.

var result = from item in source
where condition
select item;
var fruits = new[] { "apple", "banana", "cherry", "date" };
var longNames = from f in fruits
where f.Length > 5
select f.ToUpper();
var query = from n in numbers
let square = n * n
where square > 10
select new { n, square };

Reporting queries: When generating reports from multiple data sources, query syntax with joins and grouping is more intuitive.

Example: In LINQ to XML, query syntax is common for extracting elements.