linqlinq-to-sqlleft-joinon-clause

Linq left joining with non trivial condition


This is fine, it produces a left join

var q =
    from c in categories
    join p in products 
    on c equals p.Category into ps
    from p in ps.DefaultIfEmpty()
    select new { Category = c, ProductName = p == null ? "(No products)" : p.ProductName };

But what about if I wanted to do something like this:

...
on p.date between c.startdate and c.enddate
...

Solution

  • var q =
        from c in categories
        join p in products 
        on c equals p.Category into ps
        from p in ps.DefaultIfEmpty()
        where p.date >= c.startdate && p.date <= c.enddate
        select new { Category = c, ProductName = p == null ? "(No products)" : p.ProductName };