基于php+mysql的网站开发,seo外推软件,如何在百度打广告,公司网站开发视频目录
一、使用反射分组
二、不使用反射分组
三、调用示例
四、代码demo
一、使用反射分组
private static ListGroupListT GetGroupListT(ListT entities, string groupByProperty)
{// 获取分组字段的类型var propertyInfo typeof(T).…目录
一、使用反射分组
二、不使用反射分组
三、调用示例
四、代码demo
一、使用反射分组
private static ListGroupListT GetGroupListT(ListT 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));// 创建分组列表ListGroupListT groupLists new ListGroupListT();foreach (var group in groupedEntities){GroupListT groupList new GroupListT{GroupKey group.Key.ToString(), // 使用分组键作为GroupKeyList group.ToList(), // 分组数据//Count group.Count() //每组数据条数};groupLists.Add(groupList);}return groupLists;
}
二、不使用反射分组
private static ListGroupListStudent GetGroupListSimple(ListStudent entities)
{// 根据班级分组var groupedStudents entities.GroupBy(s s.ClassNumber);// 创建分组列表ListGroupListStudent groupLists new ListGroupListStudent();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 GroupListT{public string GroupKey { get; set; }public int Count { get List.Count; }public ListT List { get; set; } new ListT();}public class GroupListStudent{public string GroupKey { get; set; }public int Count { get List.Count; }public ListStudent List { get; set; } new ListStudent();}// 主程序class Program{static void Main(string[] args){// 创建1班的学生ListStudent Students new ListStudent{new Student(学生1-1, 1),new Student(学生1-2, 1)};// 创建2班的学生ListStudent class2Students new ListStudent{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 ListGroupListT GetGroupListT(ListT 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));// 创建分组列表ListGroupListT groupLists new ListGroupListT();foreach (var group in groupedEntities){GroupListT groupList new GroupListT{GroupKey group.Key.ToString(), // 使用分组键作为GroupKeyList group.ToList(), // 分组数据//Count group.Count() //每组数据条数};groupLists.Add(groupList);}return groupLists;}private static ListGroupListStudent GetGroupListSimple(ListStudent entities){// 根据班级分组var groupedStudents entities.GroupBy(s s.ClassNumber);// 创建分组列表ListGroupListStudent groupLists new ListGroupListStudent();foreach (var group in groupedStudents){GroupListStudent groupList new GroupListStudent{GroupKey group.Key.ToString(),List group.ToList(),// Count group.Count()};groupLists.Add(groupList);}return groupLists;}}
}