在C#中,可以使用LINQ的GroupBy方法对集合进行分组操作。有以下几种使用方法:
var groupedData = collection.GroupBy(x => x.Property);
var groupedData = collection.GroupBy(x => new { x.Property1, x.Property2 });
var groupedData = collection.GroupBy(x => x.Property % 2 == 0);
var groupedData = collection.GroupBy(x => x.Property)
.Select(group => new { Key = group.Key, Count = group.Count() });
var groupedData = collection.Where(x => x.Property > 0)
.GroupBy(x => x.Property);