【问题标题】:How do I perform a Left Outer Join in Linq? [duplicate]如何在 Linq 中执行左外连接? [复制]
【发布时间】:2014-09-17 09:12:24
【问题描述】:

我是 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();

我如何做到这一点?

【问题讨论】:

标签: c# sql linq join


【解决方案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>

如果 id 上的连接失败,b 将为空

【讨论】:

    猜你喜欢
    • 2018-02-10
    • 1970-01-01
    • 1970-01-01
    • 2010-10-04
    • 2012-03-21
    • 2014-11-26
    • 2010-10-09
    • 1970-01-01
    相关资源
    最近更新 更多