Mixing both
Context: You can start with query syntax and then switch to method syntax by wrapping the query in parentheses and calling extension methods.
var numbers = new[] { 1, 2, 3, 4, 5, 6 };
var result = (from n in numbers where n % 2 == 0 select n) .OrderByDescending(n => n) .Take(2);When to mix
Section titled “When to mix”- Use query syntax for the main filtering/joining
- Use method syntax for operations not available in query syntax (e.g.,
Take,Skip,ToDictionary)
Real-world usage example
Section titled “Real-world usage example”Paging with query syntax: Write the base query in query syntax, then add .Skip(page * size).Take(size) using method syntax.
Example: In EF Core, you often write from c in context.Customers where c.Active select c then chain .Skip(10).Take(10).