前段時間我玩轉pivoting和ExpandoObject。這當然不是生產代碼。
public static dynamic pivot(IEnumerable<Employee> rows)
{
IDictionary<string, Object> expando = new ExpandoObject();
expando["Joindate"] = rows.First().Joindate;
foreach (var row in rows)
{
expando[row.Department] = row.Salary;
}
return (dynamic)expando;
}
然後在某處
var employees = new ObservableCollection<Employee>() {
new Employee() {ID=1, Department="NET", Salary=5000, Joindate=new DateTime(2011,04,08)},
new Employee() {ID=2, Department="NET", Salary=6000, Joindate=new DateTime(2011,04,07)},
new Employee() {ID=3, Department="JAVA", Salary=7000, Joindate=new DateTime(2011,04,08)},
new Employee() {ID=4, Department="JAVA", Salary=8000, Joindate=new DateTime(2011,04,07)},
new Employee() {ID=5, Department="NET", Salary=9000, Joindate=new DateTime(2011,04,06)}
};
var distinctDates = employees.Select(j => j.Joindate).Distinct().OrderByDescending(d => d);
var salaryByDepartmentAndJoindate = distinctDates.Select(d => pivot(employees.Where(jd => jd.Joindate == d)));
var result = new ObservableCollection<dynamic>(salaryByDepartmentAndJoindate);
會的結果是什麼,如果有一個'6 .NET 9500 04/08/2011'? –
它看起來像你試圖擺動你的數據。 –