Linq常用List操作总结,ForEach、分页、交并集、去重、SelectMany等

  1 /*
  2 以下围绕Person类实现,Person类只有Name和Age两个属性
  3 一.List<T>排序
  4 1.1 List<T>提供了很多排序方法,sort(),Orderby(),OrderByDescending().
  5 */
  6  
  7 lstPerson = lstPerson.OrderByDescending(x=>x.Name).ToList(); //降序
  8 lstPerson = lstPerson.OrderBy(x => x.Age).ToList();//升序
  9  
 10 //通过Name和Age升序
 11 lstPerson.Sort((x, y) =>
 12             {
 13                 if ((x.Name.CompareTo(y.Name) > 0) || ((x.Name == y.Name) && x.Age > y.Age))
 14                 {
 15                     return 1;
 16                 }
 17                 else if ((x.Name == y.Name) && (x.Age == y.Age))
 18                 {
 19                     return 0;
 20                 }
 21                 else
 22                 {
 23                     return -1;
 24                 }
 25             });
 26  
 27 /*
 28 1.2 因为最近有做datagrid里面像实现点击任何一列的名称就按照该名称排序,那我们该怎么做呢?可能第一反应是想,为每一个属性写一个排序方法不就得了,其实这样的话无意间增加的代码量了,而且不通用,其实这里可以结合反射来实现.
 29 */
 30  
 31 string propertityName = "Name";
 32 lstPerson = lstPerson.OrderBy(x =>
 33             {
 34                 PropertyInfo[] proInfos = x.GetType().GetProperties();
 35                 return proInfos.Where(info => info.Name == propertityName).ToList()[0].GetValue(x);
 36             }).ToList();
 37  
 38 /*
 39 二.List<T>分页
 40 2.1往往有时候我们会从后台获取很多数据,存放在List<T>,可是因为界面受限制无法完全展示,我们就会想到分页显示,对于分页显示我们基本上第一种想法肯定是通过循环设置每一页的Size,
 41 其实linq有skip和take方法,skip表示跳过多少元素,take获取特定个数元素. 看起来代码简洁多了.
 42 */
 43  
 44 public static List<Person> GetPageByLinq(List<Person> lstPerson, int pageIndex, int PageSize)
 45 {
 46     return lstPerson.Skip((pageIndex - 1) * PageSize).Take(PageSize).ToList();
 47 }
 48  
 49 /*
 50 三,List<T>之foreach用法.
 51 2.1 如何我相对List里面的每个对象执行相同操作的话,以前都是通过for循环遍历,其实Linq提供了便捷的Foreach来实现。下面我将对所有的Person年龄+2.
 52 */
 53  
 54 lstPerson.ForEach(x => x.Age= x.Age + 2);
 55  
 56 /*两个集合之间操作*/
 57 List<string> ListResult = new List<string>();
 58 ListResult = ListA.Distinct().ToList();//去重
 59 ListResult = ListA.Except(ListB).ToList();//差集
 60 ListResult = ListA.Union(ListB).ToList();  //并集
 61 ListResult = ListA.Intersect(ListB).ToList();//交集
 62  
 63 //这里有7个老师,每个人有3个学生,总共21一个学生里,我们想要获得这3个未及格的学生集合。
 64 public class Student
 65 {
 66     public string StudentName { get; set; }
 67     public int Score { get; set; }
 68  
 69     public Student(string StudentName,int Score)
 70     {
 71         this.StudentName = StudentName;
 72         this.Score = Score;
 73     }
 74 }
 75 public class Teacher
 76 {
 77     public string TeacherName { get; set; }
 78     public List<Student> Students { get; set; }
 79     public Teacher(string TeacherName, List<Student> Students)
 80     {
 81         this.TeacherName = TeacherName;
 82         this.Students = Students;
 83     }
 84 }
 85  
 86 using System;
 87 using System.Collections.Generic;
 88 using System.Linq;
 89 using System.Text;
 90  
 91 namespace TestLinq
 92 {
 93     class Program
 94     {
 95         static void Main(string[] args)
 96         {
 97             //运行结果见下图
 98             List<Teacher> teachers = new List<Teacher>
 99             {
100                 new Teacher("张老师",new List<Student>{ new Student("张三1", 100),new Student("李四1", 90),new Student("王五1", 30) }), //
101                 new Teacher("李老师",new List<Student>{ new Student("张三2", 100),new Student("李四2", 90),new Student("王五2", 60) }),
102                 new Teacher("赵老师",new List<Student>{ new Student("张三3", 100),new Student("李四3", 90),new Student("王五3", 40) }), //
103                 new Teacher("孙老师",new List<Student>{ new Student("张三4", 100),new Student("李四4", 90),new Student("王五4", 60) }),
104                 new Teacher("钱老师",new List<Student>{ new Student("张三5", 100),new Student("李四5", 90),new Student("王五5", 50) }), //
105                 new Teacher("周老师",new List<Student>{ new Student("张三6", 100),new Student("李四6", 90),new Student("王五6", 60) }),
106                 new Teacher("吴老师",new List<Student>{ new Student("张三7", 100),new Student("李四7", 90),new Student("王五7", 60) })
107             };
108  
109             #region 所有任课老师下未及格的学生 方式一
110             List<Student> studentList = new List<Student>();
111             foreach (var t in teachers)
112             {
113                 foreach (var s in t.Students)
114                 {
115                     if (s.Score < 60)
116                     {
117                         studentList.Add(s);
118                     }
119                 }
120             }
121             studentList.ForEach(s => Console.WriteLine(string.Format("{0} - {1}", s.StudentName, s.Score)));
122             #endregion
123  
124             Console.ReadKey();
125  
126             #region 所有任课老师下未及格的学生 方式二
127             var list1 = from t in teachers
128                         from s in t.Students
129                         where s.Score < 60
130                         select s;
131             foreach (var item in list1)
132             {
133                 Console.WriteLine(string.Format("{0} - {1}", item.StudentName, item.Score));
134             }
135             #endregion
136  
137             Console.ReadKey();
138  
139             #region 所有任课老师下未及格的学生 方式三
140             var list2 = teachers.SelectMany(t => t.Students).Where(s => s.Score < 60);
141  
142             foreach (var s in list2)
143             {
144                 Console.WriteLine(string.Format("{0} - {1}", s.StudentName, s.Score));
145             }
146             #endregion
147  
148             Console.ReadKey();
149  
150             #region 所有未及格的学生及其授课老师 
151             var list3 = teachers.SelectMany(
152                 t => t.Students,
153                 (t, s) => new { t.TeacherName, s.StudentName, s.Score })
154                 .Where(n => n.Score < 60);
155  
156             foreach (var item in list3)
157             {
158                 Console.WriteLine(string.Format("任课老师{0} - 学生{1} 分数{2}", item.TeacherName, item.StudentName, item.Score));
159             }
160             #endregion
161             Console.ReadKey();
162         }
163     }
164 }

 

posted @ 2018-11-14 14:39  jasonlai2016  阅读(1436)  评论(0编辑  收藏  举报