轉載:https://blog.csdn.net/smartsmile2012/article/details/52883285
/*
以下圍繞Person類實現,Person類只有Name和Age兩個屬性
一.List<T>排序
1.1 List<T>提供了很多排序方法,sort(),Orderby(),OrderByDescending().
*/
lstPerson = lstPerson.OrderByDescending(x=>x.Name).ToList(); //降序
lstPerson = lstPerson.OrderBy(x => x.Age).ToList();//升序
//通過Name和Age升序
lstPerson.Sort((x, y) =>
{
if ((x.Name.CompareTo(y.Name) > 0) || ((x.Name == y.Name) && x.Age > y.Age))
{
return 1;
}
else if ((x.Name == y.Name) && (x.Age == y.Age))
{
return 0;
}
else
{
return -1;
}
});
/*
1.2 因爲最近有做datagrid裏面像實現點擊任何一列的名稱就按照該名稱排序,那我們該怎麼做呢?可能第一反應是想,爲每一個屬性寫一個排序方法不就得了,其實這樣的話無意間增加的代碼量了,而且不通用,其實這裏可以結合反射來實現.
*/
string propertityName = "Name";
lstPerson = lstPerson.OrderBy(x =>
{
PropertyInfo[] proInfos = x.GetType().GetProperties();
return proInfos.Where(info => info.Name == propertityName).ToList()[0].GetValue(x);
}).ToList();
/*
二.List<T>分頁
2.1往往有時候我們會從後臺獲取很多數據,存放在List<T>,可是因爲界面受限制無法完全展示,我們就會想到分頁顯示,對於分頁顯示我們基本上第一種想法肯定是通過循環設置每一頁的Size,
其實linq有skip和take方法,skip表示跳過多少元素,take獲取特定個數元素. 看起來代碼簡潔多了.
*/
public static List<Person> GetPageByLinq(List<Person> lstPerson, int pageIndex, int PageSize)
{
return lstPerson.Skip((pageIndex - 1) * PageSize).Take(PageSize).ToList();
}
/*
三,List<T>之foreach用法.
2.1 如何我相對List裏面的每個對象執行相同操作的話,以前都是通過for循環遍歷,其實Linq提供了便捷的Foreach來實現。下面我將對所有的Person年齡+2.
*/
lstPerson.ForEach(x => x.Age= x.Age + 2);
/*兩個集合之間操作*/
List<string> ListResult = new List<string>();
ListResult = ListA.Distinct().ToList();//去重
ListResult = ListA.Except(ListB).ToList();//差集
ListResult = ListA.Union(ListB).ToList(); //並集
ListResult = ListA.Intersect(ListB).ToList();//交集
//這裏有7個老師,每個人有3個學生,總共21一個學生裏,我們想要獲得這3個未及格的學生集合。
public class Student
{
public string StudentName { get; set; }
public int Score { get; set; }
public Student(string StudentName,int Score)
{
this.StudentName = StudentName;
this.Score = Score;
}
}
public class Teacher
{
public string TeacherName { get; set; }
public List<Student> Students { get; set; }
public Teacher(string TeacherName, List<Student> Students)
{
this.TeacherName = TeacherName;
this.Students = Students;
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace TestLinq
{
class Program
{
static void Main(string[] args)
{
//運行結果見下圖
List<Teacher> teachers = new List<Teacher>
{
new Teacher("張老師",new List<Student>{ new Student("張三1", 100),new Student("李四1", 90),new Student("王五1", 30) }), //
new Teacher("李老師",new List<Student>{ new Student("張三2", 100),new Student("李四2", 90),new Student("王五2", 60) }),
new Teacher("趙老師",new List<Student>{ new Student("張三3", 100),new Student("李四3", 90),new Student("王五3", 40) }), //
new Teacher("孫老師",new List<Student>{ new Student("張三4", 100),new Student("李四4", 90),new Student("王五4", 60) }),
new Teacher("錢老師",new List<Student>{ new Student("張三5", 100),new Student("李四5", 90),new Student("王五5", 50) }), //
new Teacher("周老師",new List<Student>{ new Student("張三6", 100),new Student("李四6", 90),new Student("王五6", 60) }),
new Teacher("吳老師",new List<Student>{ new Student("張三7", 100),new Student("李四7", 90),new Student("王五7", 60) })
};
#region 所有任課老師下未及格的學生 方式一
List<Student> studentList = new List<Student>();
foreach (var t in teachers)
{
foreach (var s in t.Students)
{
if (s.Score < 60)
{
studentList.Add(s);
}
}
}
studentList.ForEach(s => Console.WriteLine(string.Format("{0} - {1}", s.StudentName, s.Score)));
#endregion
Console.ReadKey();
#region 所有任課老師下未及格的學生 方式二
var list1 = from t in teachers
from s in t.Students
where s.Score < 60
select s;
foreach (var item in list1)
{
Console.WriteLine(string.Format("{0} - {1}", item.StudentName, item.Score));
}
#endregion
Console.ReadKey();
#region 所有任課老師下未及格的學生 方式三
var list2 = teachers.SelectMany(t => t.Students).Where(s => s.Score < 60);
foreach (var s in list2)
{
Console.WriteLine(string.Format("{0} - {1}", s.StudentName, s.Score));
}
#endregion
Console.ReadKey();
#region 所有未及格的學生及其授課老師
var list3 = teachers.SelectMany(
t => t.Students,
(t, s) => new { t.TeacherName, s.StudentName, s.Score })
.Where(n => n.Score < 60);
foreach (var item in list3)
{
Console.WriteLine(string.Format("任課老師{0} - 學生{1} 分數{2}", item.TeacherName, item.StudentName, item.Score));
}
#endregion
Console.ReadKey();
}
}
}