Linq查询与列表< >

本文关键字:列表 查询 Linq | 更新日期: 2023-09-27 18:18:46

我有一个列表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是可能的吗?

Linq查询与列表< >

GroupBy()是否足够?

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