使用LINQ对对象集合进行排序? [英] Sorting an object collection using LINQ?
本文介绍了使用LINQ对对象集合进行排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
|
|
推荐答案
这对LINQ来说当然是可行的。以下是您需要的查询:
从salesTransactions中的s发送新的{s.EmployeeID,s .EmployeeName}到新的组中了一个g
let totalSales = g .Sum(s => s.Amount)
orderby totalSales descending
select g.Key.EmployeeName +":" + totalSales.ToString(" C")
首先我们按EmployeeID对销售进行分组。我们还带有EmployeeName,因为我们稍后需要显示它。接下来,我们评估每个组的总销售额。我们使用"let"因为它节省了我们不得不评估它两次(我们需要这些数据用于排序和在最终投影中显示)。然后我们按这个聚合顺序,然后选择每个组的EmployeeName(来自Key)和我们之前计算的totalSales聚合。
乔
This is certainly possible with LINQ. Here's the query that you need:
from s in salesTransactions
group s by new { s.EmployeeID, s.EmployeeName } into g
let totalSales = g.Sum (s => s.Amount)
orderby totalSales descending
select g.Key.EmployeeName + ": " + totalSales.ToString ("C")
First we group the sales by the EmployeeID. We also bring along the EmployeeName because we'll need to display it later on. Next, we evaluate the total sales for each group. We use a "let" clause because it saves us having to evaluate it twice (we need this data both for ordering and to display in the final projection). Then we order by this aggregate, and then select each group's EmployeeName (from the Key) and the totalSales aggregate we previously calculated.
Joe
这篇关于使用LINQ对对象集合进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文