我有以下LINQ查询,用于对所有员工进行分组。
var groups =
from employee in employees
group employee by employee.ReportsTo;
该链接返回一个IGrouping对象。
我现在想使用ReportsTo订购此结果,其中ReportsTo是每个员工的经理的名称。我怎样才能用C#实现这个目标?这可能吗?
答案 0 :(得分:5)
var groups = from employee in employees
group employee by employee.ReportsTo into g
orderby g.Key
select g;
答案 1 :(得分:0)
我宁愿这样写,因为它更“可读”
var groups = employees.GroupBy(employee => employee.ReportsTo).OrderBy(group => group.Key)