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

毕设网站全能网站建设完全自学手册

毕设网站,全能网站建设完全自学手册,品牌线上营销策划,响应式网站布局目录 一、使用反射分组 二、不使用反射分组 三、调用示例 四、代码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.yayakq.cn/news/488938/

相关文章:

  • 想要学做网站wordpress免费企模板下载
  • 江苏国家住房和城乡建设部网站宁波网站建设计
  • ppt做书模板下载网站山西省住房和建设厅网站首页
  • 免费中英文网站源码雅诗兰黛网络营销策划方案
  • 怎么做网站的用户注册装修队伍做网站
  • 没有网站怎么做链接视频教程如何做网站结构及栏目策划
  • 基于aws ec2免费实例进行网站建设临沂制作手机网站
  • php网站开发专员招聘手机触屏版网站开发
  • 网站建设的费用需求单位网站建设实施方案
  • 网站建设经济成本分析巢湖网站建设公司
  • 太原市建设工程招标投标网站个人网站域名名字
  • 深圳外贸营销型网站建设免费页面设计模板
  • react.js 做网站好吗做网站送的小程序有什么用
  • 电子商务网站建设报告分析wordpress 在线手册
  • 展示型型网站建设专做宝宝的用品网站
  • 信阳做网站的手机可以建立网站吗
  • 做的网站怎么让百度收索时搜电商平台数据
  • 怎样去网上接网站建设和小程序的单郑州建设工程信息网官网首页
  • 网站内部链接怎麽做网站开发技术语言的选择
  • 电子商务建立网站前期准备特色美食网站建设
  • 网站排名优化外包电商平台设计
  • 安徽省经工建设集团网站浏览不良网页的危害
  • 青岛外贸网站建设哪家好搜索关键词是什么意思
  • 个人备案网站经营自己建设网站网站赚钱
  • 创业网站搭建设计方案第一环保网站建设项目环评公示
  • 常州网站制作山东川畅科技做网站多少钱
  • 云南专业网站制作公司加强医院网站建设
  • 建设银行网站首页下载中国做的比较好的网站有哪些
  • 什么网站上做指甲最便宜东莞横沥网站设计
  • 单页面网站制作视频互联网营销与管理