linq——常用方法
take 前几
skip 跳过前几
takeWhile
var firstNumbersLessThan6 = numbers.TakeWhile(n => n < 6); //从前面查询直到满足条件停止
SkipWhile
var allButFirst3Numbers = numbers.SkipWhile(n => n % 3 != 0); //查找到满足条件开始
Reverse 翻转,颠倒
Distinct 消除重复
int[] factorsOf300 = { 2, 2, 3, 5, 5 };
var uniqueFactors = factorsOf300.Distinct(); 2,3,5
Union //求并集
1 int[] numbersA = { 0, 2, 4, 5, 6, 8, 9 }; 2 int[] numbersB = { 1, 3, 5, 7, 8 }; 3 4 var uniqueNumbers = numbersA.Union(numbersB); 5 6 uniqueNumbers =0, 2, 4, 5, 6, 8, 9,1,3,7 7 //A里面消除B里面的重复 求并集
Intersect //求交集
int[] numbersA = { 0, 2, 4, 5, 6, 8, 9 }; int[] numbersB = { 1, 3, 5, 7, 8 }; var uniqueNumbers = numbersA.Intersect(numbersB); uniqueNumbers =5,8 //A里面消除B里面的重复 求交集
Except //排除相同
int[] numbersA = { 0, 2, 4, 5, 6, 8, 9 }; int[] numbersB = { 1, 3, 5, 7, 8 }; IEnumerable<int> aOnlyNumbers = numbersA.Except(numbersB); aOnlyNumbers=0,2,4,6,9
First //第一条数据
ElementAt(int n) //根据索引获取元素
Any
All
EqualAll //全部等于
具体实践参考:https://code.msdn.microsoft.com/101-LINQ-Samples-3fb9811b