【问题标题】:Left Outer Join左外连接
【发布时间】:2011-09-10 15:39:12
【问题描述】:
List<ServicePacksDTO> allServicePacks = new List<ServicePacksDTO>();
        using (var db = new DataContext())
        {
            allServicePacks=(
                    from sp in db.ServicePacks
                    join st in db.States.DefaultIfEmpty() on sp.State_id equals st.State_Id
                    join type in db.ServiceTypes on sp.ServiceType_Id equals type.ServiceType_Id
                    where
                     (type.ServiceType_desc.ToLower() == "accepted") 
                    orderby sp.AustState_id
                    select sp.ToServicePacksDTO(db)).ToList();
        }

当前代码工作正常,直到我尝试对状态进行外部连接。有没有容易做到这一点?

【问题讨论】:

    标签: c# linq-to-sql outer-join


    【解决方案1】:

    首先,您需要提供更多“工作正常,直到我尝试做 xx”的细节。

    什么不起作用?有错误吗?出乎意料的结果?

    忘记 DTO 投影和 db.ServiceTypes 加入,在 @​​987654322@ 和 db.States 之间进行 LOJ,这样做:

    var x = (from sp in db.ServicePacks
    join st in db.States on sp.State_id equals st.State_id into spst
    from x in spst.DefaultIfEmpty()
    select new { /* fields */ }
    ).ToList();
    

    首先尝试一下,确保它有效(它应该),然后添加您的其他连接和投影。

    【讨论】:

      猜你喜欢
      • 2011-10-01
      • 2016-10-02
      • 1970-01-01
      • 2014-02-24
      • 2019-01-22
      • 2016-10-29
      • 2011-08-10
      • 2011-02-14
      • 2015-09-17
      相关资源
      最近更新 更多