简体   繁体   English

分组并计数

[英]Group by and Count on List

I have a list of model class with some fields in it,I want to apply group by and count on the base of ID on it,in my lst I have data like this 我有一个模型类的列表,其中有一些字段,我想按组应用,并以ID为基础,首先,我有这样的数据

lst[0]=Id=10,Name="user1",Gender="Male",Course="course1";
lst[1]=Id=10,Name="user1",Gender="Male",Course="course2";
lst[2]=Id=10,Name="user1",Gender="Male",Course="course3";
lst[3]=Id=11,Name="user2",Gender="Male",Course="course4";

I want to count the course with group by on Id. 我想按ID分组计算课程。 This is how I am doing 我就是这样

var result = lst.GroupBy(n => n.Id).Select(c => new { Key = c.Key, total = c.Count()});

But after applying this I am not getting any value in result variable(ie result.Name or result[0].Name) How can I apply group by and count in this list so I get the list data after applying the group by too. 但是应用此方法后,结果变量中没有任何值(即result.Name或result [0] .Name),如何应用分组依据并计入此列表中,因此也可以在应用分组依据后获得列表数据。

Having used GroupBy you have projected ( Select(..) ) to an anonymous object which throws away the selected items in the group; 使用GroupBy您已经将( Select(..) )投影到一个匿名对象上,该对象将丢弃该组中的所选项目。 your result will be an IEnumerable<anonymous> of items with properties Key and total 您的结果将是具有属性Keytotal的项的IEnumerable<anonymous>

var result = lst.GroupBy(n => n.Id)
                .Select(c => new { Key = c.Key, total = c.Count()});

If you want to keep the selected items, you need to include this in your projection 如果要保留选定的项目,则需要将其包括在投影中

var result = lst.GroupBy(n => n.Id)
                .Select(c => new { Key = c.Key, total = c.Count(), Items = c});
foreach(var r in result)
{
    Console.WriteLine("Key: {0} Count:{1}",r.Key,r.total)
    foreach(var i in r.Items)
        Console.WriteLine(i.Name);
}

The reason is because you are grouping by Id and just provinding an anonymous object with Key (which is the Id ) and a Count . 原因是因为您要按Id分组,而只是使用Key (即Id )和Count来提供匿名对象。 If you want to read the values, each item of the result group set will have the Count and the values where you can loop between them. 如果要读取值,则结果组集中的每个项目都将具有“计数”和可以在它们之间循环的值。 For sample: 样品:

var result = lst.GroupBy(n => n.Id);

foreach (var g in result)
{
    Console.WriteLine("For group {0} the total is {1}.", g.Key, g.Count());
    foreach(var item in g)
    {
        Console.WriteLine("Name: {0}", item.Name);
    }
}

All you have to do is call ToList or ToArray . 您所要做的就是调用ToListToArray

var result = lst.GroupBy(n => n.Id)
    .Select(c => new 
    { 
        Key = c.Key, 
        total = c.Count(),
        Gender = c.First().Gender
    }).ToList();

Or alternativly - to avoid multiple iterations of the same enumeration: 或交替使用-避免同一枚举的多次迭代:

var result = lst.GroupBy(n => n.Id)
    .Select(c => 
    { 
        var firstEl = c.First();
        return new {
            Key = c.Key, 
            total = c.Count(),
            Gender = firstEl.Gender,
            Name = firstEl.Name,
            Course = firstEl.Course
        }
    }).ToList();

Select will only return a query which is deferredly executed . Select将仅返回延迟执行的查询。 This means you get the result when you actually iterate on the enumeration, which is enforeced by calling one of the mentioned methods. 这意味着您在实际迭代枚举时会得到结果,这是通过调用上述方法之一实现的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM