2016-04-05 191 views
1

我如何提供條件在這個查詢與i​​nto子句?Linq在哪裏條入

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必須過濾並且應該與左外部聯接連接。

的誤差是

查詢體必須與選擇子句或組子句

結束。 感謝

+1

你可以使用'db.UserPrivDtls.Where(EE1 => EE1 .pageid == pg)',就像你使用'db.UserMsts.Where'一樣。 –

+0

@GertArnold謝謝它的工作。應該讓它成爲答案。 – Deb

回答

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, 
           };