LINQ supports joins in both query syntax and method syntax. Assume these collections: record Customer(int Id, string Name); record Order(int Id, int CustomerId, decimal Total); var customers = new List<Customer> { new(1, "Asha"), new(2, "Ravi"), new(3, "Neha") }; var orders = new List<Order> { new(101, 1, 500), new(102, 1, 1200), new(103, 2, 300) }; 1. Inner join — only customers who have orders Query syntax: var inner = from c in customers join o in orders on c.Id equals o.CustomerId select new { c.Name, o.Id, o.Total }; Method syntax: var inner2 = customers.Join(orders, c => c.Id, o => o.CustomerId, (c, o) => new { c.Name, o.Id, o.Total }); Result: Asha 101, Asha 102, Ravi 103. Neha is excluded because she has no orders. 2. Group join — each customer with a collection of their orders var grouped = from c in customers join o in orders on c.Id equals o.CustomerId into customerOrders select new { c.Name, Orders = customerOrders, Total = customerOrders.Sum(x => x.Total) }; Method syntax: var grouped2 = customers.GroupJoin(orders, c => c.Id, o => o.CustomerId, (c, os) => new { c.Name, Orders = os, Total = os.Sum(x => x.Total) }); Result: Asha with two orders (1700), Ravi with one (300), Neha with an empty collection (0). A group join produces hierarchical results and never drops left-side items. 3. Left outer join — all customers, with order details or nulls Classic pattern: group join plus DefaultIfEmpty and SelectMany. var left = from c in customers join o in orders on c.Id equals o.CustomerId into os from o in os.DefaultIfEmpty() select new { c.Name, OrderId = o?.Id, Total = o?.Total ?? 0 }; Method syntax: var left2 = customers .GroupJoin(orders, c => c.Id, o => o.CustomerId, (c, os) => new { c, os }) .SelectMany(x => x.os.DefaultIfEmpty(), (x, o) => new { x.c.Name, OrderId = o?.Id, Total = o?.Total ?? 0 }); Result includes "Neha, null, 0". .NET 10 added dedicated LeftJoin and RightJoin operators, so you can write: var left3 = customers.LeftJoin(orders, c => c.Id, o => o.CustomerId, (c, o) => new { c.Name, OrderId = o?.Id }); 4. Joins on multiple keys Use anonymous objects with matching property names and types: join s in shipments on new { o.Id, o.CustomerId } equals new { Id = s.OrderId, s.CustomerId } 5. Cross join from c in customers from p in products select new { c.Name, p.Name } — every combination (use carefully). Joins with Entity Framework Core - EF Core translates Join, GroupJoin with SelectMany/DefaultIfEmpty (and LeftJoin in EF Core 10) into SQL INNER JOIN and LEFT JOIN. - When navigation properties exist, prefer them; EF generates the joins automatically: var result = db.Orders.Select(o => new { o.Id, CustomerName = o.Customer.Name }); - Group joins that return nested collections are translated differently; projections with navigation collections are usually clearer. Performance note: LINQ to Objects Join builds a hash lookup of the inner sequence (O(n + m)), which is far better than nested loops (O(n × m)).
Ravindra Wadile · · 0 likes