Query syntax
Context: Query syntax resembles SQL and is often more readable for complex queries involving joins, grouping, and let clauses.
Basic structure
Section titled “Basic structure”var result = from item in source where condition select item;Example
Section titled “Example”var fruits = new[] { "apple", "banana", "cherry", "date" };var longNames = from f in fruits where f.Length > 5 select f.ToUpper();let clause
Section titled “let clause”var query = from n in numbers let square = n * n where square > 10 select new { n, square };Real-world usage example
Section titled “Real-world usage example”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.