gpt4 book ai didi

c# - 使用 LINQ 对项目进行分组和组合

转载 作者:太空宇宙 更新时间:2023-11-03 20:49:53 25 4
gpt4 key购买 nike

public class Product
{
public string Code { get; set; }
public string Name { get; set; }
public string Amount { get; set; }
}

public List<Product> Products = new List<Product>()
{
new Product() { Code="A1", Name="Vacuum", Amount = "10" },
new Product() { Code="A2", Name="Iron", Amount = "20" },
new Product() { Code="A3", Name="Kettle", Amount = "13" },
new Product() { Code="A2", Name="Microwave", Amount = "11" },
new Product() { Code="A3", Name="Dryer", Amount = "3" }
};

我需要选择没有重复代码的所有产品。具有相同代码的产品应合并为一行,在这种情况下,名称和数量应以逗号分隔。如何修改下面的代码,让它更优雅

    var list1 = new List<Product>();
var gl = Products.GroupBy(x => x.Code).Where(x => x.Count() > 1);
gl.ToList().ForEach(x => list1.AddRange(x));

var list2 = Products.Where(x => !list1.Contains(x)).ToList(); // uniq values

var list3 = gl.Select(x =>
{
var p = new Product() { Code = x.Key };
p.Name = string.Join(",", x.ToList().Select(r => r.Name).Distinct());
p.Amount = string.Join(",", x.ToList().Select(r => r.Amount).Distinct());
return p;
}).ToList();

list2.AddRange(list3);
list2.ForEach(x =>
{
Console.WriteLine($"{x.Code.PadRight(20)},{x.Name.PadRight(20)},{x.Amount.PadRight(20)}");
});

结果应该是:

Code  Name              Amount
A1 Vacuum 10
A2 Iron, Microwave 20, 11
A3 Kettle, Dryer 13, 3

最佳答案

Code 上使用 GroupBy,然后对其进行迭代以使用 string.Join() 组合各个元素。

var results = products
.GroupBy
(
p => p.Code
)
.Select
(
g => new
{
Code = g.Key,
Name = string.Join
(
",",
g.Select( p => p.Name )
),
Amount = string.Join
(
",",
g.Select( p => p.Amount.ToString() )
)
}
);

输出:

A1 Vacuum 10
A2 Iron,Microwave 20,11
A3 Kettle,Dryer 13,3

Link to working example on DotNetFiddle

关于c# - 使用 LINQ 对项目进行分组和组合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56352189/

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