Linq凡进入

时间:2016-04-05 20:23:40

标签: c# linq

如何使用into子句提供此查询中的条件?

    var res = from aa in db.UserMsts.Where(p => p.Id == id)
              join bb in db.UserGrpHdrs on aa.usrgrpid equals bb.usrgrpid
              join cc in db.UserGrpDtls on aa.usrgrpid equals cc.usrgrpid where cc.pageid == pg
              join dd in db.UserPrivHdrs on new { aa.usrgrpid, userid = aa.Id } equals new { dd.usrgrpid, dd.userid }  into ddd
              from dd in ddd.DefaultIfEmpty()// <==  left outer join

//Error in following line
              join ee in db.UserPrivDtls on dd.usrprivid equals ee.usrprivid
              where ee.pageid == pg into eee // error here at into

              from ee in eee.DefaultIfEmpty() // <==  left outer join
              select new UserPrivDto{
                     CanAdd = ee.addpriv != null ? ee.addpriv : cc.addpriv,
                     CanEdit = ee.editpriv != null ? ee.editpriv : cc.editpriv,
                     CanView = ee.viewpriv != null ? ee.viewpriv : cc.viewpriv,
                     CanDel = ee.delpriv != null ? ee.delpriv : cc.delpriv,
                                  };

必须过滤UserPrivDtls,并且应该使用左外连接加入。

错误是

  

查询正文必须以select子句或Group子句结束

。 感谢

1 个答案:

答案 0 :(得分:0)

由于您已经将查询语法和流利语法混合在一起,因此最实用的解决方案是向Where添加db.UserPrivDtls条件:

var res = from aa in db.UserMsts.Where(p => p.Id == id)
          join bb in db.UserGrpHdrs on aa.usrgrpid equals bb.usrgrpid
          join cc in db.UserGrpDtls on aa.usrgrpid equals cc.usrgrpid where cc.pageid == pg
          join dd in db.UserPrivHdrs on new { aa.usrgrpid, userid = aa.Id } equals new { dd.usrgrpid, dd.userid }  into ddd
          from dd in ddd.DefaultIfEmpty()

          join ee in db.UserPrivDtls.Where(ee1 => ee1.pageid == pg)
              on dd.usrprivid equals ee.usrprivid
              into eee
          from ee in eee.DefaultIfEmpty()

          select new UserPrivDto{
                 CanAdd = ee.addpriv != null ? ee.addpriv : cc.addpriv,
                 CanEdit = ee.editpriv != null ? ee.editpriv : cc.editpriv,
                 CanView = ee.viewpriv != null ? ee.viewpriv : cc.viewpriv,
                 CanDel = ee.delpriv != null ? ee.delpriv : cc.delpriv,
                              };