我上课了。
public class Compaints
{
public string CustomerNumber{get; set;},
public string Complaint{get; set}
}
我有一份投诉清单。我需要按customerNumber分组并将其添加到字典中说类型
Dictionary<string, int>
- 字符串将是客户编号,而int将是计数。
如何在linq中执行此操作?
感谢。
答案 0 :(得分:5)
var countByCustomer = complaints.GroupBy(row => row.CustomerNumber)
.ToDictionary(grp => grp.Key, grp => grp.Count());