网站链接: 我爱捣鼓
当前位置: 首页 > 数据库  > Linq

LINQ实现sql中类似left join左连的方法

2021/2/21 19:31:50

在Linq语句中实现SQL语句中类似left join的方法:左连接写法(from a in this.ObjectContext.T_SCHEDULE join b in this.ObjectContext.T_SPGROUP on a.SPGID equals b.ID into ab from c in ab.DefaultIfEmpty() //多个连接条件写法join d in this.ObjectContext.T_SCHED…

在Linq语句中实现SQL语句中类似left join的方法:


  • 左连接写法

(from a in this.ObjectContext.T_SCHEDULE

 join b in this.ObjectContext.T_SPGROUP on a.SPGID equals b.ID into ab

 from c in ab.DefaultIfEmpty() 

  • //多个连接条件写法

join d in this.ObjectContext.T_SCHEDULETIME on new { A = a.DID, B = a.SPGID } equals new { A = d.ID, B = d.SPGID }  

 orderby a.SPGID,a.TIME,d.STARTTIME


 select new ScheduleModel

  {

        SDID = (int)a.SDID,

        SPGID = (int)a.SPGID,

        SPGName = (c.NAME == null ? "" : c.NAME),//左连接表字段为null的话赋默认值

        ScheduleDate = a.TIME

 }).ToList();

相关资讯

    暂无相关的数据...