LINQ基础篇(下)
分页 Skip Take
var query = DataSource.Skip((PageNo-1)*PageSize).Take(PageSize).ToList();
Skip 跳过多少个元素
Take取出多少个元素
SkipWhile 逐一判断直到不满足条件,将该元素及其后的元素取出
TakeWhile 逐一判断取出满足条件的元素
Except
语法 a.Except(b)
作用 a中去除b中元素后剩余部分
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 List<string> strListA = new List<string> { "Hello","World","C#","JavaScript","Animal"}; 2 List<string> strListB = new List<string> { "Hello", "Person", "History", "Furion", "Animal" }; 3 var exceptValue = strListA.Except(strListB); 4 foreach (var item in exceptValue) 5 { 6 Console.WriteLine(item); 7 } 8 Console.ReadKey();
Concat
语法 a.Concat(b)
作用 合并a和b中的元素,不去除重复
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 List<string> strListA = new List<string> { "Hello","World","C#","JavaScript","Animal"}; 2 List<string> strListB = new List<string> { "Hello", "Person", "History", "Furion", "Animal" }; 3 var concatValue = strListA.Concat(strListB); 4 foreach (var item in concatValue) 5 { 6 Console.WriteLine(item); 7 } 8 Console.ReadKey();
Union
语法 a.Union(b)
作用 合并a和b中的元素,去除重复
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 List<string> strListA = new List<string> { "Hello","World","C#","JavaScript","Animal"}; 2 List<string> strListB = new List<string> { "Hello", "Person", "History", "Furion", "Animal" }; 3 var unionValue = strListA.Concat(strListB); 4 foreach (var item in unionValue) 5 { 6 Console.WriteLine(item); 7 } 8 Console.ReadKey();
Intersect
语法 a.Intersect(b)
作用 求a和b的交集
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 List<string> strListA = new List<string> { "Hello","World","C#","JavaScript","Animal"}; 2 List<string> strListB = new List<string> { "Hello", "Person", "History", "Furion", "Animal" }; 3 var intersectValue = strListA.Intersect(strListB); 4 foreach (var item in intersectValue) 5 { 6 Console.WriteLine(item); 7 } 8 Console.ReadKey();
Contains
语法 a.Contains(b)
作用 类似 like ‘%b%’
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 List<string> strListA = new List<string> { "Hello","World","C#","JavaScript","Animal"}; 2 List<string> strListB = new List<string> { "Hello", "Person", "History", "Furion", "Animal" }; 3 var containsValue = from s in strListA where s.Contains("l") select s; 4 foreach (var item in containsValue) 5 { 6 Console.WriteLine(item); 7 } 8 Console.ReadKey();