using System; using System.Collections; using System.Collections.Generic; using System.Text; namespace System.Linq { public static partial class Enumerable { // Count / LongCount public static int Count (this IEnumerable source) { if (source is ICollection) return ((ICollection)source).Count; if (source is ICollection) return ((ICollection)source).Count; int count = 0; foreach (TSource element in source) count++; return count; } public static int Count (this IEnumerable source, Func predicate) { if (source == null) throw new ArgumentNullException ("source"); if (predicate == null) throw new ArgumentNullException ("predicate"); int count = 0; foreach (TSource element in source) if (predicate (element)) count++; return count; } public static long LongCount (this IEnumerable source) { if (source is ICollection) return ((ICollection)source).Count; if (source is ICollection) return ((ICollection)source).Count; long count = 0; foreach (TSource element in source) count++; return count; } public static long LongCount (this IEnumerable source, Func predicate) { if (source == null) throw new ArgumentNullException ("source"); if (predicate == null) throw new ArgumentNullException ("predicate"); long count = 0; foreach (TSource element in source) if (predicate (element)) count++; return count; } // Min public static TSource Min (this IEnumerable source) { if (!source.Any () && default (TSource) == null) return default (TSource); return source.Aggregate ((accum, element) => Comparer.Default.Compare (accum, element) < 0 ? accum : element); } public static TSource? Min (this IEnumerable source) where TSource : struct { if (!source.Any ()) return default (TSource?); return source.Aggregate ((accum, element) => Comparer.Default.Compare (accum, element) < 0 ? accum : element); } public static TResult Min (this IEnumerable source, Func selector) { return source.Select (selector).Min (); } // Max public static TSource Max (this IEnumerable source) { if (!source.Any () && default (TSource) == null) return default (TSource); return source.Aggregate ((accum, element) => Comparer.Default.Compare (accum, element) > 0 ? accum : element); } public static TSource? Max (this IEnumerable source) where TSource : struct { if (!source.Any ()) return default (TSource?); return source.Aggregate ((accum, element) => Comparer.Default.Compare (accum, element) > 0 ? accum : element); } public static TResult Max (this IEnumerable source, Func selector) { return source.Select (selector).Max (); } // Aggregate public static TSource Aggregate (this IEnumerable source, Func func) { if (source == null) throw new ArgumentNullException ("source"); if (func == null) throw new ArgumentNullException ("func"); bool noElements = true; TSource runningValue = default (TSource); foreach (TSource element in source) { if (noElements) { noElements = false; runningValue = element; } else runningValue = func (runningValue, element); } if (noElements) ThrowNoElements (); return runningValue; } public static TAccumulate Aggregate ( this IEnumerable source, TAccumulate seed, Func func) { return source.Aggregate (seed, func, x => x); } public static TResult Aggregate ( this IEnumerable source, TAccumulate seed, Func func, Func resultSelector) { if (source == null) throw new ArgumentNullException ("source"); if (func == null) throw new ArgumentNullException ("func"); if (resultSelector == null) throw new ArgumentNullException ("resultSelector"); TAccumulate runningValue = seed; foreach (TSource element in source) runningValue = func (runningValue, element); return resultSelector (runningValue); } } }