2017-01-10 34 views
0

我有一個SQL查詢提取排序數據的總和列值。我有一個名爲CustomerPoint的表格,它有兩列,名稱分別爲CusTomerIDPoint,我想提取最高點SUM的人。這是我的SQL查詢,它運行正常。但我需要在EF6.3中使用LambdaExpressionsLinq查詢執行它。將MSSQL查詢轉換爲Lambda表達式或LINQ。分組加總和排序

SELECT SUM(Point) AS POINTS, CustomerID FROM CustomerPoint AS POINTS GROUP BY CustomerID ORDER BY POINTS 

謝謝你的幫忙!

回答

2

事情是這樣的:

from p in context.Points 
group p by p.CustomerID into gr 
select new { CustomerID = gr.Key, POINTS = gr.Sum(c=>c.Point) } into re 
orderby re.POINTS 
1

請試試這個。

from cp in db.CustomerPoints 
Group cp by cp.CusTomerID into cpg 
select new { CusTomerID = cpg.Key, Points = cpg.Sum(c => c.Point)} 
orderby cpg.Points 
1

而對於多樣性的λ形式:

var query = youContext.CustomerPoints 
      .GroupBy(x => x.CustomerID) 
      .Select(x => new { Points = x.Sum(y => y.Point), 
           CustomerID = x.Key }) 
      .OrderBy(x => x.Points);