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