📅  最后修改于: 2023-12-03 15:39:48.459000             🧑  作者: Mango
在编程中,我们经常需要对集合或数据进行分组操作。C#中提供了多种方法可以对数据进行分组,其中之一便是按唯一值进行分组。
按唯一值进行分组是将相同唯一值的数据项放在一组中。例如,我们可以根据某个属性对一组对象进行分组,使得相同属性值的对象放在同一组中。在C#中,我们可以使用GroupBy()函数轻松地实现这一过程。
GroupBy()方法是IEnumerable
public static IEnumerable<IGrouping<TKey, TElement>> GroupBy<TSource, TKey, TElement>(
this IEnumerable<TSource> source,
Func<TSource, TKey> keySelector,
Func<TSource, TElement> elementSelector
)
其中,source代表需要分组的集合,keySelector代表分组键的选择器,elementSelector代表元素选择器。
举个例子,如果我们有以下一组学生记录:
List<Student> students = new List<Student>()
{
new Student() { Name = "A", Age = 20, Gender = Gender.Female },
new Student() { Name = "B", Age = 22, Gender = Gender.Male },
new Student() { Name = "C", Age = 20, Gender = Gender.Female },
new Student() { Name = "D", Age = 22, Gender = Gender.Male },
new Student() { Name = "E", Age = 21, Gender = Gender.Female },
};
我们可以按年龄进行分组:
var groups = students.GroupBy(s => s.Age);
这个分组操作将返回:
[
{ Key: 20, Values: [ { Name: "A", Age: 20, Gender: Female }, { Name: "C", Age: 20, Gender: Female } ] },
{ Key: 22, Values: [ { Name: "B", Age: 22, Gender: Male }, { Name: "D", Age: 22, Gender: Male } ] },
{ Key: 21, Values: [ { Name: "E", Age: 21, Gender: Female } ] }
]
如果我们只想将唯一值相同的元素放在同一组中,我们可以使用Distinct()方法来消除集合中的重复项,并将其传递给GroupBy()方法进行分组。
举个例子,如果我们有以下一组学生记录:
List<Student> students = new List<Student>()
{
new Student() { Name = "A", Age = 20, Gender = Gender.Female },
new Student() { Name = "B", Age = 22, Gender = Gender.Male },
new Student() { Name = "C", Age = 20, Gender = Gender.Female },
new Student() { Name = "D", Age = 22, Gender = Gender.Male },
new Student() { Name = "E", Age = 21, Gender = Gender.Female },
};
我们可以按年龄的唯一值进行分组:
var groups = students.Select(s => s.Age).Distinct().Select(age => students.Where(s => s.Age == age));
这个分组操作将返回:
[
[ { Name: "A", Age: 20, Gender: Female }, { Name: "C", Age: 20, Gender: Female } ],
[ { Name: "B", Age: 22, Gender: Male }, { Name: "D", Age: 22, Gender: Male } ],
[ { Name: "E", Age: 21, Gender: Female } ]
]
我们来看一个实际的例子。假设我们有以下一组商品记录:
List<Product> products = new List<Product>()
{
new Product() { Name = "A", Category = Category.Food, Price = 2.5m },
new Product() { Name = "B", Category = Category.Food, Price = 3m },
new Product() { Name = "C", Category = Category.Electronics, Price = 20m },
new Product() { Name = "D", Category = Category.Electronics, Price = 15m },
new Product() { Name = "E", Category = Category.Clothing, Price = 10m },
new Product() { Name = "F", Category = Category.Clothing, Price = 5m },
new Product() { Name = "G", Category = Category.Food, Price = 4m },
};
我们可以按唯一类别分组,并计算每个类别的总价值和平均价值:
var groups = products.Select(p => p.Category).Distinct().Select(category => new
{
Category = category,
Total = products.Where(p => p.Category == category).Sum(p => p.Price),
Average = products.Where(p => p.Category == category).Average(p => p.Price),
});
这个分组操作将返回:
[
{ Category: Food, Total: 9.5m, Average: 3.1666666666666666666666666667m },
{ Category: Electronics, Total: 35m, Average: 17.5m },
{ Category: Clothing, Total: 15m, Average: 7.5m }
]
按唯一值进行分组是一种灵活、可靠、高效的数据集操作方法。借助GroupBy()方法,我们能够轻松地将给定的数据集按指定的规则进行分组,从而更好地利用数据资源、方便数据分析和科学计算。www7030com视频我不卡tw任意门四虎www最新地址