目录
一、使用反射分组
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(), // 使用分组键作为GroupKey
List = 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(), // 使用分组键作为GroupKey
List = 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;
}
}
}
标签:group,C#,List,Linq,分组,var,new,public
From: https://blog.csdn.net/m0_67412019/article/details/141332828