使用List <t> </t>进行Linq查询

时间:2011-12-23 17:06:41

标签: c# linq

我的列表List<Employee>包含

等数据
Name   Year  Salary

John   2001   10000
Warn   2001   11000
George 2001   12000
Nick   2001   13000
Warn   2002   14000
John   2002   15000
Adam   2002   16000
Kile   2003   17000
John   2003   18000
Kile   2004   19000
Warn   2004   20000

我需要像那样转换

    Year  John   Warn   George   Nick    Adam   Kile
----------------------------------------------------------
    2001  10000  11000  12000    13000   0      0
    2002  15000  14000  0        0       16000  0
    2003  18000  0      0        0       0      17000
    2004  0      20000  0        0       0      19000

LINQ可以吗?

2 个答案:

答案 0 :(得分:2)

GroupBy()是否足够?

employees.GroupBy(x => x.Year);

答案 1 :(得分:2)

var annualGroups = 
        from e in List<Employee> 
        group e by e.Year into y 
        select new { Year = y.Key, Employees = y };