在C#中使用GroupBy對(duì)結(jié)果集進(jìn)行分組后,可以使用以下幾種技巧來展示結(jié)果集:
var groupedResult = yourCollection.GroupBy(item => item.GroupingKey);
foreach(var group in groupedResult)
{
Console.WriteLine($"Key: {group.Key}");
foreach(var item in group)
{
Console.WriteLine($"Item: {item}");
}
}
var groupedResult = from item in yourCollection
group item by item.GroupingKey into groupedItems
select new
{
Key = groupedItems.Key,
Items = groupedItems
};
foreach(var group in groupedResult)
{
Console.WriteLine($"Key: {group.Key}");
foreach(var item in group.Items)
{
Console.WriteLine($"Item: {item}");
}
}
var groupedResult = yourCollection.GroupBy(item => item.GroupingKey)
.ToDictionary(group => group.Key, group => group.ToList());
foreach(var key in groupedResult.Keys)
{
Console.WriteLine($"Key: {key}");
foreach(var item in groupedResult[key])
{
Console.WriteLine($"Item: {item}");
}
}
這些技巧可以根據(jù)具體需求來選擇使用,展示分組結(jié)果集時(shí)可以根據(jù)情況選擇最合適的方法。