How to use LINQ extension methods on non generic IEnumerable?

LINQ extension methods are great but… it’s not so cool when we use non generic collections as it is often the case with Sharepoint or TFS APIs.

Likely, we have two LINQ extension methods Cast and OfType which allow us to transform our non generic collection into a generic one.

For example, with a StoredQueryCollection sqc, it is possible de do sqc.Cast<StoredProcedure>().FirstOrDefault(predicate);

Ok great but the Cast is boring. I think it would be better if we were be able to do sqc.FirstOrDefault<StoredProcedure>(predicate);

To do this, I redefine LINQ extension methods for non generic IEnumerable :

public static class IEnumerableExtension

{

    public static TSource Aggregate<TSource>(this IEnumerable source, Func<TSource, TSource, TSource> func)

    {

        return source.Cast<TSource>().Aggregate(func);

    }

    public static TAccumulate Aggregate<TSource, TAccumulate>(this IEnumerable source, TAccumulate seed, Func<TAccumulate, TSource, TAccumulate> func)

    {

        return source.Cast<TSource>().Aggregate(seed, func);

    }

    public static TResult Aggregate<TSource, TAccumulate, TResult>(this IEnumerable source, TAccumulate seed, Func<TAccumulate, TSource, TAccumulate> func, Func<TAccumulate, TResult> resultSelector)

    {

        return source.Cast<TSource>().Aggregate(seed, func, resultSelector);

    }

    public static bool All<TSource>(this IEnumerable source, Func<TSource, bool> predicate)

    {

        return source.Cast<TSource>().All(predicate);

    }

    public static bool Any(this IEnumerable source)

    {

        var enumerator = source.GetEnumerator();

        return enumerator.MoveNext();

    }

    public static bool Any<TSource>(this IEnumerable source)

    {

        return source.OfType<TSource>().Any();

    }

    public static bool Any<TSource>(this IEnumerable source, Func<TSource, bool> predicate)

    {

        return source.Cast<TSource>().Any(predicate);

    }

    public static decimal? Average<TSource>(this IEnumerable source, Func<TSource, decimal?> selector)

    {

        return source.Cast<TSource>().Average(selector);

    }

    public static decimal Average<TSource>(this IEnumerable source, Func<TSource, decimal> selector)

    {

        return source.Cast<TSource>().Average(selector);

    }

    public static double? Average<TSource>(this IEnumerable source, Func<TSource, double?> selector)

    {

        return source.Cast<TSource>().Average(selector);

    }

    public static double Average<TSource>(this IEnumerable source, Func<TSource, double> selector)

    {

        return source.Cast<TSource>().Average(selector);

    }

    public static float? Average<TSource>(this IEnumerable source, Func<TSource, float?> selector)

    {

        return source.Cast<TSource>().Average(selector);

    }

    public static float Average<TSource>(this IEnumerable source, Func<TSource, float> selector)

    {

        return source.Cast<TSource>().Average(selector);

    }

    public static double? Average<TSource>(this IEnumerable source, Func<TSource, int?> selector)

    {

        return source.Cast<TSource>().Average(selector);

    }

    public static double Average<TSource>(this IEnumerable source, Func<TSource, int> selector)

    {

        return source.Cast<TSource>().Average(selector);

    }

    public static double? Average<TSource>(this IEnumerable source, Func<TSource, long?> selector)

    {

        return source.Cast<TSource>().Average(selector);

    }

    public static double Average<TSource>(this IEnumerable source, Func<TSource, long> selector)

    {

        return source.Cast<TSource>().Average(selector);

    }

    public static IEnumerable<TSource> Concat<TSource>(this IEnumerable first, IEnumerable second)

    {

        return first.Cast<TSource>().Concat(second.Cast<TSource>());

    }

    public static bool Contains(this IEnumerable source, object value)

    {

        foreach (var item in source)

            if (item == value)

                return true;

        return false;

    }

    public static bool Contains<TSource>(this IEnumerable source, TSource value, IEqualityComparer<TSource> comparer)

    {

        return source.Cast<TSource>().Contains(value, comparer);

    }

    public static int Count(this IEnumerable source)

    {

        int count = 0;

        foreach (var item in source)

            count++;

        return count;

    }

    public static int Count<TSource>(this IEnumerable source)

    {

        return source.OfType<TSource>().Count();

    }

    public static int Count<TSource>(this IEnumerable source, Func<TSource, bool> predicate)

    {

        return source.Cast<TSource>().Count(predicate);

    }

    public static IEnumerable<TSource> DefaultIfEmpty<TSource>(this IEnumerable source)

    {

        return source.Cast<TSource>().DefaultIfEmpty();

    }

    public static IEnumerable<TSource> DefaultIfEmpty<TSource>(this IEnumerable source, TSource defaultValue)

    {

        return source.Cast<TSource>().DefaultIfEmpty(defaultValue);

    }

    public static IEnumerable<TSource> Distinct<TSource>(this IEnumerable source)

    {

        return source.Cast<TSource>().Distinct();

    }

    public static IEnumerable<TSource> Distinct<TSource>(this IEnumerable source, IEqualityComparer<TSource> comparer)

    {

        return source.Cast<TSource>().Distinct(comparer);

    }

    public static TSource ElementAt<TSource>(this IEnumerable source, int index)

    {

        return source.Cast<TSource>().ElementAt(index);

    }

    public static TSource ElementAtOrDefault<TSource>(this IEnumerable source, int index)

    {

        return source.Cast<TSource>().ElementAtOrDefault(index);

    }

    public static IEnumerable<TSource> Except<TSource>(this IEnumerable first, IEnumerable second)

    {

        return first.Cast<TSource>().Except(second.Cast<TSource>());

    }

    public static IEnumerable<TSource> Except<TSource>(this IEnumerable first, IEnumerable second, IEqualityComparer<TSource> comparer)

    {

        return first.Cast<TSource>().Except(second.Cast<TSource>(), comparer);

    }

    public static TSource First<TSource>(this IEnumerable source)

    {

        return source.Cast<TSource>().First();

    }

    public static TSource First<TSource>(this IEnumerable source, Func<TSource, bool> predicate)

    {

        return source.Cast<TSource>().First(predicate);

    }

    public static TSource FirstOrDefault<TSource>(this IEnumerable source)

    {

        return source.Cast<TSource>().FirstOrDefault();

    }

    public static TSource FirstOrDefault<TSource>(this IEnumerable source, Func<TSource, bool> predicate)

    {

        return source.Cast<TSource>().FirstOrDefault(predicate);

    }

    public static IEnumerable<IGrouping<TKey, TSource>> GroupBy<TSource, TKey>(this IEnumerable source, Func<TSource, TKey> keySelector)

    {

        return source.Cast<TSource>().GroupBy(keySelector);

    }

    public static IEnumerable<TResult> GroupBy<TSource, TKey, TResult>(this IEnumerable source, Func<TSource, TKey> keySelector, Func<TKey, IEnumerable<TSource>, TResult> resultSelector)

    {

        return source.GroupBy(keySelector, resultSelector);

    }

    public static IEnumerable<IGrouping<TKey, TElement>> GroupBy<TSource, TKey, TElement>(this IEnumerable source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector)

    {

        return source.Cast<TSource>().GroupBy(keySelector, elementSelector);

    }

    public static IEnumerable<IGrouping<TKey, TSource>> GroupBy<TSource, TKey>(this IEnumerable source, Func<TSource, TKey> keySelector, IEqualityComparer<TKey> comparer)

    {

        return source.Cast<TSource>().GroupBy(keySelector, comparer);

    }

    public static IEnumerable<TResult> GroupBy<TSource, TKey, TResult>(this IEnumerable source, Func<TSource, TKey> keySelector, Func<TKey, IEnumerable<TSource>, TResult> resultSelector, IEqualityComparer<TKey> comparer)

    {

        return source.Cast<TSource>().GroupBy(keySelector, resultSelector, comparer);

    }

    public static IEnumerable<TResult> GroupBy<TSource, TKey, TElement, TResult>(this IEnumerable source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector, Func<TKey, IEnumerable<TElement>, TResult> resultSelector)

    {

        return source.Cast<TSource>().GroupBy(keySelector, elementSelector, resultSelector);

    }

    public static IEnumerable<IGrouping<TKey, TElement>> GroupBy<TSource, TKey, TElement>(this IEnumerable source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector, IEqualityComparer<TKey> comparer)

    {

        return source.Cast<TSource>().GroupBy(keySelector, elementSelector, comparer);

    }

    public static IEnumerable<TResult> GroupBy<TSource, TKey, TElement, TResult>(this IEnumerable source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector, Func<TKey, IEnumerable<TElement>, TResult> resultSelector, IEqualityComparer<TKey> comparer)

    {

        return source.Cast<TSource>().GroupBy(keySelector, elementSelector, resultSelector, comparer);

    }

    public static IEnumerable<TResult> GroupJoin<TOuter, TInner, TKey, TResult>(this IEnumerable outer, IEnumerable inner, Func<TOuter, TKey> outerKeySelector, Func<TInner, TKey> innerKeySelector, Func<TOuter, IEnumerable<TInner>, TResult> resultSelector)

    {

        return outer.Cast<TOuter>().GroupJoin(inner.Cast<TInner>(), outerKeySelector, innerKeySelector, resultSelector);

    }

    public static IEnumerable<TResult> GroupJoin<TOuter, TInner, TKey, TResult>(this IEnumerable outer, IEnumerable inner, Func<TOuter, TKey> outerKeySelector, Func<TInner, TKey> innerKeySelector, Func<TOuter, IEnumerable<TInner>, TResult> resultSelector, IEqualityComparer<TKey> comparer)

    {

        return outer.GroupJoin(inner, outerKeySelector, innerKeySelector, resultSelector, comparer);

    }

    public static IEnumerable<TSource> Intersect<TSource>(this IEnumerable first, IEnumerable second)

    {

        return first.Cast<TSource>().Intersect(second.Cast<TSource>());

    }

    public static IEnumerable<TSource> Intersect<TSource>(this IEnumerable first, IEnumerable<TSource> second, IEqualityComparer<TSource> comparer)

    {

        return first.Cast<TSource>().Intersect(second.Cast<TSource>(), comparer);

    }

    public static IEnumerable<TResult> Join<TOuter, TInner, TKey, TResult>(this IEnumerable outer, IEnumerable inner, Func<TOuter, TKey> outerKeySelector, Func<TInner, TKey> innerKeySelector, Func<TOuter, TInner, TResult> resultSelector)

    {

        return outer.Cast<TOuter>().Join(inner.Cast<TInner>(), outerKeySelector, innerKeySelector, resultSelector);

    }

    public static IEnumerable<TResult> Join<TOuter, TInner, TKey, TResult>(this IEnumerable outer, IEnumerable inner, Func<TOuter, TKey> outerKeySelector, Func<TInner, TKey> innerKeySelector, Func<TOuter, TInner, TResult> resultSelector, IEqualityComparer<TKey> comparer)

    {

        return outer.Cast<TOuter>().Join(inner.Cast<TInner>(), outerKeySelector, innerKeySelector, resultSelector, comparer);

    }

    public static TSource Last<TSource>(this IEnumerable source)

    {

        return source.Cast<TSource>().Last();

    }

    public static TSource Last<TSource>(this IEnumerable source, Func<TSource, bool> predicate)

    {

        return source.Cast<TSource>().Last(predicate);

    }

    public static TSource LastOrDefault<TSource>(this IEnumerable source)

    {

        return source.Cast<TSource>().LastOrDefault();

    }

    public static TSource LastOrDefault<TSource>(this IEnumerable source, Func<TSource, bool> predicate)

    {

        return source.Cast<TSource>().LastOrDefault(predicate);

    }

    public static long LongCount<TSource>(this IEnumerable source)

    {

        return source.Cast<TSource>().LongCount();

    }

    public static long LongCount(this IEnumerable source, Func<TSource, bool> predicate)

    {

        long count = 0;

        foreach (var item in source)

            count++;

        return count;

    }

    public static long LongCount<TSource>(this IEnumerable source, Func<TSource, bool> predicate)

    {

        return source.OfType<TSource>().LongCount(predicate);

    }

    public static TSource Max<TSource>(this IEnumerable source)

    {

        return source.Cast<TSource>().Max();

    }

    public static decimal? Max<TSource>(this IEnumerable source, Func<TSource, decimal?> selector)

    {

        return source.Cast<TSource>().Max(selector);

    }

    public static decimal Max<TSource>(this IEnumerable source, Func<TSource, decimal> selector)

    {

        return source.Cast<TSource>().Max(selector);

    }

    public static double? Max<TSource>(this IEnumerable source, Func<TSource, double?> selector)

    {

        return source.Cast<TSource>().Max(selector);

    }

    public static double Max<TSource>(this IEnumerable source, Func<TSource, double> selector)

    {

        return source.Cast<TSource>().Max(selector);

    }

    public static float? Max<TSource>(this IEnumerable source, Func<TSource, float?> selector)

    {

        return source.Cast<TSource>().Max(selector);

    }

    public static float Max<TSource>(this IEnumerable source, Func<TSource, float> selector)

    {

        return source.Cast<TSource>().Max(selector);

    }

    public static int? Max<TSource>(this IEnumerable source, Func<TSource, int?> selector)

    {

        return source.Cast<TSource>().Max(selector);

    }

    public static int Max<TSource>(this IEnumerable source, Func<TSource, int> selector)

    {

        return source.Cast<TSource>().Max(selector);

    }

    public static long? Max<TSource>(this IEnumerable source, Func<TSource, long?> selector)

    {

        return source.Cast<TSource>().Max(selector);

    }

    public static long Max<TSource>(this IEnumerable source, Func<TSource, long> selector)

    {

        return source.Cast<TSource>().Max(selector);

    }

    public static TResult Max<TSource, TResult>(this IEnumerable source, Func<TSource, TResult> selector)

    {

        return source.Cast<TSource>().Max(selector);

    }

    public static TSource Min<TSource>(this IEnumerable source)

    {

        return source.Cast<TSource>().Min();

    }

    public static decimal? Min<TSource>(this IEnumerable source, Func<TSource, decimal?> selector)

    {

        return source.Cast<TSource>().Min(selector);

    }

    public static decimal Min<TSource>(this IEnumerable source, Func<TSource, decimal> selector)

    {

        return source.Cast<TSource>().Min(selector);

    }

    public static double? Min<TSource>(this IEnumerable source, Func<TSource, double?> selector)

    {

        return source.Cast<TSource>().Min(selector);

    }

    public static double Min<TSource>(this IEnumerable source, Func<TSource, double> selector)

    {

        return source.Cast<TSource>().Min(selector);

    }

    public static float? Min<TSource>(this IEnumerable source, Func<TSource, float?> selector)

    {

        return source.Cast<TSource>().Min(selector);

    }

    public static float Min<TSource>(this IEnumerable source, Func<TSource, float> selector)

    {

        return source.Cast<TSource>().Min(selector);

    }

    public static int? Min<TSource>(this IEnumerable source, Func<TSource, int?> selector)

    {

        return source.Cast<TSource>().Min(selector);

    }

    public static int Min<TSource>(this IEnumerable source, Func<TSource, int> selector)

    {

        return source.Cast<TSource>().Min(selector);

    }

    public static long? Min<TSource>(this IEnumerable source, Func<TSource, long?> selector)

    {

        return source.Cast<TSource>().Min(selector);

    }

    public static long Min<TSource>(this IEnumerable source, Func<TSource, long> selector)

    {

        return source.Cast<TSource>().Min(selector);

    }

    public static TResult Min<TSource, TResult>(this IEnumerable source, Func<TSource, TResult> selector)

    {

        return source.Cast<TSource>().Min(selector);

    }

    public static IOrderedEnumerable<TSource> OrderBy<TSource, TKey>(this IEnumerable source, Func<TSource, TKey> keySelector)

    {

        return source.Cast<TSource>().OrderBy(keySelector);

    }

    public static IOrderedEnumerable<TSource> OrderBy<TSource, TKey>(this IEnumerable source, Func<TSource, TKey> keySelector, IComparer<TKey> comparer)

    {

        return source.Cast<TSource>().OrderBy(keySelector, comparer);

    }

    public static IOrderedEnumerable<TSource> OrderByDescending<TSource, TKey>(this IEnumerable source, Func<TSource, TKey> keySelector)

    {

        return source.Cast<TSource>().OrderByDescending(keySelector);

    }

    public static IOrderedEnumerable<TSource> OrderByDescending<TSource, TKey>(this IEnumerable source, Func<TSource, TKey> keySelector, IComparer<TKey> comparer)

    {

        return source.Cast<TSource>().OrderByDescending(keySelector, comparer);

    }

    public static IEnumerable<TSource> Reverse<TSource>(this IEnumerable source)

    {

        return source.Cast<TSource>().Reverse();

    }

    public static IEnumerable<TResult> Select<TSource, TResult>(this IEnumerable source, Func<TSource, int, TResult> selector)

    {

        return source.Cast<TSource>().Select(selector);

    }

    public static IEnumerable<TResult> Select<TSource, TResult>(this IEnumerable source, Func<TSource, TResult> selector)

    {

        return source.Cast<TSource>().Select(selector);

    }

    public static IEnumerable<TResult> SelectMany<TSource, TResult>(this IEnumerable source, Func<TSource, IEnumerable<TResult>> selector)

    {

        return source.Cast<TSource>().SelectMany(selector);

    }

    public static IEnumerable<TResult> SelectMany<TSource, TResult>(this IEnumerable source, Func<TSource, int, IEnumerable<TResult>> selector)

    {

        return source.Cast<TSource>().SelectMany(selector);

    }

    public static IEnumerable<TResult> SelectMany<TSource, TCollection, TResult>(this IEnumerable source, Func<TSource, IEnumerable<TCollection>> collectionSelector, Func<TSource, TCollection, TResult> resultSelector)

    {

        return source.Cast<TSource>().SelectMany(collectionSelector, resultSelector);

    }

    public static IEnumerable<TResult> SelectMany<TSource, TCollection, TResult>(this IEnumerable source, Func<TSource, int, IEnumerable<TCollection>> collectionSelector, Func<TSource, TCollection, TResult> resultSelector)

    {

        return source.Cast<TSource>().SelectMany(collectionSelector, resultSelector);

    }

    public static bool SequenceEqual<TSource>(this IEnumerable first, IEnumerable second)

    {

        return first.Cast<TSource>().SequenceEqual(second.Cast<TSource>());

    }

    public static bool SequenceEqual<TSource>(this IEnumerable first, IEnumerable second, IEqualityComparer<TSource> comparer)

    {

        return first.Cast<TSource>().SequenceEqual(second, comparer);

    }

    public static TSource Single<TSource>(this IEnumerable source)

    {

        return source.Cast<TSource>().Single();

    }

    public static TSource Single<TSource>(this IEnumerable source, Func<TSource, bool> predicate)

    {

        return source.Cast<TSource>().Single(predicate);

    }

    public static TSource SingleOrDefault<TSource>(this IEnumerable source)

    {

        return source.Cast<TSource>().SingleOrDefault();

    }

    public static TSource SingleOrDefault<TSource>(this IEnumerable source, Func<TSource, bool> predicate)

    {

        return source.Cast<TSource>().SingleOrDefault(predicate);

    }

    public static IEnumerable<TSource> Skip<TSource>(this IEnumerable source, int count)

    {

        return source.Cast<TSource>().Skip(count);

    }

    public static IEnumerable<TSource> SkipWhile<TSource>(this IEnumerable source, Func<TSource, bool> predicate)

    {

        return source.Cast<TSource>().SkipWhile(predicate);

    }

    public static IEnumerable<TSource> SkipWhile<TSource>(this IEnumerable source, Func<TSource, int, bool> predicate)

    {

        return source.Cast<TSource>().SkipWhile(predicate);

    }

    public static decimal? Sum<TSource>(this IEnumerable source, Func<TSource, decimal?> selector)

    {

        return source.Cast<TSource>().Sum(selector);

    }

    public static decimal Sum<TSource>(this IEnumerable source, Func<TSource, decimal> selector)

    {

        return source.Cast<TSource>().Sum(selector);

    }

    public static double? Sum<TSource>(this IEnumerable source, Func<TSource, double?> selector)

    {

        return source.Cast<TSource>().Sum(selector);

    }

    public static double Sum<TSource>(this IEnumerable source, Func<TSource, double> selector)

    {

        return source.Cast<TSource>().Sum(selector);

    }

    public static float? Sum<TSource>(this IEnumerable source, Func<TSource, float?> selector)

    {

        return source.Cast<TSource>().Sum(selector);

    }

    public static float Sum<TSource>(this IEnumerable source, Func<TSource, float> selector)

    {

        return source.Cast<TSource>().Sum(selector);

    }

    public static int? Sum<TSource>(this IEnumerable source, Func<TSource, int?> selector)

    {

        return source.Cast<TSource>().Sum(selector);

    }

    public static int Sum<TSource>(this IEnumerable source, Func<TSource, int> selector)

    {

        return source.Cast<TSource>().Sum(selector);

    }

    public static long? Sum<TSource>(this IEnumerable source, Func<TSource, long?> selector)

    {

        return source.Cast<TSource>().Sum(selector);

    }

    public static long Sum<TSource>(this IEnumerable source, Func<TSource, long> selector)

    {

        return source.Cast<TSource>().Sum(selector);

    }

    public static IEnumerable<TSource> Take<TSource>(this IEnumerable source, int count)

    {

        return source.Cast<TSource>().Take(count);

    }

    public static IEnumerable<TSource> TakeWhile<TSource>(this IEnumerable source, Func<TSource, bool> predicate)

    {

        return source.Cast<TSource>().TakeWhile(predicate);

    }

    public static IEnumerable<TSource> TakeWhile<TSource>(this IEnumerable source, Func<TSource, int, bool> predicate)

    {

        return source.Cast<TSource>().TakeWhile(predicate);

    }

    public static TSource[] ToArray<TSource>(this IEnumerable source)

    {

        return source.Cast<TSource>().ToArray();

    }

    public static Dictionary<TKey, TSource> ToDictionary<TSource, TKey>(this IEnumerable source, Func<TSource, TKey> keySelector)

    {

        return source.Cast<TSource>().ToDictionary(keySelector);

    }

    public static Dictionary<TKey, TElement> ToDictionary<TSource, TKey, TElement>(this IEnumerable source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector)

    {

        return source.ToDictionary(keySelector, elementSelector);

    }

    public static Dictionary<TKey, TSource> ToDictionary<TSource, TKey>(this IEnumerable source, Func<TSource, TKey> keySelector, IEqualityComparer<TKey> comparer)

    {

        return source.Cast<TSource>().ToDictionary(keySelector, comparer);

    }

    public static Dictionary<TKey, TElement> ToDictionary<TSource, TKey, TElement>(this IEnumerable source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector, IEqualityComparer<TKey> comparer)

    {

        return source.Cast<TSource>().ToDictionary(keySelector, elementSelector, comparer);

    }

    public static List<TSource> ToList<TSource>(this IEnumerable source)

    {

        return source.Cast<TSource>().ToList();

    }

    public static ILookup<TKey, TSource> ToLookup<TSource, TKey>(this IEnumerable source, Func<TSource, TKey> keySelector)

    {

        return source.Cast<TSource>().ToLookup(keySelector);

    }

    public static ILookup<TKey, TElement> ToLookup<TSource, TKey, TElement>(this IEnumerable source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector)

    {

        return source.Cast<TSource>().ToLookup(keySelector, elementSelector);

    }

    public static ILookup<TKey, TSource> ToLookup<TSource, TKey>(this IEnumerable source, Func<TSource, TKey> keySelector, IEqualityComparer<TKey> comparer)

    {

        return source.Cast<TSource>().ToLookup(keySelector, comparer);

    }

    public static ILookup<TKey, TElement> ToLookup<TSource, TKey, TElement>(this IEnumerable source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector, IEqualityComparer<TKey> comparer)

    {

        return source.Cast<TSource>().ToLookup(keySelector, elementSelector, comparer);

    }

    public static IEnumerable<TSource> Union<TSource>(this IEnumerable first, IEnumerable second)

    {

        return first.Cast<TSource>().Union(second.Cast<TSource>());

    }

    public static IEnumerable<TSource> Union<TSource>(this IEnumerable first, IEnumerable second, IEqualityComparer<TSource> comparer)

    {

        return first.Cast<TSource>().Union(second.Cast<TSource>(), comparer);

    }

    public static IEnumerable<TSource> Where<TSource>(this IEnumerable source, Func<TSource, bool> predicate)

    {

        return source.Cast<TSource>().Where(predicate);

    }

    public static IEnumerable<TSource> Where<TSource>(this IEnumerable source, Func<TSource, int, bool> predicate)

    {

        return source.Cast<TSource>().Where(predicate);

    }

}


Enjoy [:)] 

This entry was posted in 7671, 7672, 7677. Bookmark the permalink.

7 Responses to How to use LINQ extension methods on non generic IEnumerable?

  1. Smartass says:

    You just put in a ton of effort to get rid of 4 characters per invocation. Usually lazy is good, but that doesn’t seem like the smart kind of lazy.

    It’ll probably take you years of writing .Cast.LINQ() before you catch up to the amount of effort you just put into this.

    I don’t know how many places in your code use non-generics, but it seems like the best thing to do would have been to wrap those references to non-generics with something that’ll do the .Cast for you. Then it’d work for anything outside LINQ too.

  2. Matthieu MEZIL says:

    I did it for a colleague who works with Sharepoint and who were very happy with this.

  3. Tux says:

    Very, very nice! :)

  4. Vlad says:

    2Smartass: It is probably not an extension to people who know about Cast<> and TypeOf<>. But you will be surprized how many developers are just giving up using linq because ‘it does not work with this enumerable’

  5. Ray Akkanson says:

    Is there any alternative aproach to .Cast Method in Linq?

    Ray Akkanson

  6. 51541 says:

    How to use LINQ extension methods on non generic IEnumerable? – Matthieu MEZIL
    FAKE oakleys http://fakeoakleysunglasses.navsop.us/

Leave a Reply

Your email address will not be published. Required fields are marked *


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>