How to write joins (inner, left) in LINQ with EF Core?
- Inner join example:
var query = from c in context.Customers
join o in context.Orders on c.Id equals o.CustomerId
select new { c.Name, o.OrderDate };
- Left join (using DefaultIfEmpty()):
var query = from c in context.Customers
join o in context.Orders on c.Id equals o.CustomerId
into orders
from o in orders.DefaultIfEmpty()
select new { c.Name, OrderDate = o != null ? o.OrderDate
: (DateTime?)null };