2014-03-19 47 views
0

我想轉換的T-SQL是離開與組的加入和LINQ總結爲sql

select c.description, sum(t.quantity), sum(t.Price) from trn_itm t 
left join CAFE c on t.Item_key = c.dfm_key 
where transaction_key = 87378 
group by c.Description 

我的LINQ是

var query = from p in DfmSession.CurrentContext.TRN_ITMs 
      join c in DfmSession.CurrentContext.CAFEs on p.Item_key equals c.Dfm_key into t 
      from rt in t.DefaultIfEmpty() 
      where p.Transaction_key == _dfmkey 
      select new 
      { 
       Dfm_key = (int?)rt.Dfm_key, 
       rt.Description, 
       p.Quantity, 
       p.Price 
      }; 

我得到的總和(p.Quantity怎麼辦)和Sum(p.Price)?

回答

0
group by rt.Description into g 

,然後g使用自己選擇的聚合功能,例如:

select new { Price = g.Sum(x => x.Price) } 
1
var query = from p in DfmSession.CurrentContext.TRN_ITMs 
      join c in DfmSession.CurrentContext.CAFEs on p.Item_key equals c.Dfm_key into t 
      from rt in t.DefaultIfEmpty() 
      where p.Transaction_key == _dfmkey 
      group p by rt.Description into g 
      select new 
      { 
       Description = g.Keu 
       Quantity = g.Select(x => x.Quantity).Sum() 
       Price = g.Select(x => x.Price).Sum() 
      }; 
+0

您的意思是說明= g.Key,數量= g.Sum(x => x.Quantity),Price = g.Sum(x => x.Price) – Pramesh

0
group by su.Description into h 

,然後使用自己選擇的H,例如聚合方法:

select new { Price = h.Sum(y =? y.Price) } 
1
var query = from p in DfmSession.CurrentContext.TRN_ITMs 
     join c in DfmSession.CurrentContext.CAFEs on p.Item_key equals c.Dfm_key into t 
     from rt in t.DefaultIfEmpty() 
     where p.Transaction_key == _dfmkey 
     group by rt.Description into g 
     select new 
    { 
     description = g.Key, 
     Price = g.Sum(x => x.Price), 
     Quantity = g.Sum(x => x.Quantity) 
    }; 
+0

我收到了錯誤。我得到的消息是無法將lambda表達式轉換爲類型System.Collections.Generic.IEqualityComparer ',因爲它不是委託類型 – Pramesh