按语句向组添加更多项目

时间:2014-07-22 21:47:20

标签: c# linq

我有这个问题:

    var rowsPerProvider = (from row in dt.Select()
        let emp = row["f_name"].ToString().Trim()
        group row by emp
        into g
        select g).ToDictionary(
            g => g.Key,
            g => g.ToArray());

如何更新它以过滤更多列?例如,目前它在f_name上。如何将其更新为f_namem_name以及l_name上的群组?

2 个答案:

答案 0 :(得分:3)

使用匿名类:

// (...)
    group row by new { emp, something }

答案 1 :(得分:3)

创建一个包含您要分组的字段的匿名对象:

var rowsPerProvider = (from row in dt.Select()
    group row by new
    {
        emp1 = row["f_name"].ToString().Trim(),
        emp2 = row["m_name"].ToString().Trim(),
        emp3 = row["l_name"].ToString().Trim(),
    }
    into g
    select g).ToDictionary(
        g => g.Key,
        g => g.ToArray());