2016-12-27 39 views
0

我在我的應用程序中使用linq編寫我的子查詢。我在sql server中寫了我的sql子查詢。當我在SQL Server中執行此查詢的結果給予完善和SQL查詢會像如何在mvc5中使用linq編寫子查詢?

我的SQL Server:

select Row_Number() Over(OrderBy Mmname) as Sno,Mmname,Mmcardno,Mmdob,MmEmail,(Select SUM(MSSRNETAMT) from MSCAS where MSCAS.MSSFORMNO = MSMEM.MmCardno and MSCAS.MSSRBILLDT between '01/01/2016' and '30/12/2016') Billopen,(Select SUM(MSSRNETAMT) from MSCAS where MSCAS.MSSFORMNO = MSMEM.MmCardno and MSCAS.MSSRBILLDT between '01/01/2016' and '30/12/2016') BillForm,Mmredpv,Msdval,Mmcontact,Mmdate,Mmaddr,MmCuser,MmCuserdt,Mmusercd, 
    Mmuser,Mmuserdt,Cast(Mmcntrn as int) as Mmcntr from MSMEM inner join MSSCHEDET on MSMEM.Mschuid = MSSCHEDET.MSDID 

我的LINQ查詢

   Var query = (from Ms in db.msmems join Mss in db. Msschedet on Ms. Mechuid equals mss. MSDID select new 
     { 
      name = Ms. Mmname, 
      Billopen = (from mc in db. MSCAS where Ms.  Mmcardno = mc. Mssformno) select mc.MSSRNETAMT); 
    }): 

這只是我試過,但我我得到的錯誤是打開它是金額我得到錯誤

這是我的SQL查詢。我試圖在linq中轉換這個sql查詢。但是我失敗了很多次,我得到了一個錯誤。

+0

你有什麼Linq代碼,你有什麼錯誤? – halfer

回答

0

你能試試這樣的事嗎?如果沒有更好地理解你的表格結構,我會優化它。是否沒有理由不能第二次加入你的MSCAS表?

var query = (from Ms in db.msmems 
      join Mss in db.Msschedet on Ms.Mechuid equals mss.MSDID 
      join Mc in db.MSCAS on Ms.Mmcardno equals Mc.Mssformno 
      select new { 
         name = Ms.Mmname, 
         Billopen = Mc.where(x=>x.MSSRBILLDT >= '01-01-2016' and x.MSSRBILLDT <= '12-30-2016').Sum(z=>z.MSSRNETAMT) 
         } 
      );