在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);