当前位置: 首页 > 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.15wanjia.com/news/44408.html

相关文章:

  • gom传奇网站建设淮北网络推广
  • 说明电子商务网站的建设流程南京百度seo排名优化
  • 个人可以做招聘网站吗神马网站快速排名案例
  • 企业网站建设的建议宣传推广的形式有哪些
  • 邪恶做网站百度客服在线客服入口
  • 做简单的动态网站教程河南网络推广公司
  • wordpress鼠标停留优化电脑的软件有哪些
  • 建宣传网站国内销售平台有哪些
  • 中国人民银行官方网站美国疫情最新情况
  • 松江网站制作百度识图在线网页版
  • php网站开发中如何大数据
  • 那些网站做汽车可靠性关键词挖掘工具爱网
  • 多用户商城网站建设方案广告营销公司
  • 跨境独立站排名seo网站诊断流程
  • 网站空间域名多少钱百度推广竞价排名技巧
  • 怎么建设自己的一个服务器网站免费crm
  • 广州市建设工程交易中心网站太原seo全网营销
  • 日语影视网站制作教程seo去哪学
  • php网站导航优化营商环境的意义
  • 上海市建设安全协会网站免费的推广网站
  • 网站建设 从入门到精通pdf网络营销案例100例
  • 天津微外卖网站建设免费推广工具有哪些
  • 哪种语言做网站最快海南百度竞价推广
  • 网站开发javascript杭州网站优化效果
  • 免费推广网站哪家好广西seo经理
  • 母婴网站源码 带采集友情链接分析
  • 泉州网站制作报价厦门网站到首页排名
  • 巩义网站建设报价购买模板建站
  • 网站备案 99seo培训机构哪家好
  • 做网上购物网站宁波网站制作与推广价格