如何在Linq中执行左外连接?

时间:2014-07-25 13:34:16

标签: c# sql linq join

我是Linq的新手,我需要将此查询转换为DocumentStores和Orders之间的左外连接,因为并非所有文档都与订单相关:

    var documents=(from d in _dataContextOrders .DocumentStores 
                           join o in _dataContextOrders.Orders on d.OrderID equals o.ID
                           join t in _dataContextOrders .DocumentTypes on d.DocumentType equals t.DocTypeID
                       select new
                           {
                               d.ID,
                               o.PORef ,
                               t.DocTypeDescription,
                               d.Name,
                               d.ContentType
                           }).ToList();

我如何实现这一目标?

1 个答案:

答案 0 :(得分:1)

from a in dataContext.<tableA>
join _b in dataContext.<tableB> on a.id equals _b.aid into _b
from b in _b.DefaultIfEmpty()
select <whatyouwanttoselect>
如果ids上的连接失败,

b将为null