4

I have list List<Employee> it contains data like

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

I need to convert like that

    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

Is it possible with LINQ?

Govind Malviya
  • 13,627
  • 17
  • 68
  • 94
  • 1
    possible duplicate of [Pivot data using LINQ](http://stackoverflow.com/questions/963491/pivot-data-using-linq) – Oded Dec 23 '11 at 17:10
  • You really want as result a list of things with properties like `decimal John` ? Do you have a prepared type for that? – H H Dec 23 '11 at 17:13

2 Answers2

2

Would GroupBy() be sufficient?

employees.GroupBy(x => x.Year);
Cameron
  • 96,106
  • 25
  • 196
  • 225
2
var annualGroups = 
        from e in List<Employee> 
        group e by e.Year into y 
        select new { Year = y.Key, Employees = y };
Jesse Smith
  • 963
  • 1
  • 8
  • 21