gpt4 book ai didi

LINQ聚合

转载 作者:行者123 更新时间:2023-12-04 18:54:45 25 4
gpt4 key购买 nike

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


Name Count
AA 2
BB 3
AA 4

我有一个 IEnumerable<Category>
并希望获得具有唯一名称和多个条目的总和的类别列表

输出
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;}
}

我如何将两列相加。并且“电话”列可以是最后一行或任何一行

最佳答案

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) });

关于LINQ聚合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/694200/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com