sql等价的linq表达式

时间:2014-01-27 07:45:17

标签: sql linq c#-4.0

以下sql表达式的linq表达式是什么

select BRAND_ID,SUM(QUANTITY) as Qty from SR_TRN_DETAILS where ORDER_ID in 
      (select ORDER_ID from SR_TRN_MAIN where
       MEMO_DATE    between '2014-01-26' and '2014-01-26') 
 group by BRAND_ID

我尝试如下无法结束

var br = (from p in db.SR_TRN_DETAILS
                      where
                          (from ppt in db.SR_TRN_MAIN
                           where
                           ppt.DATE >= DateTime.Now.Date && ppt.DATE <= DateTime.Now.Date.AddDays(-30)
                           select ppt.ORDER_ID).Contains(p.ORDER_ID)
                      group p by p.BRAND_ID into g
                      select new
                      {
                          ID = g.Key,
                          Qty = ???
                      }).ToList();

任何人都非常感谢。谢谢。

1 个答案:

答案 0 :(得分:0)

var br = (from p in db.SR_TRN_DETAILS
          where
              (from ppt in db.SR_TRN_MAIN
               where
               ppt.DATE >= DateTime.Now.Date && ppt.DATE <= DateTime.Now.Date.AddDays(-30)
               select ppt.ORDER_ID).Contains(p.ORDER_ID)
          group p by p.BRAND_ID into g
          select new
          {
              ID = g.Key,
              Qty = g.Select(x => x.Quantity).Sum()
          }).ToList();