按字段对列表进行分组,并将项添加到字典中

时间:2011-05-12 06:06:10

标签: c# linq

我上课了。

public class Compaints
{
    public string CustomerNumber{get; set;},

    public string Complaint{get; set} 
}

我有一份投诉清单。我需要按customerNumber分组并将其添加到字典中说类型 Dictionary<string, int> - 字符串将是客户编号,而int将是计数。

如何在linq中执行此操作?

感谢。

1 个答案:

答案 0 :(得分:5)

var countByCustomer = complaints.GroupBy(row => row.CustomerNumber)
    .ToDictionary(grp => grp.Key, grp => grp.Count());