April 2009 - Posts

French ALT .NET Demo

Last week, I spoke at an EF session for the french ALT .NET community.

During its course, I made a WCF demo with EF POCO and a sort of LINQ provider in the client part.

This provider is very simplistic (made in less than 15 minutes), isn't thread safe but is interesting for the POC and because if my provider doesn't support a LINQ method, it will use LINQ To Object method.

I just want to come back on this provider, which is very different than normal LINQ providers, and particularly on this code:

public static class CategoryClient

{

    public static MyQueryable<Category> Categories

    {

        get

        {

            ClientLINQ.AllEntities = true;

            return GetCategories().ToMyQueryable();

        }

    }

 

    private static IEnumerable<Category> GetCategories()

    {

        List<Category> value;

        if (ClientLINQ.AllEntities)

            value = Context.Instance.CategoryContract.GetAllCategories();

        else

            value = Context.Instance.CategoryContract.GetCategories(ClientLINQ.IncludeValues, ClientLINQ.WhereValue, ClientLINQ.OrderByValue, ClientLINQ.SkipValue, ClientLINQ.TakeValue);

        foreach (var category in value)

            yield return category;

    }

}

How does it work? The set of AllEntities to true initializes IncludeValues, WhereValues, etc

IncludeValues = null;

WhereValue = null;

OrderByValue = null;

SkipValue = 0;

TakeValue = 0;

How can AllEntities be false in GetCategories method whereas the instruction which follows AllEntities = true is the GetCategories call and this method starts with a test on AllEntities ?

In fact, you should see that this method uses yield return syntax. So, it will be run only when Categories properties will be iterated. This means that the different LINQ methods are called before. That's why it's works.

EDM Designer: one more new feature

I just added a new feature in my EDM Designer: when an entity type isn't mapped or is only partially mapped (so we aren't able to use our edmx), I reduce its opacity:

image

I do the same for Model Browser:

image

I also do the same for associations:

image

EDM Designer: new feature

I added a new feature in my EDM Designer: the ability to see with context menu in which views the selected type (entity type or complex type) is present and the ability to navigate to this view.

image

Or directly from the Model Browser

image  

EDM Designer: new version

I just published my EDM Designer.

After a lot of talks about it, particularly at the MVP summit, I changed my tasks priorities. With this new version, my EDM Designer doesn't manage SSDL functions (including Stored Procedure) and is not integrated in VS. For this version, I added two features :

  • designer entities location saving
  • Abilty to see different views of the same edmx, which simplifies the big edmx management. Note that an entity type can appear in multiple views.

image

image

image

EF: Working with the context cache

When we want to work with ObjectContext cache, we have to use the ObjectStateManager with its boring syntax.

products = context.ObjectStateManager.GetObjectStateEntries(EntityState.Added | EntityState.Modified | EntityState.Unchanged).Select(ose => ose.Entity).OfType<Product>();

My idea is to make it into an extension method:

public static class ContextCacheExtension

{

    public static IEnumerable<T> GetEntitiesInCache<T>(this ObjectContext context)

    {

        return context.ObjectStateManager.GetObjectStateEntries(EntityState.Added | EntityState.Modified | EntityState.Unchanged).Select(ose => ose.Entity).OfType<T>();

    }

}

Now, I can have a "nicer" code:

products = context.GetEntitiesInCache<Product>();

MVP again :)

All is on the title. Smile

Posted by Matthieu MEZIL | 3 comment(s)
Filed under:

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 Smile 

Posted by Matthieu MEZIL | 3 comment(s)
Filed under: , ,