目录
一、使用反射分组
二、不使用反射分组
三、调用示例
四、代码demo
一、使用反射分组
private static List<GroupList<T>> GetGroupList<T>(List<T> entities, string groupByProperty)
{// 获取分组字段的类型var propertyInfo = typeof(T).GetProperty(groupByProperty);if (propertyInfo == null){throw new ArgumentException($"类型 {typeof(T).Name} 不包含名为 {groupByProperty} 的属性.");}// 按指定属性分组var groupedEntities = entities.GroupBy(e => propertyInfo.GetValue(e, null));// 创建分组列表List<GroupList<T>> groupLists = new List<GroupList<T>>();foreach (var group in groupedEntities){GroupList<T> groupList = new GroupList<T>{GroupKey = group.Key.ToString(), // 使用分组键作为GroupKeyList = group.ToList(), // 分组数据//Count = group.Count() //每组数据条数};groupLists.Add(groupList);}return groupLists;
}
二、不使用反射分组
private static List<GroupListStudent> GetGroupListSimple(List<Student> entities)
{// 根据班级分组var groupedStudents = entities.GroupBy(s => s.ClassNumber);// 创建分组列表List<GroupListStudent> groupLists = new List<GroupListStudent>();foreach (var group in groupedStudents){GroupListStudent groupList = new GroupListStudent{GroupKey = group.Key.ToString(),List = group.ToList(),// Count = group.Count()};groupLists.Add(groupList);}return groupLists;
}
三、调用示例
//反射获取分组
var result = GetGroupList(Students, "ClassNumber");
//直接获取分组
var result2 = GetGroupListSimple(Students);
四、代码demo
using System;
using System.Collections.Generic;
using System.Linq;namespace StudentClassExample
{// 学生类public class Student{public string Name { get; set; }public int ClassNumber { get; set; }public Student(string name, int classNumber){Name = name;ClassNumber = classNumber;}}public class GroupList<T>{public string GroupKey { get; set; }public int Count { get => List.Count; }public List<T> List { get; set; } = new List<T>();}public class GroupListStudent{public string GroupKey { get; set; }public int Count { get => List.Count; }public List<Student> List { get; set; } = new List<Student>();}// 主程序class Program{static void Main(string[] args){// 创建1班的学生List<Student> Students = new List<Student>{new Student("学生1-1", 1),new Student("学生1-2", 1)};// 创建2班的学生List<Student> class2Students = new List<Student>{new Student("学生2-1", 2),new Student("学生2-2", 2),new Student("学生2-3", 2)};Students.AddRange(class2Students);//反射获取分组var result = GetGroupList(Students, "ClassNumber");//直接获取分组var result2 = GetGroupListSimple(Students);;}private static List<GroupList<T>> GetGroupList<T>(List<T> entities, string groupByProperty){// 获取分组字段的类型var propertyInfo = typeof(T).GetProperty(groupByProperty);if (propertyInfo == null){throw new ArgumentException($"类型 {typeof(T).Name} 不包含名为 {groupByProperty} 的属性.");}// 按指定属性分组var groupedEntities = entities.GroupBy(e => propertyInfo.GetValue(e, null));// 创建分组列表List<GroupList<T>> groupLists = new List<GroupList<T>>();foreach (var group in groupedEntities){GroupList<T> groupList = new GroupList<T>{GroupKey = group.Key.ToString(), // 使用分组键作为GroupKeyList = group.ToList(), // 分组数据//Count = group.Count() //每组数据条数};groupLists.Add(groupList);}return groupLists;}private static List<GroupListStudent> GetGroupListSimple(List<Student> entities){// 根据班级分组var groupedStudents = entities.GroupBy(s => s.ClassNumber);// 创建分组列表List<GroupListStudent> groupLists = new List<GroupListStudent>();foreach (var group in groupedStudents){GroupListStudent groupList = new GroupListStudent{GroupKey = group.Key.ToString(),List = group.ToList(),// Count = group.Count()};groupLists.Add(groupList);}return groupLists;}}
}