一些c# groupby的分組轉(zhuǎn)換操作技巧包括:
var groupedData = data.GroupBy(x => x.Category)
.Select(g => new
{
Category = g.Key,
Count = g.Count(),
TotalValue = g.Sum(x => x.Value)
});
var transformedData = data.GroupBy(x => x.Category)
.SelectMany(g => g.Select(x => new
{
Category = g.Key,
Value = x.Value * 2
}));
var groupedDictionary = data.GroupBy(x => x.Category)
.ToDictionary(g => g.Key, g => g.ToList());
var aggregatedData = data.GroupBy(x => x.Category)
.Select(g => new
{
Category = g.Key,
TotalValue = g.Aggregate(0.0, (acc, x) => acc + x.Value)
});