C#:如何获取计数并将结果从嵌套列表中分组为唯一项?

时间:2018-12-07 07:59:37

标签: c# linq

我已经尝试/搜索了2天。我有这种对象类型。

    public class QCProcessDetail
    {
        public DateTime CreatedDateUTC { get; set; }
        public string CreatedByUser { get; set; }
        public DateTime CompletedDateUTC { get; set; }
        public string CompletedByUser { get; set; }
        public string Location { get; set; }
    }

    public class QCProcessStages
    {
        public string SerialNumber { get; set; }
        public ICollection<QCProcessDetail> QCProcessDetails { get; set; }
    }

我正在尝试获取唯一的“ createdByUsers”计数。我可以使用从这里看到的各种示例来获取用户名,但结果显示为

CreateByUserX = 1;
CreatedByUserY = 1;
CreatedByUserX = 1; ...

我需要这样的结果

CreatedByUserX = 2
CreateByUserY = 1

1 个答案:

答案 0 :(得分:1)

为您更新的示例数据相应地更新我的答案:

stages
    .SelectMany(s => s.QCProcessDetails)
    .GroupBy(d => d.CreatedByUser)
    .ToList()
    .ForEach(g => Console.WriteLine(g.Key + " = " + g.Count()));