【发布时间】:2022-12-07 16:46:12
【问题描述】:
string flowerList = string.Empty;
foreach (var flower in Plants.Where(x => x.Status == PlantStatus.Active))
{
flowerList = string.IsNullOrWhiteSpace(flowerList)
? "<li>" + flower.Colour + " " + flower.Priority + " " + flower.Category + "</li>"
: flowerList + "<li>" + flower.Colour + " " + flower.Priority + " " + flower.Category+ "</li>" ;
}
我有上面的代码在 html 页面中显示 C# 列表数据。我如何将 flower.Category 输出的 flowerList html 分组。
对于每个类别,我希望将 flower.Category 作为组标题,然后在其下列出相关记录。
【问题讨论】:
-
您已经在使用 LINQ。你试过
GroupBy操作了吗?不要将查询放在foreach子句中。已经很难读了。