在C#中使用GroupBy进行分组统计操作时,可以结合LINQ查询语句来实现各种统计需求。以下是一些常见的操作技巧:
var groupedData = data.GroupBy(x => x.Category)
.Select(g => new { Category = g.Key, Count = g.Count() });
var groupedData = data.GroupBy(x => x.Category)
.Select(g => new { Category = g.Key, Sum = g.Sum(x => x.Value) });
var groupedData = data.GroupBy(x => x.Category)
.Select(g => new { Category = g.Key, Average = g.Average(x => x.Value) });
var groupedData = data.GroupBy(x => x.Category)
.Select(g => new { Category = g.Key, MaxValue = g.Max(x => x.Value), MinValue = g.Min(x => x.Value) });
var groupedData = data.GroupBy(x => x.Category)
.Select(g => new {
Category = g.Key,
Count = g.Count(),
Sum = g.Sum(x => x.Value),
Average = g.Average(x => x.Value),
MaxValue = g.Max(x => x.Value),
MinValue = g.Min(x => x.Value)
});
通过以上技巧,可以灵活地对分组后的数据进行各种统计操作,满足不同的业务需求。