非延時標準查詢操做符是指不具有延時查詢特性的標準查詢操做符,這些操做符通常用於輔助延時標準查詢操做符使用。javascript
一、ToArray操做符html
ToArray操做符用於將一個輸入序列轉換成一個數組。java
方法原型:git
public static TSource[] ToArray<TSource>(this IEnumerable<TSource> source);
代碼示例:數組
static void Main(string[] args) { List<int> listInt = new List<int>(); listInt.Add(1); listInt.Add(2); listInt.Add(3); int[] intArr = listInt.AsEnumerable().ToArray(); //將集合轉換爲數組 //若是不AsEnumerable()則調用的就是List<T>類的ToArray()方法,效果同樣 foreach (int i in intArr) { Console.WriteLine(i); //輸出1 2 3 } Console.ReadKey(); }
二、ToList操做符框架
ToList操做符用於將一個輸入序列轉換成一個List<T>對象。post
來看方法原型:學習
public static List<TSource> ToList<TSource>(this IEnumerable<TSource> source);
注意到返回的List<T>,任意IEnumerable<T>轉換爲List<T>this
代碼示例:spa
static void Main(string[] args) { int[] ints = new int[5] { 1, 2, 3, 4, 5 }; List<int> ListInt = ints.ToList(); foreach (int i in ints) { Console.WriteLine(i); //輸出 1 2 3 4 5 } Console.ReadKey(); }
三、ToDictionary操做符
ToDictionary操做符用於將一個輸入序列轉換爲一個Dictionary<K,T>集合對象。
看方法原型:
public static Dictionary<TKey, TSource> ToDictionary<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector); [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")] public static Dictionary<TKey, TElement> ToDictionary<TSource, TKey, TElement>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector); public static Dictionary<TKey, TSource> ToDictionary<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, IEqualityComparer<TKey> comparer); public static Dictionary<TKey, TElement> ToDictionary<TSource, TKey, TElement>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector, IEqualityComparer<TKey> comparer);
代碼示例:
static void Main(string[] args) { People p1 = new People(1,"張飛",20); People p2 = new People(1,"關羽",21); People p3 = new People(1,"劉備",22); People[] PeoArr = new People[3] { p1, p2, p3 }; Dictionary<string,People> DicPeo = PeoArr.ToDictionary(p => p.Name); //指定Key是Name屬性 Console.WriteLine(DicPeo["張飛"].Id + DicPeo["張飛"].Name + DicPeo["張飛"].Age); //輸出 1 張飛 20 Console.ReadKey(); }
四、ToLookup操做符
ToLookup操做符能夠將一個輸入序列轉換成一個ILookup<K,T>集合對象。ILookup與IDictionary<K,T>很是類似,只不過在Dictionary中一個鍵只能對應一個元素,而在ILookup<K,T>中,一個鍵能夠對應一組元素(一個序列)。
五、SequenceEqual操做符
SequenceEqual操做符用於判斷兩個序列是否相等。
方法原型:
public static bool SequenceEqual<TSource>(this IEnumerable<TSource> first, IEnumerable<TSource> second); public static bool SequenceEqual<TSource>(this IEnumerable<TSource> first, IEnumerable<TSource> second, IEqualityComparer<TSource> comparer);
代碼示例:
static void Main(string[] args) { int[] ints1 = new int[3] { 1, 2, 3 }; int[] ints2 = new int[3] { 1, 2, 3 }; Console.WriteLine(ints1.SequenceEqual(ints2)); //輸出 True ints2[2] = 4; Console.WriteLine(ints1.SequenceEqual(ints2)); //輸出False Console.ReadKey(); }
六、First操做符
First操做符用於返回輸入序列的第一個元素或知足條件的第一個元素。
方法原型:
public static TSource First<TSource>(this IEnumerable<TSource> source); public static TSource First<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate);
第二個方法多了一個委託,是再次篩選的,即第一個元素要令第二個委託爲True才返回。
代碼示例:
static void Main(string[] args) { int[] ints1 = new int[3] { 1, 2, 3 }; int i = ints1.Where(p => p > 1).First(); Console.WriteLine(i); //輸出 2 int j = ints1.Where(p => p > 1).First(m => m > 2); //輸出3,First參數裏的委託要求第一個大於2的元素 Console.WriteLine(j); Console.ReadKey(); }
七、FirstOrDefault操做符
FirstOrDefault操做符的使用與First操做符很是類似,只是在沒有找到元素時,First操做符會拋出一個異常,而FirstOrDefault操做符會返回一個相關類型默認值元素。
方法原型:
public static TSource FirstOrDefault<TSource>(this IEnumerable<TSource> source); public static TSource FirstOrDefault<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate);
第二個方法原型,增長了一個委託參數,該參數是進一步篩選用的。
static void Main(string[] args) { int[] ints1 = new int[3] { 1, 2, 3 }; //int i = ints1.Where(p => p > 4).First(); //此處會拋出異常,「序列中不包含任何元素」 //Console.WriteLine(i); int j = ints1.Where(p => p > 4).FirstOrDefault(); Console.WriteLine(j); //輸出0 能夠看到在沒有元素的時候,int類型返回0,其餘類型也會返回默認值 int k = ints1.Where(p => p > 1).FirstOrDefault(m => m > 2); Console.WriteLine(k); //輸出3 Console.ReadKey(); }
八、Last操做符
Last操做符用於返回輸入序列的最後一個元素或知足條件的最後一個元素。
方法原型:
public static TSource Last<TSource>(this IEnumerable<TSource> source); public static TSource Last<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate);
第二個方法原型能夠再次篩選。
代碼示例:
static void Main(string[] args) { int[] ints1 = new int[3] { 1, 2, 3 }; int i = ints1.Where(p => p > 1).Last(); //大於1的有2 3 Console.WriteLine(i); //輸出 3 int j = ints1.Where(p => p > 1).Last(m => m < 3); Console.WriteLine(j); //輸出 2 Console.ReadKey(); }
九、LastOrDefault操做符
LastOrDefault操做符的使用與Last操做符相似,只是在沒有找到元素時,Last操做符會拋出一個異常,而LastOrDefault會返回一個默認元素。
方法原型:
public static TSource LastOrDefault<TSource>(this IEnumerable<TSource> source); public static TSource LastOrDefault<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate);
代碼示例:
static void Main(string[] args) { int[] ints1 = new int[3] { 1, 2, 3 }; int i = ints1.Where(p => p > 4).LastOrDefault(); //大於4的元素沒有 Console.WriteLine(i); //輸出 0 int j = ints1.Where(p => p > 1).LastOrDefault(m => m < 3); Console.WriteLine(j); //輸出 2 Console.ReadKey(); }
十、Single操做符
Single操做符用於從一個只含有一個元素的輸入序列中返回惟一的元素或者知足特定條件的惟一元素。若是輸入序列中的元素個數爲零或多於一個,Single操做符會拋出一個異常。
方法原型:
public static TSource Single<TSource>(this IEnumerable<TSource> source); public static TSource Single<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate);
第二個方法原型,多了個委託,能夠再傳入條件篩選。
static void Main(string[] args) { int[] ints1 = new int[1] { 10 }; int i = ints1.Single(); Console.WriteLine(i); //輸出10 int[] ints2 = new int[3] { 1, 2, 3 }; int j = ints2.Single(m => m > 2); Console.WriteLine(j); //輸出3 Console.ReadKey(); }
十一、SingleOrDefault操做符
SingleOrDefault操做符的使用與Single操做符很是類似,只是在沒有找到元素時,Single操做符會拋出一個異常對象,而SingleOrDefault會返回一個默認值的元素。
方法原型:
public static TSource SingleOrDefault<TSource>(this IEnumerable<TSource> source); public static TSource SingleOrDefault<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate);
第二個方法原型仍是容許傳入條件進一步篩選。
代碼示例:
static void Main(string[] args) { int[] ints1 = new int[0] { }; //沒有元素 int i = ints1.SingleOrDefault(); Console.WriteLine(i); //輸出 0 int[] ints2 = new int[3] { 1, 2, 3 }; int j = ints2.SingleOrDefault(m => m > 4); Console.WriteLine(j); //輸出0 Console.ReadKey(); }
十二、ElementAt操做符
ElementAt操做符用於返回輸入序列中指定位置的元素。
方法原型:
public static TSource ElementAt<TSource>(this IEnumerable<TSource> source, int index);
代碼示例:
static void Main(string[] args) { int[] ints1 = new int[3] { 1, 2, 3 }; int i = ints1.ElementAt(2); Console.WriteLine(i); //輸出 3 Console.ReadKey(); }
1三、ElementAtOrDefault操做符
ElementAtOrDefault操做符的使用與ElementAt操做符很是類似,只是在指定的位置索引值不合法的狀況下。
方法原型:
public static TSource ElementAtOrDefault<TSource>(this IEnumerable<TSource> source, int index);
代碼示例:
static void Main(string[] args) { int[] ints1 = new int[3] { 1, 2, 3 }; int i = ints1.ElementAtOrDefault(4); Console.WriteLine(i); //輸出 0 Console.ReadKey(); }
1四、Any操做符
Any操做符用於判斷一個輸入序列中是否含有元素或者知足特定條件的元素。
方法原型:
public static bool Any<TSource>(this IEnumerable<TSource> source); public static bool Any<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate);
代碼示例:
static void Main(string[] args) { int[] ints1 = new int[3] { 1, 2, 3 }; bool b1 = ints1.Any(); Console.WriteLine(b1); //返回True 輸入序列中包含元素 bool b2 = ints1.Any(p => p > 4); //判斷輸入序列中是否包含大於4的元素 Console.WriteLine(b2); //輸出False 輸入序列中不包含小於4的元素 Console.ReadKey(); }
1五、All操做符
All操做符用於判斷輸入序列中的全部元素是否所有知足指定條件。
方法原型:
public static bool All<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate);
代碼示例:
static void Main(string[] args) { int[] ints1 = new int[3] { 1, 2, 3 }; bool b = ints1.All(p => p > 0); Console.WriteLine(b); //輸出True 輸入序列中的全部元素都大於0 Console.ReadKey(); }
1六、Contains操做符
Contains操做符用於判斷輸入序列中是否含有指定的元素。
方法原型:
public static bool Contains<TSource>(this IEnumerable<TSource> source, TSource value); public static bool Contains<TSource>(this IEnumerable<TSource> source, TSource value, IEqualityComparer<TSource> comparer);
第二個方法原型支持自定義比較方法。
代碼示例:
static void Main(string[] args) { int[] ints = new int[3] { 1, 2, 3 }; bool b = ints.Contains(2); Console.WriteLine(b); //輸出 True Console.ReadKey(); }
1七、Count操做符
Count操做符用於獲取輸入序列的元素個數或者知足某個特定條件的元素個數。
方法原型:
public static int Count<TSource>(this IEnumerable<TSource> source); public static int Count<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate);
代碼示例:
static void Main(string[] args) { int[] ints = new int[3] { 1, 2, 3 }; int count1 = ints.Count(); Console.WriteLine(count1); //輸出3 int count2 = ints.Count(p => p > 2); //求大於1的元素個數 Console.WriteLine(count2); //輸出1 Console.ReadKey(); }
1八、LongCount操做符
LongCount操做符的使用與Count操做符基本相同,不一樣的是Count操做符以int類型返回元素個數,而LongCount以Long類型返回元素個數。
方法原型:
public static long LongCount<TSource>(this IEnumerable<TSource> source); public static long LongCount<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate);
代碼示例:
static void Main(string[] args) { int[] ints = new int[3] { 1, 2, 3 }; long count1 = ints.LongCount(); Console.WriteLine(count1); //輸出3 long count2 = ints.LongCount(p => p > 2); //求大於1的元素個數 Console.WriteLine(count2); //輸出1 Console.ReadKey(); }
1九、Sum操做符
Sum操做符用於計算輸入序列中全部元素的數值總和,使用Sum操做符時須要注意,輸入序列中的元素類型只能是int、long、double、或者decimal等數值型數據類型。
方法原型:
public static decimal? Sum(this IEnumerable<decimal?> source); public static decimal Sum(this IEnumerable<decimal> source); public static double? Sum(this IEnumerable<double?> source); public static double Sum(this IEnumerable<double> source); public static float? Sum(this IEnumerable<float?> source); public static float Sum(this IEnumerable<float> source); public static int? Sum(this IEnumerable<int?> source); public static int Sum(this IEnumerable<int> source); public static long? Sum(this IEnumerable<long?> source); public static long Sum(this IEnumerable<long> source); public static decimal? Sum<TSource>(this IEnumerable<TSource> source, Func<TSource, decimal?> selector); public static decimal Sum<TSource>(this IEnumerable<TSource> source, Func<TSource, decimal> selector); public static double? Sum<TSource>(this IEnumerable<TSource> source, Func<TSource, double?> selector); public static double Sum<TSource>(this IEnumerable<TSource> source, Func<TSource, double> selector); public static float? Sum<TSource>(this IEnumerable<TSource> source, Func<TSource, float?> selector); public static float Sum<TSource>(this IEnumerable<TSource> source, Func<TSource, float> selector); public static int? Sum<TSource>(this IEnumerable<TSource> source, Func<TSource, int?> selector); public static int Sum<TSource>(this IEnumerable<TSource> source, Func<TSource, int> selector); public static long? Sum<TSource>(this IEnumerable<TSource> source, Func<TSource, long?> selector); public static long Sum<TSource>(this IEnumerable<TSource> source, Func<TSource, long> selector);
代碼示例:
class Program { static void Main(string[] args) { int[] ints = new int[3] { 1, 2, 3 }; int sum1 = ints.Sum(); Console.WriteLine(sum1); //輸出 6 People p1 = new People(1, "張飛", 20); People p2 = new People(2, "關羽", 21); People p3 = new People(3, "劉備", 22); List<People> listP = new List<People>(); listP.Add(p1); listP.Add(p2); listP.Add(p3); int AgeCount = listP.Sum(s => s.Age); Console.WriteLine(AgeCount); //輸出63 Console.ReadKey(); } } public class People { public People(int id, string name, int age) { this.Id = id; this.Name = name; this.Age = age; } public int Id { get; set; } public string Name { get; set; } public int Age { get; set; } }
20、Min操做符
Min操做符用於獲取輸入序列中全部元素的最小值,使用Min操做符須要注意,輸入序列的元素類型只能是int、long、double、decimal等數值型數據類型或者是實現了IComparable<T>接口的數據類型。
方法原型:
public static decimal? Min(this IEnumerable<decimal?> source); public static decimal Min(this IEnumerable<decimal> source); public static double? Min(this IEnumerable<double?> source); public static double Min(this IEnumerable<double> source); public static float? Min(this IEnumerable<float?> source); public static float Min(this IEnumerable<float> source); public static int? Min(this IEnumerable<int?> source); public static int Min(this IEnumerable<int> source); public static long? Min(this IEnumerable<long?> source); public static long Min(this IEnumerable<long> source); public static TSource Min<TSource>(this IEnumerable<TSource> source); public static decimal? Min<TSource>(this IEnumerable<TSource> source, Func<TSource, decimal?> selector); public static decimal Min<TSource>(this IEnumerable<TSource> source, Func<TSource, decimal> selector); public static double? Min<TSource>(this IEnumerable<TSource> source, Func<TSource, double?> selector); public static double Min<TSource>(this IEnumerable<TSource> source, Func<TSource, double> selector); public static float? Min<TSource>(this IEnumerable<TSource> source, Func<TSource, float?> selector); public static float Min<TSource>(this IEnumerable<TSource> source, Func<TSource, float> selector); public static int? Min<TSource>(this IEnumerable<TSource> source, Func<TSource, int?> selector); public static int Min<TSource>(this IEnumerable<TSource> source, Func<TSource, int> selector); public static long? Min<TSource>(this IEnumerable<TSource> source, Func<TSource, long?> selector); public static long Min<TSource>(this IEnumerable<TSource> source, Func<TSource, long> selector); public static TResult Min<TSource, TResult>(this IEnumerable<TSource> source, Func<TSource, TResult> selector);
代碼示例:
static void Main(string[] args) { int[] ints = new int[3] { 1, 2, 3 }; int min1 = ints.Min(); Console.WriteLine(min1); //輸出 1 People p1 = new People(1, "張飛", 20); People p2 = new People(2, "關羽", 21); People p3 = new People(3, "劉備", 22); List<People> listP = new List<People>(); listP.Add(p1); listP.Add(p2); listP.Add(p3); int AgeMin = listP.Min(s => s.Age); Console.WriteLine(AgeMin); //輸出 20 Console.ReadKey(); }
2一、Max操做符
Max操做符用於獲取輸入序列中全部元素的最大值。與Min的用法基本相同
方法原型:
public static decimal? Max(this IEnumerable<decimal?> source); public static decimal Max(this IEnumerable<decimal> source); public static double? Max(this IEnumerable<double?> source); public static double Max(this IEnumerable<double> source); public static float? Max(this IEnumerable<float?> source); public static float Max(this IEnumerable<float> source); public static int? Max(this IEnumerable<int?> source); public static int Max(this IEnumerable<int> source); public static long? Max(this IEnumerable<long?> source); public static long Max(this IEnumerable<long> source); public static TSource Max<TSource>(this IEnumerable<TSource> source); public static decimal? Max<TSource>(this IEnumerable<TSource> source, Func<TSource, decimal?> selector); public static decimal Max<TSource>(this IEnumerable<TSource> source, Func<TSource, decimal> selector); public static double? Max<TSource>(this IEnumerable<TSource> source, Func<TSource, double?> selector); public static double Max<TSource>(this IEnumerable<TSource> source, Func<TSource, double> selector); public static float? Max<TSource>(this IEnumerable<TSource> source, Func<TSource, float?> selector); public static float Max<TSource>(this IEnumerable<TSource> source, Func<TSource, float> selector); public static int? Max<TSource>(this IEnumerable<TSource> source, Func<TSource, int?> selector); public static int Max<TSource>(this IEnumerable<TSource> source, Func<TSource, int> selector); public static long? Max<TSource>(this IEnumerable<TSource> source, Func<TSource, long?> selector); public static long Max<TSource>(this IEnumerable<TSource> source, Func<TSource, long> selector); public static TResult Max<TSource, TResult>(this IEnumerable<TSource> source, Func<TSource, TResult> selector);
代碼示例:
static void Main(string[] args) { int[] ints = new int[3] { 1, 2, 3 }; int max1 = ints.Max(); Console.WriteLine(max1); //輸出 3 People p1 = new People(1, "張飛", 20); People p2 = new People(2, "關羽", 21); People p3 = new People(3, "劉備", 22); List<People> listP = new List<People>(); listP.Add(p1); listP.Add(p2); listP.Add(p3); int AgeMax = listP.Max(s => s.Age); Console.WriteLine(AgeMax); //輸出 22 Console.ReadKey(); }
2二、Average操做符
Average操做符用於計算輸入序列中全體元素的平均值。一樣,輸入序列中的元素類型值可以是int、long、double、decimal等數值類型數據。
方法原型:
public static decimal? Average(this IEnumerable<decimal?> source); public static decimal Average(this IEnumerable<decimal> source); public static double? Average(this IEnumerable<double?> source); public static double Average(this IEnumerable<double> source); public static float? Average(this IEnumerable<float?> source); public static float Average(this IEnumerable<float> source); public static double? Average(this IEnumerable<int?> source); public static double Average(this IEnumerable<int> source); public static double? Average(this IEnumerable<long?> source); public static double Average(this IEnumerable<long> source); public static decimal? Average<TSource>(this IEnumerable<TSource> source, Func<TSource, decimal?> selector); public static decimal Average<TSource>(this IEnumerable<TSource> source, Func<TSource, decimal> selector); public static double? Average<TSource>(this IEnumerable<TSource> source, Func<TSource, double?> selector); public static double Average<TSource>(this IEnumerable<TSource> source, Func<TSource, double> selector); public static float? Average<TSource>(this IEnumerable<TSource> source, Func<TSource, float?> selector); public static float Average<TSource>(this IEnumerable<TSource> source, Func<TSource, float> selector); public static double? Average<TSource>(this IEnumerable<TSource> source, Func<TSource, int?> selector); public static double Average<TSource>(this IEnumerable<TSource> source, Func<TSource, int> selector); public static double? Average<TSource>(this IEnumerable<TSource> source, Func<TSource, long?> selector); public static double Average<TSource>(this IEnumerable<TSource> source, Func<TSource, long> selector);
代碼示例:
static void Main(string[] args) { int[] ints = new int[3] { 1, 2, 3 }; double ave1 = ints.Average(); Console.WriteLine(ave1); //輸出 2 People p1 = new People(1, "張飛", 20); People p2 = new People(2, "關羽", 21); People p3 = new People(3, "劉備", 22); List<People> listP = new List<People>(); listP.Add(p1); listP.Add(p2); listP.Add(p3); double AgeAve = listP.Average(s => s.Age); Console.WriteLine(AgeAve); //輸出 21 Console.ReadKey(); }
2三、Aggregate操做符
Aggregate操做符用於在輸入序列的全部元素上累積執行某個特定額方法。
方法原型:
public static TSource Aggregate<TSource>(this IEnumerable<TSource> source, Func<TSource, TSource, TSource> func); public static TAccumulate Aggregate<TSource, TAccumulate>(this IEnumerable<TSource> source, TAccumulate seed, Func<TAccumulate, TSource, TAccumulate> func); public static TResult Aggregate<TSource, TAccumulate, TResult>(this IEnumerable<TSource> source, TAccumulate seed, Func<TAccumulate, TSource, TAccumulate> func, Func<TAccumulate, TResult> resultSelector);
代碼示例:
static void Main(string[] args) { int[] ints = new int[4] { 1, 2, 3 ,4}; int i = ints.Aggregate((p,next) => p + next); //p表明當前元素,next表明下一個元素 Console.WriteLine(i); //輸出10 int j = ints.Aggregate(5,(p, next) => p + next); //所有加完再加 15 Console.WriteLine(j); //輸出11 int k = ints.Aggregate(5, (p, next) => p * next); //所有乘完再加 120 Console.WriteLine(k); //輸出30 Console.ReadKey(); }