Linq表达要求

时间:2012-08-01 18:49:23

标签: linq

假设我有包含这两个记录的对象列表:

EmployeeId - 类别 - 金额

1 - T - 150

1 - D - 300

我在决赛中想要的结果是看起来像这样:

EmployeeId - TAmount - DAmount

1 - 150 - 300

我如何使用LINQ实现这一点,我只是在我的EmployeeId字段中看不到如何使用一个简单的组...

1 个答案:

答案 0 :(得分:0)

var employeeId = 1;
var query =
    from row in table
    group row by row.EmployeeId into g
    where g.Key == employeeId
    select new
    {
        EmployeeId = row.Key,
        TAmount = g.SingleOrDefault(r => r.Categpry == "T"),
        DAmount = g.SingleOrDefault(r => r.Categpry == "D"),
    };