using System; using System.Collections.Generic; using System.Text; using System.Collections; namespace System.Linq { public static partial class Enumerable { public static List ToList (this IEnumerable source) { if (source == null) throw new ArgumentNullException ("source"); return new List (source); } public static TSource [] ToArray (this IEnumerable source) { if (source == null) throw new ArgumentNullException ("source"); return source.ToList ().ToArray (); } public static Dictionary ToDictionary (this IEnumerable source, Func keySelector) { return source.ToDictionary (keySelector, x => x, null); } public static Dictionary ToDictionary (this IEnumerable source, Func keySelector, IEqualityComparer comparer) { return source.ToDictionary (keySelector, x => x, comparer); } public static Dictionary ToDictionary ( this IEnumerable source, Func keySelector, Func elementSelector) { return source.ToDictionary (keySelector, elementSelector, null); } public static Dictionary ToDictionary ( this IEnumerable source, Func keySelector, Func elementSelector, IEqualityComparer comparer) { if (source == null) throw new ArgumentNullException ("source"); if (keySelector == null) throw new ArgumentNullException ("keySelector"); if (elementSelector == null) throw new ArgumentNullException ("elementSelector"); Dictionary d = new Dictionary (comparer); foreach (TSource element in source) d.Add (keySelector (element), elementSelector (element)); return d; } public static ILookup ToLookup (this IEnumerable source, Func keySelector) { return Lookup.Create (source, keySelector, x => x, null); } public static ILookup ToLookup ( this IEnumerable source, Func keySelector, IEqualityComparer comparer) { return Lookup.Create (source, keySelector, x => x, comparer); } public static ILookup ToLookup ( this IEnumerable source, Func keySelector, Func elementSelector) { return Lookup.Create (source, keySelector, elementSelector, null); } public static ILookup ToLookup ( this IEnumerable source, Func keySelector, Func elementSelector, IEqualityComparer comparer) { return Lookup.Create (source, keySelector, elementSelector, comparer); } public static IEnumerable AsEnumerable (this IEnumerable source) { return source; } public static IEnumerable OfType (this IEnumerable source) { if (source == null) throw new ArgumentNullException ("source"); foreach (object obj in source) if (obj is TResult) yield return (TResult) obj; } public static IEnumerable Cast (this IEnumerable source) { if (source == null) throw new ArgumentNullException ("source"); foreach (object obj in source) yield return (TResult) obj; } } }