C#linq查询方法使用简介
All():判断所有元素是否都满足条件,若有一个不满足就返回false,否则返回true,源代码如下,如果方法里参数均为null,则会抛出异常。若该对象为null也会抛出异常,若该IEnumbe类型里元素个数为0,则会一直返回true
public static bool All<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate) { if (source == null) throw Error.ArgumentNull("source"); if (predicate == null) throw Error.ArgumentNull("predicate"); foreach (TSource element in source) { if (!predicate(element)) return false; } return true; }
Any():如下判断对象里是否有元素,若集合为null,则会报错
public static bool Any<TSource>(this IEnumerable<TSource> source) { if (source == null) throw Error.ArgumentNull("source"); using (IEnumerator<TSource> e = source.GetEnumerator()) { if (e.MoveNext()) return true; } return false; }
如下判断对象里是否存在满足某条件的,若满足为true,否则为false 若对象为null,则会报错
public static bool Any<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate) { if (source == null) throw Error.ArgumentNull("source"); if (predicate == null) throw Error.ArgumentNull("predicate"); foreach (TSource element in source) { if (predicate(element)) return true; } return false; }
Average 求平均值 若集合为Null,则会抛出异常,若集合中没有元素,则也会抛出异常
public static double Average(this IEnumerable<int> source) { if (source == null) throw Error.ArgumentNull("source"); long sum = 0; long count = 0; checked { foreach (int v in source) { sum += v; count++; } } if (count > 0) return (double)sum / count; throw Error.NoElements(); }
public static double? Average(this IEnumerable<int?> source) { if (source == null) throw Error.ArgumentNull("source"); long sum = 0; long count = 0; checked { foreach (int? v in source) { if (v != null) { sum += v.GetValueOrDefault(); count++; } } } if (count > 0) return (double)sum / count; return null; }
LastOrDefault/FirstOrDefault 返回第一个元素,若没有元素,则返回元素的默认值,若集合为Null,则抛出异常,LastOrDefault 相反
First/Last 返回第一个或者最后一个元素,若集合为中没有元素,则抛出异常
Single/SingleOrDefault 返回序列的唯一元素,若存在多个元素,则都报错,若集合为null,则 SingleOrDefault 返回默认值
public static TSource FirstOrDefault<TSource>(this IEnumerable<TSource> source) { if (source == null) throw Error.ArgumentNull("source"); IList<TSource> list = source as IList<TSource>; if (list != null) { if (list.Count > 0) return list[0]; } else { using (IEnumerator<TSource> e = source.GetEnumerator()) { if (e.MoveNext()) return e.Current; } } return default(TSource); }
满足条件的第一个元素
public static TSource FirstOrDefault<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate) {
if (source == null) throw Error.ArgumentNull("source");
if (predicate == null) throw Error.ArgumentNull("predicate");
foreach (TSource element in source) {
if (predicate(element)) return element;
}
return default(TSource);
}
ToList/toArray/ToDictionary
public static List<TSource> ToList<TSource>(this IEnumerable<TSource> source) { if (source == null) throw Error.ArgumentNull("source"); return new List<TSource>(source); }
LongCount/Count 返回元素个数,若集合为Null,则抛出异常
public static int Count<TSource>(this IEnumerable<TSource> source) { if (source == null) throw Error.ArgumentNull("source"); ICollection<TSource> collectionoft = source as ICollection<TSource>; if (collectionoft != null) return collectionoft.Count; ICollection collection = source as ICollection; if (collection != null) return collection.Count; int count = 0; using (IEnumerator<TSource> e = source.GetEnumerator()) { checked { while (e.MoveNext()) count++; } } return count; }
Contains public static bool Contains<TSource>(this IEnumerable<TSource> source, TSource value) { ICollection<TSource> collection = source as ICollection<TSource>; if (collection != null) return collection.Contains(value); return Contains<TSource>(source, value, null); } public static bool Contains<TSource>(this IEnumerable<TSource> source, TSource value, IEqualityComparer<TSource> comparer) { if (comparer == null) comparer = EqualityComparer<TSource>.Default; if (source == null) throw Error.ArgumentNull("source"); foreach (TSource element in source) if (comparer.Equals(element, value)) return true; return false; }
Concat
blic static IEnumerable<TSource> Concat<TSource>(this IEnumerable<TSource> first, IEnumerable<TSource> second) { if (first == null) throw Error.ArgumentNull("first"); if (second == null) throw Error.ArgumentNull("second"); return ConcatIterator<TSource>(first, second); } static IEnumerable<TSource> ConcatIterator<TSource>(IEnumerable<TSource> first, IEnumerable<TSource> second) { foreach (TSource element in first) yield return element; foreach (TSource element in second) yield return element; }
Distinct
public static IEnumerable<TSource> Distinct<TSource>(this IEnumerable<TSource> source) { if (source == null) throw Error.ArgumentNull("source"); return DistinctIterator<TSource>(source, null); } public static IEnumerable<TSource> Distinct<TSource>(this IEnumerable<TSource> source, IEqualityComparer<TSource> comparer) { if (source == null) throw Error.ArgumentNull("source"); return DistinctIterator<TSource>(source, comparer); } static IEnumerable<TSource> DistinctIterator<TSource>(IEnumerable<TSource> source, IEqualityComparer<TSource> comparer) { Set<TSource> set = new Set<TSource>(comparer); foreach (TSource element in source) if (set.Add(element)) yield return element; }
ElementAt /ElementAtOrDefault 返回某个索引处的元素,第一个若索引不存在,则抛出异常,第二个方法若索引越界,则返回元素的默认值。
Except 返回集合差集 Union 返回集合并集 Intersect返回交集
Max/Min 返回集合的最大值和最小值 Sum求和