非延时标准查询操作符是指不具备延时查询特性的标准查询操作符,这些操作符一般用于辅助延时标准查询操作符使用。
1、ToArray操作符
ToArray操作符用于将一个输入序列转换成一个数组。
方法原型:
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(); }
2、ToList操作符
ToList操作符用于将一个输入序列转换成一个List<T>对象。
来看方法原型:
public static List<TSource> ToList<TSource>(this IEnumerable<TSource> source);
注意到返回的List<T>,任意IEnumerable<T>转换为List<T>
代码示例:
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(); }
3、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(); }
4、ToLookup操作符
ToLookup操作符可以将一个输入序列转换成一个ILookup<K,T>集合对象。ILookup与IDictionary<K,T>非常相似,只不过在Dictionary中一个键只能对应一个元素,而在ILookup<K,T>中,一个键可以对应一组元素(一个序列)。
5、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(); }
6、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(); }
7、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(); }
8、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(); }
9、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(); }
10、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(); }
11、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(); }
12、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(); }
13、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(); }
14、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(); }
15、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(); }
16、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(); }
17、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(); }
18、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(); }
19、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(); }
21、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(); }
22、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(); }
23、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(); }