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 }
?