当前位置: 首页 > news >正文

C#基础:数据库中使用Linq作分组处理(反射/直接分组)

目录

一、使用反射分组

二、不使用反射分组

三、调用示例

四、代码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;}}
}

http://www.lryc.cn/news/428152.html

相关文章:

  • Revite二次开发_使用WPF和WebView2制作一个访问网站的窗口
  • Java Spring Boot 连接数据库
  • Java面试八股之消息队列中推模式和拉模式分别有哪些使用场景
  • springboot jar是如何启动的
  • Android 12系统源码_屏幕设备(二)DisplayAdapter和DisplayDevice的创建
  • 常用Mysql命令
  • IDEA Debug工具
  • ARM64的汇编资源
  • 实验室安全分级分类管理系统在高校中的具体应用
  • 使用 prerenderRoutes 进行预渲染路由
  • 【深度解析】WRF-LES与PALM微尺度气象大涡模拟
  • redis事件机制
  • 【C++】模拟实现vector
  • 【CAN-IDPS】汽车网关信息安全要求以及实验方法
  • EASE-Grid是啥东西?
  • 前端用户管理模块方法及api分析
  • microsoft edge怎么关闭安全搜索
  • Qt | QSQLite内存数据库增删改查
  • 【论文阅读】SegNeXt:重新思考卷积注意力设计
  • 【C++】String类:标准库介绍
  • MS523非接触式读卡器 IC
  • 仓颉编程语言入门 -- Socket 编程与HTTP 编程概述
  • Oracle基本SQL操作-用户角色权限管理
  • Qt-信号和槽(8)
  • 80.游戏的分辨率修改思路与分析
  • MaxKB(二):Ubuntu24.04搭建maxkb开发环境
  • c#实现数据导出为PDF的方式
  • 【联想电脑】:使用拓展坞后转接HDMI,无法识别显示屏
  • Verilog刷题笔记53
  • GoFly快速开发后台框架-后端接口请求返回403提示码就跨域问题/请求端域名拦截问题