Roadmap › LINQ — Language Integrated Query › Module: LINQ Basics
Lessons in this course (12)

1. Query syntax vs method syntax

Basic★ Must knowLesson 1 of 12
csharp
var products = new List<Product>
{
    new(1, "Laptop", "Electronics", 65000, 5),
    new(2, "Mouse", "Electronics", 900, 50),
    new(3, "Desk", "Furniture", 9000, 0),
    new(4, "Chair", "Furniture", 6500, 12),
};

// Query syntax (SQL-like) — compiled into method calls
var q1 = from p in products
         where p.Price > 5000
         orderby p.Price descending
         select new { p.Name, p.Price };

// Method syntax (fluent, lambda-based) — same result
var q2 = products.Where(p => p.Price > 5000)
                 .OrderByDescending(p => p.Price)
                 .Select(p => new { p.Name, p.Price });

public record Product(int Id, string Name, string Category, decimal Price, int Stock);

Query syntax supports from, where, select, orderby, group … by … into, join … on … equals, let. Everything else (Count, First, Distinct, Skip, Take, Any, Sum) needs method syntax. Most teams use method syntax; query syntax is nicer for multi-joins and let.