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

Lam*_*mps 1 c# linq

我上课了.

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

    public string Complaint{get; set} 
}
Run Code Online (Sandbox Code Playgroud)

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

如何在linq中执行此操作?

谢谢.

Mar*_*ell 5

var countByCustomer = complaints.GroupBy(row => row.CustomerNumber)
    .ToDictionary(grp => grp.Key, grp => grp.Count());
Run Code Online (Sandbox Code Playgroud)