linq聚合

发布于 2024-07-16 21:43:33 字数 571 浏览 1 评论 0 原文

class Category
{        
    public string Name { get; set; }
    public int Count { get; set;}
}


Name Count
AA   2
BB   3
AA   4

我有一个 IEnumerable

并希望获得具有唯一名称的类别列表以及多个条目的总和

输出

Name Count
AA   6
BB   3

更新

class Category 
{ 
public string Name { get; set; } 
public int CountA { get; set;} 
public int CountB { get; set;} 
public string Phone { get; set;} 
}

如何我对两列求和。 电话列可以是最后一行或任意行

class Category
{        
    public string Name { get; set; }
    public int Count { get; set;}
}


Name Count
AA   2
BB   3
AA   4

I have an IEnumerable<Category>

and would like to get a list of Categories with unique names and the sum of multiple entries

Output

Name Count
AA   6
BB   3

Update

class Category 
{ 
public string Name { get; set; } 
public int CountA { get; set;} 
public int CountB { get; set;} 
public string Phone { get; set;} 
}

How would I sum two columns. and the phone column can be the last row or any row

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

奶茶白久 2024-07-23 21:43:33

您更新的问题在电话号码方面并不完全清楚,但我怀疑您想要类似的内容:

    var query = from category in list
                group category by category.Name into grouped
                select new { Name = grouped.Key,
                             SumA = grouped.Sum(x => x.CountA),
                             SumB = grouped.Sum(x => x.CountB),
                             Phone = grouped.Last().Phone };

grouped.Last() 更改为 grouped.First() 会顺便说一句,变得更有效率。

一般来说,以这种方式评估多个聚合并不是非常有效。 Push LINQ 项目使其效率更高,但代价是不太易于使用。 如果您需要处理大量数据,您可能需要研究它。

Your updated question isn't entirely clear in terms of the phone number, but I suspect you want something like:

    var query = from category in list
                group category by category.Name into grouped
                select new { Name = grouped.Key,
                             SumA = grouped.Sum(x => x.CountA),
                             SumB = grouped.Sum(x => x.CountB),
                             Phone = grouped.Last().Phone };

Changing grouped.Last() to grouped.First() would be more efficient, by the way.

Evaluating multiple aggregates in this way isn't terribly efficient in general. The Push LINQ project developed by myself and Marc Gravell makes it a lot more efficient at the cost of not being quite as easy to use. You might want to look into it if you need to deal with a lot of data.

思慕 2024-07-23 21:43:33
var foo = new List<Category>() { 
                new Category() { Name = "AA", Count = 2},
                new Category() { Name = "BB", Count = 3},
                new Category() { Name = "AA", Count = 4}
            };

            var bar = foo.GroupBy(c => c.Name).Select(g => new Category(){ Name = g.Key, Count = g.Sum(c => c.Count) });
var foo = new List<Category>() { 
                new Category() { Name = "AA", Count = 2},
                new Category() { Name = "BB", Count = 3},
                new Category() { Name = "AA", Count = 4}
            };

            var bar = foo.GroupBy(c => c.Name).Select(g => new Category(){ Name = g.Key, Count = g.Sum(c => c.Count) });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文