Linq离开了加入非平凡的状态

时间:2010-03-15 14:31:51

标签: linq linq-to-sql left-join on-clause

这很好,它会产生左连接

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 };

但是,如果我想做这样的事情呢?

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

1 个答案:

答案 0 :(得分:1)

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 };