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

b2c网站建设公司广告制作公司

b2c网站建设公司,广告制作公司,深圳湾在深圳哪个区,xp做网站服务器吗目录 一、使用反射分组 二、不使用反射分组 三、调用示例 四、代码demo 一、使用反射分组 private static List<GroupList<T>> GetGroupList<T>(List<T> entities, string groupByProperty) {// 获取分组字段的类型var propertyInfo typeof(T).…

目录

一、使用反射分组

二、不使用反射分组

三、调用示例

四、代码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.wangmingla.cn/news/61821.html

相关文章:

  • 设计网站需要多少钱百度全网营销
  • 申请自己邮箱域名seo是什么平台
  • 从seo角度谈网站建设长沙靠谱的关键词优化
  • 东莞高端建站公司seo软件全套
  • asp做的网站数据库在哪里嵌入式培训班一般多少钱
  • 上海品牌设计有限公司关键词seo优化软件
  • 南京教育网站开发建站平台哪个好
  • 电子商务网站开发方式搜索引擎优化的简称是
  • 濮阳市网站建设百度手机端排名如何优化
  • 做门窗接活的网站日本粉色iphone
  • 免费一键生成logo网站长春网站建设方案咨询
  • 广东深圳招聘信息最新招聘2022推广优化关键词
  • 东莞网站建设网络公司公司经典软文案例分析
  • 网站建设 语言唐山seo优化
  • 国内网站不备案可以吗seo整合营销
  • 做棋牌网站建设哪家便宜seo如何快速排名百度首页
  • 生成短链接的网站百度手机助手网页版
  • 做响应式网站代码免费建站系统哪个好用吗
  • 网站建设网页开发seo优化专员招聘
  • 基于web的个人网站设计与实现万州网站建设
  • 绿色网站设计网络快速推广渠道
  • php做网站需要什么技术营销方法有哪几种
  • wordpress整体加速惠州seo外包费用
  • 设计网站哪个唐山百度seo公司
  • 做网站需注意事项怎么建一个自己的网站
  • 简述网站建设及维护的全过程最常用的搜索引擎有哪些
  • wordpress改版权百度首页排名优化服务
  • 网站视频与服务器的关系网站排名工具
  • 香港做网站软件排名优化
  • 温州做网站就来温州易富网络青岛网络科技公司排名