2017-08-04 64 views
0

我想這個SQL查詢轉換:轉換多個SQL連接到實體框架

select t.id,t.name as table_name,r.name as room_name,iif(c.id is null ,0,1) as opened, 
from gs_pos_t t 
inner join gs_pos_r r on t.id_r=r.id 
left join gs_pos_c c on t.id=c.id_t 
left join gs_pos_pz p on c.id=p.id_c 
where c.open_date is not null 
group by 1,2,3,4 

我需要從所有客房中的所有表和狀態,如果上面有開盤銷售,在這種情況下,電流的總和開售

我已經試過,沒有運氣是這樣的:

 db.GS_POS_t.Join(
         db.GS_POS_r, 
         t => t.ID_r, 
         r => r.ID, 
        (tables, rooms) => new {tables,rooms}) 
       .GroupJoin(
        db.GS_POS_C 
         .GroupJoin(
          db.GS_POS_C_PZ, 
          c => c.ID, 
          p => p.ID_C, 
          (sales, p) => new { sales, s_detail= p.DefaultIfEmpty() } 
          ) 
          , 
        mg => mg.tables.ID, 
        cg => cg.sales.ID_t, 
        (tablesrooms, saleswithdetail) => new { tablesrooms, saleswithdetail } 
       ).Where(r => r.saleswithdetail.) <=Here !!!! 
      ); 

這是模型:

GS_POS_T (tables) 
     public long ID { get; set; } 
     public short ID_R { get; set; } 
     public string NAME { get; set; } 
     public virtual ICollection<GS_POS_C> GS_POS_C { get; set; } 
     public virtual GS_POS_R GS_POS_R { get; set; } 
     ... 

GS_POS_R (rooms) 

     public short ID { get; set; } 
     public string NAME { get; set; } 
     public virtual ICollection<GS_POS_T> GS_POS_T { get; set; } 
     ... 

GS_POS_C (sales) 
     public long ID { get; set; } 
     public long ID_T { get; set; } 
     public Nullable<System.DateTime> OPEN_DATE { get; set; } 
     ......  
     public virtual GS_POS_M GS_POS_M { get; set; } 
     public virtual ICollection<GS_POS_C_PZ> GS_POS_C_PZ { get; set; } 


GS_POS_C_PZ (sale details) 
     public long ID { get; set; } 
     public long ID_C { get; set; } 
     public Nullable<decimal> CANT { get; set; } 
     public Nullable<decimal> PRICE { get; set; } 
     public virtual GS_POS_C GS_POS_C { get; set; } 
     ... 

部分解決 我有一個代碼,幾乎工作,但2個問題:

。我不能在最終結果ID字段包括db.GS_POS_C

ExceptionMessage 「:」由於物化值爲空,轉換爲值類型'System.Int64'失敗。無論是結果型的通用 參數或查詢必須使用可空類型。」

。我想它

from t in db.GS_POS_T 
     join r in db.GS_POS_R on t.id_r equals r.id //it is inner join statement 
     join c in db.GS_POS_C on t.id equals c.id_t into ct 
       from sub1 in ct.DefaultIfEmpty() //it is left join statement 
     join p in db.GS_POS_PZ on sub1.id equals p.id_c into psub1 
       from sub2 in psub1.DefaultIfEmpty() //it is left join statement 
       where t.activ == 1 
     select new { t.id, t.name, rname = r.name, sub2.cant, sub2.price } into x 
     group x by new { id = x.id, name = x.name, rname = x.rname } into g 
     select new 
     { 
      tid = g.Key.id, 
      tname = g.Key.name, 
      rname = g.Key.rname, 
      sumpcantpprice = g.Sum(abc => abc.price * abc.cant) 
     }; 

感謝轉換爲方法的語法任何幫助!

+1

怎麼做你的模型是什麼樣子? – osanger

+0

嗨,我已在第一篇文章中加入 – mrapi

+1

您應該使用導航屬性(如'GS_POS_T'),而不是加入。 –

回答

1

對於linq中的左外連接,您可以使用以下語法:

DefaultIfEmpty(new Model()) 

So ,這可能會給你一個想法,我很快就寫出了它,所以它可能不會從第一次嘗試。我不知道你的車型,所以在LINQ語句來替換模型,並給它一個嘗試:

var result = from t in gs_pos_t 

         join r in gs_pos_r on t.id_r equals r.id 

         join c in gs_pos_c on t.id equals c.id_t into firstList from lt1 in firstList.DefaultIfEmpty(new Model1()) 

         join p in gs_pos_pz onc.id equals p.id_c into secondList from lt2 in secondList.DefaultIfEmpty(new Model2()) 

         where c.open_date != null 

         select new 
         { 
          t.id, 
          t.name, 
          r.name, 
          lt1.id == null ? 0 : 1 
         }; 

希望它可以幫助

+0

嗨,感謝您的aswer,我使用的方法語法,並嘗試.DefaultIfEmpty但最後.GroupJoin同樣的問題我不能從t.cgcom選擇字段時,我按下點 – mrapi

+0

你可以請粘貼你寫的代碼與DefaultIfEmpty? –

+0

我已經在第一篇文章中更新了代碼,我無法從saleswithdetail中選擇任何字段:.Where(r => r.saleswithdetail。<= Here !!!! – mrapi