给定以下列表:
var data = new[]
{
new {category = "Product", text = "aaaa"},
new {category = "Product", text = "bbbb"},
new {category = "Product", text = "bbbb"},
};
我如何按类别对其进行分组并返回一个具有类别的对象和不同文本的描述放在一起?
IE。想以 yp 结尾:
{
categroy="Product"
description = "aaaa,bbbb,cccc"
}
尝试了以下 GroupBy 和 Aggregate,但有些不对劲
data.GroupBy(x => x.category).Select(g => new
{
category = g.Key,
description = g.Aggregate((s1, s2) => s1 + "," + s2)
});
TIA