#if NET20 || NET30 || !NET_4_6 using System.Collections; using System.Collections.Generic; //using System.Linq.Expressions; using System.Linq.Expressions.Reimplement; using System.Reflection.Extensions; using LinqInternal.Core; using MethodBase = System.Reflection.MethodBase; using MethodInfo = System.Reflection.MethodInfo; namespace System.Linq.Reimplement { public static class Queryable { public static TSource Aggregate(this IQueryable source, Expression> func) { LinqCheck.SourceAndFunc(source, func); return source.Provider.Execute( StaticCall( MakeGeneric(MethodBase.GetCurrentMethod(), typeof(TSource)), source.Expression, Expression.Quote(func))); } public static TAccumulate Aggregate(this IQueryable source, TAccumulate seed, Expression> func) { LinqCheck.SourceAndFunc(source, func); return source.Provider.Execute( StaticCall( MakeGeneric(MethodBase.GetCurrentMethod(), typeof(TSource), typeof(TAccumulate)), source.Expression, Expression.Constant(seed, typeof(TAccumulate)), Expression.Quote(func))); } public static TResult Aggregate(this IQueryable source, TAccumulate seed, Expression> func, Expression> selector) { LinqCheck.SourceAndFuncAndSelector(source, func, selector); return source.Provider.Execute( StaticCall( MakeGeneric(MethodBase.GetCurrentMethod(), typeof(TSource), typeof(TAccumulate), typeof(TResult)), source.Expression, Expression.Constant(seed, typeof(TAccumulate)), Expression.Quote(func), Expression.Quote(selector))); } public static bool All(this IQueryable source, Expression> predicate) { LinqCheck.SourceAndPredicate(source, predicate); return source.Provider.Execute( StaticCall( MakeGeneric(MethodBase.GetCurrentMethod(), typeof(TSource)), source.Expression, Expression.Quote(predicate))); } public static bool Any(this IQueryable source) { LinqCheck.Source(source); return source.Provider.Execute( StaticCall( MakeGeneric(MethodBase.GetCurrentMethod(), typeof(TSource)), source.Expression)); } public static bool Any(this IQueryable source, Expression> predicate) { LinqCheck.SourceAndPredicate(source, predicate); return source.Provider.Execute( StaticCall( MakeGeneric(MethodBase.GetCurrentMethod(), typeof(TSource)), source.Expression, Expression.Quote(predicate))); } public static IQueryable AsQueryable(this IEnumerable source) { if (source == null) { throw new ArgumentNullException("source"); } var queryable = source as IQueryable; if (queryable != null) { return queryable; } return new QueryableEnumerable(source); } public static IQueryable AsQueryable(this IEnumerable source) { if (source == null) { throw new ArgumentNullException("source"); } var queryable = source as IQueryable; if (queryable != null) { return queryable; } Type ienumerable; if (!source.GetType().IsGenericImplementationOf(out ienumerable, typeof(IEnumerable<>))) { throw new ArgumentException("source is not IEnumerable<>"); } return (IQueryable)Activator.CreateInstance( typeof(QueryableEnumerable<>).MakeGenericType(ienumerable.GetFirstGenericArgument()), source); } public static double Average(this IQueryable source) { LinqCheck.Source(source); return source.Provider.Execute( StaticCall( (MethodInfo)MethodBase.GetCurrentMethod(), source.Expression)); } public static double? Average(this IQueryable source) { LinqCheck.Source(source); return source.Provider.Execute( StaticCall( (MethodInfo)MethodBase.GetCurrentMethod(), source.Expression)); } public static double Average(this IQueryable source) { LinqCheck.Source(source); return source.Provider.Execute( StaticCall( (MethodInfo)MethodBase.GetCurrentMethod(), source.Expression)); } public static double? Average(this IQueryable source) { LinqCheck.Source(source); return source.Provider.Execute( StaticCall( (MethodInfo)MethodBase.GetCurrentMethod(), source.Expression)); } public static float Average(this IQueryable source) { LinqCheck.Source(source); return source.Provider.Execute( StaticCall( (MethodInfo)MethodBase.GetCurrentMethod(), source.Expression)); } public static float? Average(this IQueryable source) { LinqCheck.Source(source); return source.Provider.Execute( StaticCall( (MethodInfo)MethodBase.GetCurrentMethod(), source.Expression)); } public static double Average(this IQueryable source) { LinqCheck.Source(source); return source.Provider.Execute( StaticCall( (MethodInfo)MethodBase.GetCurrentMethod(), source.Expression)); } public static double? Average(this IQueryable source) { LinqCheck.Source(source); return source.Provider.Execute( StaticCall( (MethodInfo)MethodBase.GetCurrentMethod(), source.Expression)); } public static decimal Average(this IQueryable source) { LinqCheck.Source(source); return source.Provider.Execute( StaticCall( (MethodInfo)MethodBase.GetCurrentMethod(), source.Expression)); } public static decimal? Average(this IQueryable source) { LinqCheck.Source(source); return source.Provider.Execute( StaticCall( (MethodInfo)MethodBase.GetCurrentMethod(), source.Expression)); } public static double Average(this IQueryable source, Expression> selector) { LinqCheck.SourceAndSelector(source, selector); return source.Provider.Execute( StaticCall( MakeGeneric(MethodBase.GetCurrentMethod(), typeof(TSource)), source.Expression, Expression.Quote(selector))); } public static double? Average(this IQueryable source, Expression> selector) { LinqCheck.SourceAndSelector(source, selector); return source.Provider.Execute( StaticCall( MakeGeneric(MethodBase.GetCurrentMethod(), typeof(TSource)), source.Expression, Expression.Quote(selector))); } public static double Average(this IQueryable source, Expression> selector) { LinqCheck.SourceAndSelector(source, selector); return source.Provider.Execute( StaticCall( MakeGeneric(MethodBase.GetCurrentMethod(), typeof(TSource)), source.Expression, Expression.Quote(selector))); } public static double? Average(this IQueryable source, Expression> selector) { LinqCheck.SourceAndSelector(source, selector); return source.Provider.Execute( StaticCall( MakeGeneric(MethodBase.GetCurrentMethod(), typeof(TSource)), source.Expression, Expression.Quote(selector))); } public static float Average(this IQueryable source, Expression> selector) { LinqCheck.SourceAndSelector(source, selector); return source.Provider.Execute( StaticCall( MakeGeneric(MethodBase.GetCurrentMethod(), typeof(TSource)), source.Expression, Expression.Quote(selector))); } public static float? Average(this IQueryable source, Expression> selector) { LinqCheck.SourceAndSelector(source, selector); return source.Provider.Execute( StaticCall( MakeGeneric(MethodBase.GetCurrentMethod(), typeof(TSource)), source.Expression, Expression.Quote(selector))); } public static double Average(this IQueryable source, Expression> selector) { LinqCheck.SourceAndSelector(source, selector); return source.Provider.Execute( StaticCall( MakeGeneric(MethodBase.GetCurrentMethod(), typeof(TSource)), source.Expression, Expression.Quote(selector))); } public static double? Average(this IQueryable source, Expression> selector) { LinqCheck.SourceAndSelector(source, selector); return source.Provider.Execute( StaticCall( MakeGeneric(MethodBase.GetCurrentMethod(), typeof(TSource)), source.Expression, Expression.Quote(selector))); } public static decimal Average(this IQueryable source, Expression> selector) { LinqCheck.SourceAndSelector(source, selector); return source.Provider.Execute( StaticCall( MakeGeneric(MethodBase.GetCurrentMethod(), typeof(TSource)), source.Expression, Expression.Quote(selector))); } public static decimal? Average(this IQueryable source, Expression> selector) { LinqCheck.SourceAndSelector(source, selector); return source.Provider.Execute( StaticCall( MakeGeneric(MethodBase.GetCurrentMethod(), typeof(TSource)), source.Expression, Expression.Quote(selector))); } public static IQueryable Cast(this IQueryable source) { LinqCheck.Source(source); return (IQueryable)source.Provider.CreateQuery(StaticCall(MakeGeneric(MethodBase.GetCurrentMethod(), typeof(TResult)), source.Expression)); } public static IQueryable Concat(this IQueryable source1, IEnumerable source2) { LinqCheck.Source1AndSource2(source1, source2); return source1.Provider.CreateQuery( StaticCall( MakeGeneric(MethodBase.GetCurrentMethod(), typeof(TSource)), source1.Expression, Expression.Constant(source2, typeof(IEnumerable)))); } public static bool Contains(this IQueryable source, TSource item) { LinqCheck.Source(source); return source.Provider.Execute( StaticCall( MakeGeneric(MethodBase.GetCurrentMethod(), typeof(TSource)), source.Expression, Expression.Constant(item, typeof(TSource)))); } public static bool Contains(this IQueryable source, TSource item, IEqualityComparer comparer) { LinqCheck.Source(source); return source.Provider.Execute( StaticCall( MakeGeneric(MethodBase.GetCurrentMethod(), typeof(TSource)), source.Expression, Expression.Constant(item, typeof(TSource)), Expression.Constant(comparer, typeof(IEqualityComparer)))); } public static int Count(this IQueryable source) { LinqCheck.Source(source); return source.Execute(MethodBase.GetCurrentMethod()); } public static int Count(this IQueryable source, Expression> predicate) { LinqCheck.SourceAndPredicate(source, predicate); return source.Provider.Execute( StaticCall( MakeGeneric(MethodBase.GetCurrentMethod(), typeof(TSource)), source.Expression, Expression.Quote(predicate))); } public static IQueryable DefaultIfEmpty(this IQueryable source) { LinqCheck.Source(source); return source.Provider.CreateQuery( StaticCall( MakeGeneric(MethodBase.GetCurrentMethod(), typeof(TSource)), source.Expression)); } public static IQueryable DefaultIfEmpty(this IQueryable source, TSource defaultValue) { LinqCheck.Source(source); return source.Provider.CreateQuery( StaticCall( MakeGeneric(MethodBase.GetCurrentMethod(), typeof(TSource)), source.Expression, Expression.Constant(defaultValue, typeof(TSource)))); } public static IQueryable Distinct(this IQueryable source) { LinqCheck.Source(source); return source.Provider.CreateQuery( StaticCall( MakeGeneric(MethodBase.GetCurrentMethod(), typeof(TSource)), source.Expression)); } public static IQueryable Distinct(this IQueryable source, IEqualityComparer comparer) { LinqCheck.Source(source); return source.Provider.CreateQuery( StaticCall( MakeGeneric(MethodBase.GetCurrentMethod(), typeof(TSource)), source.Expression, Expression.Constant(comparer, typeof(IEqualityComparer)))); } public static TSource ElementAt(this IQueryable source, int index) { LinqCheck.Source(source); return source.Provider.Execute( StaticCall( MakeGeneric(MethodBase.GetCurrentMethod(), typeof(TSource)), source.Expression, Expression.Constant(index))); } public static TSource ElementAtOrDefault(this IQueryable source, int index) { LinqCheck.Source(source); return source.Provider.Execute( StaticCall( MakeGeneric(MethodBase.GetCurrentMethod(), typeof(TSource)), source.Expression, Expression.Constant(index))); } public static IQueryable Except(this IQueryable source1, IEnumerable source2) { LinqCheck.Source1AndSource2(source1, source2); return source1.Provider.CreateQuery( StaticCall( MakeGeneric(MethodBase.GetCurrentMethod(), typeof(TSource)), source1.Expression, Expression.Constant(source2, typeof(IEnumerable)))); } public static IQueryable Except(this IQueryable source1, IEnumerable source2, IEqualityComparer comparer) { LinqCheck.Source1AndSource2(source1, source2); return source1.Provider.CreateQuery( StaticCall( MakeGeneric(MethodBase.GetCurrentMethod(), typeof(TSource)), source1.Expression, Expression.Constant(source2, typeof(IEnumerable)), Expression.Constant(comparer, typeof(IEqualityComparer)))); } public static TSource First(this IQueryable source) { LinqCheck.Source(source); return source.Provider.Execute( StaticCall( MakeGeneric(MethodBase.GetCurrentMethod(), typeof(TSource)), source.Expression)); } public static TSource First(this IQueryable source, Expression> predicate) { LinqCheck.SourceAndPredicate(source, predicate); return source.Provider.Execute( StaticCall( MakeGeneric(MethodBase.GetCurrentMethod(), typeof(TSource)), source.Expression, Expression.Quote(predicate))); } public static TSource FirstOrDefault(this IQueryable source) { LinqCheck.Source(source); return source.Provider.Execute( StaticCall( MakeGeneric(MethodBase.GetCurrentMethod(), typeof(TSource)), source.Expression)); } public static TSource FirstOrDefault(this IQueryable source, Expression> predicate) { LinqCheck.SourceAndPredicate(source, predicate); return source.Provider.Execute( StaticCall( MakeGeneric(MethodBase.GetCurrentMethod(), typeof(TSource)), source.Expression, Expression.Quote(predicate))); } public static IQueryable> GroupBy(this IQueryable source, Expression> keySelector) { LinqCheck.SourceAndKeySelector(source, keySelector); return source.Provider.CreateQuery>( StaticCall( MakeGeneric(MethodBase.GetCurrentMethod(), typeof(TSource), typeof(TKey)), source.Expression, Expression.Quote(keySelector))); } public static IQueryable> GroupBy(this IQueryable source, Expression> keySelector, IEqualityComparer comparer) { LinqCheck.SourceAndKeySelector(source, keySelector); return source.Provider.CreateQuery>( StaticCall( MakeGeneric(MethodBase.GetCurrentMethod(), typeof(TSource), typeof(TKey)), source.Expression, Expression.Quote(keySelector), Expression.Constant(comparer, typeof(IEqualityComparer)))); } public static IQueryable> GroupBy(this IQueryable source, Expression> keySelector, Expression> elementSelector) { LinqCheck.SourceAndKeyElementSelectors(source, keySelector, elementSelector); return source.Provider.CreateQuery>( StaticCall( MakeGeneric(MethodBase.GetCurrentMethod(), typeof(TSource), typeof(TKey), typeof(TElement)), source.Expression, Expression.Quote(keySelector), Expression.Quote(elementSelector))); } public static IQueryable GroupBy(this IQueryable source, Expression> keySelector, Expression, TResult>> resultSelector) { LinqCheck.SourceAndKeyResultSelectors(source, keySelector, resultSelector); return source.Provider.CreateQuery( StaticCall( MakeGeneric(MethodBase.GetCurrentMethod(), typeof(TSource), typeof(TKey), typeof(TResult)), source.Expression, Expression.Quote(keySelector), Expression.Quote(resultSelector))); } public static IQueryable> GroupBy(this IQueryable source, Expression> keySelector, Expression> elementSelector, IEqualityComparer comparer) { LinqCheck.SourceAndKeyElementSelectors(source, keySelector, elementSelector); return source.Provider.CreateQuery>( StaticCall( MakeGeneric(MethodBase.GetCurrentMethod(), typeof(TSource), typeof(TKey), typeof(TElement)), source.Expression, Expression.Quote(keySelector), Expression.Quote(elementSelector), Expression.Constant(comparer, typeof(IEqualityComparer)))); } public static IQueryable GroupBy(this IQueryable source, Expression> keySelector, Expression> elementSelector, Expression, TResult>> resultSelector) { LinqCheck.GroupBySelectors(source, keySelector, elementSelector, resultSelector); return source.Provider.CreateQuery( StaticCall( MakeGeneric(MethodBase.GetCurrentMethod(), typeof(TSource), typeof(TKey), typeof(TElement), typeof(TResult)), source.Expression, Expression.Quote(keySelector), Expression.Quote(elementSelector), Expression.Quote(resultSelector))); } public static IQueryable GroupBy(this IQueryable source, Expression> keySelector, Expression, TResult>> resultSelector, IEqualityComparer comparer) { LinqCheck.SourceAndKeyResultSelectors(source, keySelector, resultSelector); return source.Provider.CreateQuery( StaticCall( MakeGeneric(MethodBase.GetCurrentMethod(), typeof(TSource), typeof(TKey), typeof(TResult)), source.Expression, Expression.Quote(keySelector), Expression.Quote(resultSelector), Expression.Constant(comparer, typeof(IEqualityComparer)))); } public static IQueryable GroupBy(this IQueryable source, Expression> keySelector, Expression> elementSelector, Expression, TResult>> resultSelector, IEqualityComparer comparer) { LinqCheck.GroupBySelectors(source, keySelector, elementSelector, resultSelector); return source.Provider.CreateQuery( StaticCall( MakeGeneric(MethodBase.GetCurrentMethod(), typeof(TSource), typeof(TKey), typeof(TElement), typeof(TResult)), source.Expression, Expression.Quote(keySelector), Expression.Quote(elementSelector), Expression.Quote(resultSelector), Expression.Constant(comparer, typeof(IEqualityComparer)))); } public static IQueryable GroupJoin(this IQueryable outer, IEnumerable inner, Expression> outerKeySelector, Expression> innerKeySelector, Expression, TResult>> resultSelector) { if (outer == null) { throw new ArgumentNullException("outer"); } if (inner == null) { throw new ArgumentNullException("inner"); } if (outerKeySelector == null) { throw new ArgumentNullException("outerKeySelector"); } if (innerKeySelector == null) { throw new ArgumentNullException("innerKeySelector"); } if (resultSelector == null) { throw new ArgumentNullException("resultSelector"); } return outer.Provider.CreateQuery( StaticCall( MakeGeneric(MethodBase.GetCurrentMethod(), typeof(TOuter), typeof(TInner), typeof(TKey), typeof(TResult)), outer.Expression, Expression.Constant(inner, typeof(IEnumerable)), Expression.Quote(outerKeySelector), Expression.Quote(innerKeySelector), Expression.Quote(resultSelector))); } public static IQueryable GroupJoin(this IQueryable outer, IEnumerable inner, Expression> outerKeySelector, Expression> innerKeySelector, Expression, TResult>> resultSelector, IEqualityComparer comparer) { if (outer == null) { throw new ArgumentNullException("outer"); } if (inner == null) { throw new ArgumentNullException("inner"); } if (outerKeySelector == null) { throw new ArgumentNullException("outerKeySelector"); } if (innerKeySelector == null) { throw new ArgumentNullException("innerKeySelector"); } if (resultSelector == null) { throw new ArgumentNullException("resultSelector"); } return outer.Provider.CreateQuery( StaticCall( MakeGeneric(MethodBase.GetCurrentMethod(), typeof(TOuter), typeof(TInner), typeof(TKey), typeof(TResult)), outer.Expression, Expression.Constant(inner, typeof(IEnumerable)), Expression.Quote(outerKeySelector), Expression.Quote(innerKeySelector), Expression.Quote(resultSelector), Expression.Constant(comparer))); } public static IQueryable Intersect(this IQueryable source1, IEnumerable source2) { LinqCheck.Source1AndSource2(source1, source2); return source1.Provider.CreateQuery( StaticCall( MakeGeneric(MethodBase.GetCurrentMethod(), typeof(TSource)), source1.Expression, Expression.Constant(source2, typeof(IEnumerable)))); } public static IQueryable Intersect(this IQueryable source1, IEnumerable source2, IEqualityComparer comparer) { LinqCheck.Source1AndSource2(source1, source2); return source1.Provider.CreateQuery( StaticCall( MakeGeneric(MethodBase.GetCurrentMethod(), typeof(TSource)), source1.Expression, Expression.Constant(source2, typeof(IEnumerable)), Expression.Constant(comparer, typeof(IEqualityComparer)))); } public static IQueryable Join(this IQueryable outer, IEnumerable inner, Expression> outerKeySelector, Expression> innerKeySelector, Expression> resultSelector) { LinqCheck.JoinSelectors(outer, inner, outerKeySelector, innerKeySelector, resultSelector); return outer.Provider.CreateQuery( StaticCall( MakeGeneric(MethodBase.GetCurrentMethod(), typeof(TOuter), typeof(TInner), typeof(TKey), typeof(TResult)), outer.Expression, Expression.Constant(inner, typeof(IEnumerable)), Expression.Quote(outerKeySelector), Expression.Quote(innerKeySelector), Expression.Quote(resultSelector))); } public static IQueryable Join(this IQueryable outer, IEnumerable inner, Expression> outerKeySelector, Expression> innerKeySelector, Expression> resultSelector, IEqualityComparer comparer) { LinqCheck.JoinSelectors(outer, inner, outerKeySelector, innerKeySelector, resultSelector); return outer.Provider.CreateQuery( StaticCall( MakeGeneric(MethodBase.GetCurrentMethod(), typeof(TOuter), typeof(TInner), typeof(TKey), typeof(TResult)), outer.Expression, Expression.Constant(inner, typeof(IEnumerable)), Expression.Quote(outerKeySelector), Expression.Quote(innerKeySelector), Expression.Quote(resultSelector), Expression.Constant(comparer, typeof(IEqualityComparer)))); } public static TSource Last(this IQueryable source) { LinqCheck.Source(source); return source.Provider.Execute( StaticCall( MakeGeneric(MethodBase.GetCurrentMethod(), typeof(TSource)), source.Expression)); } public static TSource Last(this IQueryable source, Expression> predicate) { LinqCheck.SourceAndPredicate(source, predicate); return source.Provider.Execute( StaticCall( MakeGeneric(MethodBase.GetCurrentMethod(), typeof(TSource)), source.Expression, Expression.Quote(predicate))); } public static TSource LastOrDefault(this IQueryable source) { LinqCheck.Source(source); return source.Provider.Execute( StaticCall( MakeGeneric(MethodBase.GetCurrentMethod(), typeof(TSource)), source.Expression)); } public static TSource LastOrDefault(this IQueryable source, Expression> predicate) { LinqCheck.SourceAndPredicate(source, predicate); return source.Provider.Execute( StaticCall( MakeGeneric(MethodBase.GetCurrentMethod(), typeof(TSource)), source.Expression, Expression.Quote(predicate))); } public static long LongCount(this IQueryable source) { LinqCheck.Source(source); return source.Execute(MethodBase.GetCurrentMethod()); } public static long LongCount(this IQueryable source, Expression> predicate) { LinqCheck.SourceAndPredicate(source, predicate); return source.Provider.Execute( StaticCall( MakeGeneric(MethodBase.GetCurrentMethod(), typeof(TSource)), source.Expression, Expression.Quote(predicate))); } public static TSource Max(this IQueryable source) { LinqCheck.Source(source); return source.Provider.Execute( StaticCall( MakeGeneric(MethodBase.GetCurrentMethod(), typeof(TSource)), source.Expression)); } public static TResult Max(this IQueryable source, Expression> selector) { LinqCheck.Source(source); return source.Provider.Execute( StaticCall( MakeGeneric(MethodBase.GetCurrentMethod(), typeof(TSource), typeof(TResult)), source.Expression, Expression.Quote(selector))); } public static TSource Min(this IQueryable source) { LinqCheck.Source(source); return source.Provider.Execute( StaticCall( MakeGeneric(MethodBase.GetCurrentMethod(), typeof(TSource)), source.Expression)); } public static TResult Min(this IQueryable source, Expression> selector) { LinqCheck.SourceAndSelector(source, selector); return source.Provider.Execute( StaticCall( MakeGeneric(MethodBase.GetCurrentMethod(), typeof(TSource), typeof(TResult)), source.Expression, Expression.Quote(selector))); } public static IQueryable OfType(this IQueryable source) { LinqCheck.Source(source); return (IQueryable)source.Provider.CreateQuery( StaticCall( MakeGeneric(MethodBase.GetCurrentMethod(), typeof(TResult)), source.Expression)); } public static IOrderedQueryable OrderBy(this IQueryable source, Expression> keySelector) { LinqCheck.SourceAndKeySelector(source, keySelector); return (IOrderedQueryable)source.Provider.CreateQuery( StaticCall( MakeGeneric(MethodBase.GetCurrentMethod(), typeof(TSource), typeof(TKey)), source.Expression, Expression.Quote(keySelector))); } public static IOrderedQueryable OrderBy(this IQueryable source, Expression> keySelector, IComparer comparer) { LinqCheck.SourceAndKeySelector(source, keySelector); return (IOrderedQueryable)source.Provider.CreateQuery( StaticCall( MakeGeneric(MethodBase.GetCurrentMethod(), typeof(TSource), typeof(TKey)), source.Expression, Expression.Quote(keySelector), Expression.Constant(comparer, typeof(IComparer)))); } public static IOrderedQueryable OrderByDescending(this IQueryable source, Expression> keySelector) { LinqCheck.SourceAndKeySelector(source, keySelector); return (IOrderedQueryable)source.Provider.CreateQuery( StaticCall( MakeGeneric(MethodBase.GetCurrentMethod(), typeof(TSource), typeof(TKey)), source.Expression, Expression.Quote(keySelector))); } public static IOrderedQueryable OrderByDescending(this IQueryable source, Expression> keySelector, IComparer comparer) { LinqCheck.SourceAndKeySelector(source, keySelector); return (IOrderedQueryable)source.Provider.CreateQuery( StaticCall( MakeGeneric(MethodBase.GetCurrentMethod(), typeof(TSource), typeof(TKey)), source.Expression, Expression.Quote(keySelector), Expression.Constant(comparer, typeof(IComparer)))); } public static IQueryable Reverse(this IQueryable source) { LinqCheck.Source(source); return source.Provider.CreateQuery( StaticCall( MakeGeneric(MethodBase.GetCurrentMethod(), typeof(TSource)), source.Expression)); } public static IQueryable Select(this IQueryable source, Expression> selector) { LinqCheck.SourceAndSelector(source, selector); return source.Provider.CreateQuery( StaticCall( MakeGeneric(MethodBase.GetCurrentMethod(), typeof(TSource), typeof(TResult)), source.Expression, Expression.Quote(selector))); } public static IQueryable Select(this IQueryable source, Expression> selector) { LinqCheck.SourceAndSelector(source, selector); return source.Provider.CreateQuery( StaticCall( MakeGeneric(MethodBase.GetCurrentMethod(), typeof(TSource), typeof(TResult)), source.Expression, Expression.Quote(selector))); } public static IQueryable SelectMany(this IQueryable source, Expression>> selector) { LinqCheck.SourceAndSelector(source, selector); return source.Provider.CreateQuery( StaticCall( MakeGeneric(MethodBase.GetCurrentMethod(), typeof(TSource), typeof(TResult)), source.Expression, Expression.Quote(selector))); } public static IQueryable SelectMany(this IQueryable source, Expression>> selector) { LinqCheck.SourceAndSelector(source, selector); return source.Provider.CreateQuery( StaticCall( MakeGeneric(MethodBase.GetCurrentMethod(), typeof(TSource), typeof(TResult)), source.Expression, Expression.Quote(selector))); } public static IQueryable SelectMany(this IQueryable source, Expression>> collectionSelector, Expression> resultSelector) { LinqCheck.SourceAndCollectionSelectorAndResultSelector(source, collectionSelector, resultSelector); return source.Provider.CreateQuery( StaticCall( MakeGeneric(MethodBase.GetCurrentMethod(), typeof(TSource), typeof(TCollection), typeof(TResult)), source.Expression, Expression.Quote(collectionSelector), Expression.Quote(resultSelector))); } public static IQueryable SelectMany(this IQueryable source, Expression>> collectionSelector, Expression> resultSelector) { LinqCheck.SourceAndCollectionSelectorAndResultSelector(source, collectionSelector, resultSelector); return source.Provider.CreateQuery( StaticCall( MakeGeneric(MethodBase.GetCurrentMethod(), typeof(TSource), typeof(TCollection), typeof(TResult)), source.Expression, Expression.Quote(collectionSelector), Expression.Quote(resultSelector))); } public static bool SequenceEqual(this IQueryable source1, IEnumerable source2) { LinqCheck.Source1AndSource2(source1, source2); return source1.Provider.Execute( StaticCall( MakeGeneric(MethodBase.GetCurrentMethod(), typeof(TSource)), source1.Expression, Expression.Constant(source2, typeof(IEnumerable)))); } public static bool SequenceEqual(this IQueryable source1, IEnumerable source2, IEqualityComparer comparer) { LinqCheck.Source1AndSource2(source1, source2); return source1.Provider.Execute( StaticCall( MakeGeneric(MethodBase.GetCurrentMethod(), typeof(TSource)), source1.Expression, Expression.Constant(source2, typeof(IEnumerable)), Expression.Constant(comparer, typeof(IEqualityComparer)))); } public static TSource Single(this IQueryable source) { LinqCheck.Source(source); return source.Provider.Execute( StaticCall( MakeGeneric(MethodBase.GetCurrentMethod(), typeof(TSource)), source.Expression)); } public static TSource Single(this IQueryable source, Expression> predicate) { LinqCheck.SourceAndPredicate(source, predicate); return source.Provider.Execute( StaticCall( MakeGeneric(MethodBase.GetCurrentMethod(), typeof(TSource)), source.Expression, Expression.Quote(predicate))); } public static TSource SingleOrDefault(this IQueryable source) { LinqCheck.Source(source); return source.Provider.Execute( StaticCall( MakeGeneric(MethodBase.GetCurrentMethod(), typeof(TSource)), source.Expression)); } public static TSource SingleOrDefault(this IQueryable source, Expression> predicate) { LinqCheck.SourceAndPredicate(source, predicate); return source.Provider.Execute( StaticCall( MakeGeneric(MethodBase.GetCurrentMethod(), typeof(TSource)), source.Expression, Expression.Quote(predicate))); } public static IQueryable Skip(this IQueryable source, int count) { LinqCheck.Source(source); return source.Provider.CreateQuery( StaticCall( MakeGeneric(MethodBase.GetCurrentMethod(), typeof(TSource)), source.Expression, Expression.Constant(count))); } public static IQueryable SkipWhile(this IQueryable source, Expression> predicate) { LinqCheck.SourceAndPredicate(source, predicate); return source.Provider.CreateQuery( StaticCall( MakeGeneric(MethodBase.GetCurrentMethod(), typeof(TSource)), source.Expression, Expression.Quote(predicate))); } public static IQueryable SkipWhile(this IQueryable source, Expression> predicate) { LinqCheck.SourceAndPredicate(source, predicate); return source.Provider.CreateQuery( StaticCall( MakeGeneric(MethodBase.GetCurrentMethod(), typeof(TSource)), source.Expression, Expression.Quote(predicate))); } public static int Sum(this IQueryable source, Expression> selector) { LinqCheck.SourceAndSelector(source, selector); return source.Provider.Execute( StaticCall( MakeGeneric(MethodBase.GetCurrentMethod(), typeof(TSource)), source.Expression, Expression.Quote(selector))); } public static int? Sum(this IQueryable source, Expression> selector) { LinqCheck.SourceAndSelector(source, selector); return source.Provider.Execute( StaticCall( MakeGeneric(MethodBase.GetCurrentMethod(), typeof(TSource)), source.Expression, Expression.Quote(selector))); } public static long Sum(this IQueryable source, Expression> selector) { LinqCheck.SourceAndSelector(source, selector); return source.Provider.Execute( StaticCall( MakeGeneric(MethodBase.GetCurrentMethod(), typeof(TSource)), source.Expression, Expression.Quote(selector))); } public static long? Sum(this IQueryable source, Expression> selector) { LinqCheck.SourceAndSelector(source, selector); return source.Provider.Execute( StaticCall( MakeGeneric(MethodBase.GetCurrentMethod(), typeof(TSource)), source.Expression, Expression.Quote(selector))); } public static float Sum(this IQueryable source, Expression> selector) { LinqCheck.SourceAndSelector(source, selector); return source.Provider.Execute( StaticCall( MakeGeneric(MethodBase.GetCurrentMethod(), typeof(TSource)), source.Expression, Expression.Quote(selector))); } public static float? Sum(this IQueryable source, Expression> selector) { LinqCheck.SourceAndSelector(source, selector); return source.Provider.Execute( StaticCall( MakeGeneric(MethodBase.GetCurrentMethod(), typeof(TSource)), source.Expression, Expression.Quote(selector))); } public static double Sum(this IQueryable source, Expression> selector) { LinqCheck.SourceAndSelector(source, selector); return source.Provider.Execute( StaticCall( MakeGeneric(MethodBase.GetCurrentMethod(), typeof(TSource)), source.Expression, Expression.Quote(selector))); } public static double? Sum(this IQueryable source, Expression> selector) { LinqCheck.SourceAndSelector(source, selector); return source.Provider.Execute( StaticCall( MakeGeneric(MethodBase.GetCurrentMethod(), typeof(TSource)), source.Expression, Expression.Quote(selector))); } public static decimal Sum(this IQueryable source, Expression> selector) { LinqCheck.SourceAndSelector(source, selector); return source.Provider.Execute( StaticCall( MakeGeneric(MethodBase.GetCurrentMethod(), typeof(TSource)), source.Expression, Expression.Quote(selector))); } public static decimal? Sum(this IQueryable source, Expression> selector) { LinqCheck.SourceAndSelector(source, selector); return source.Provider.Execute( StaticCall( MakeGeneric(MethodBase.GetCurrentMethod(), typeof(TSource)), source.Expression, Expression.Quote(selector))); } public static int Sum(this IQueryable source) { LinqCheck.Source(source); return source.Provider.Execute( StaticCall( (MethodInfo)MethodBase.GetCurrentMethod(), source.Expression)); } public static int? Sum(this IQueryable source) { LinqCheck.Source(source); return source.Provider.Execute( StaticCall( (MethodInfo)MethodBase.GetCurrentMethod(), source.Expression)); } public static long Sum(this IQueryable source) { LinqCheck.Source(source); return source.Provider.Execute( StaticCall( (MethodInfo)MethodBase.GetCurrentMethod(), source.Expression)); } public static long? Sum(this IQueryable source) { LinqCheck.Source(source); return source.Provider.Execute( StaticCall( (MethodInfo)MethodBase.GetCurrentMethod(), source.Expression)); } public static float Sum(this IQueryable source) { LinqCheck.Source(source); return source.Provider.Execute( StaticCall( (MethodInfo)MethodBase.GetCurrentMethod(), source.Expression)); } public static float? Sum(this IQueryable source) { LinqCheck.Source(source); return source.Provider.Execute( StaticCall( (MethodInfo)MethodBase.GetCurrentMethod(), source.Expression)); } public static double Sum(this IQueryable source) { LinqCheck.Source(source); return source.Provider.Execute( StaticCall( (MethodInfo)MethodBase.GetCurrentMethod(), source.Expression)); } public static double? Sum(this IQueryable source) { LinqCheck.Source(source); return source.Provider.Execute( StaticCall( (MethodInfo)MethodBase.GetCurrentMethod(), source.Expression)); } public static decimal Sum(this IQueryable source) { LinqCheck.Source(source); return source.Provider.Execute( StaticCall( (MethodInfo)MethodBase.GetCurrentMethod(), source.Expression)); } public static decimal? Sum(this IQueryable source) { LinqCheck.Source(source); return source.Provider.Execute( StaticCall( (MethodInfo)MethodBase.GetCurrentMethod(), source.Expression)); } public static IQueryable Take(this IQueryable source, int count) { LinqCheck.Source(source); return source.Provider.CreateQuery( StaticCall( MakeGeneric(MethodBase.GetCurrentMethod(), typeof(TSource)), source.Expression, Expression.Constant(count))); } public static IQueryable TakeWhile(this IQueryable source, Expression> predicate) { LinqCheck.SourceAndPredicate(source, predicate); return source.Provider.CreateQuery( StaticCall( MakeGeneric(MethodBase.GetCurrentMethod(), typeof(TSource)), source.Expression, Expression.Quote(predicate))); } public static IQueryable TakeWhile(this IQueryable source, Expression> predicate) { LinqCheck.SourceAndPredicate(source, predicate); return source.Provider.CreateQuery( StaticCall( MakeGeneric(MethodBase.GetCurrentMethod(), typeof(TSource)), source.Expression, Expression.Quote(predicate))); } public static IOrderedQueryable ThenBy(this IOrderedQueryable source, Expression> keySelector) { LinqCheck.SourceAndKeySelector(source, keySelector); return (IOrderedQueryable)source.Provider.CreateQuery( StaticCall( MakeGeneric(MethodBase.GetCurrentMethod(), typeof(TSource), typeof(TKey)), source.Expression, Expression.Quote(keySelector))); } public static IOrderedQueryable ThenBy(this IOrderedQueryable source, Expression> keySelector, IComparer comparer) { LinqCheck.SourceAndKeySelector(source, keySelector); return (IOrderedQueryable)source.Provider.CreateQuery( StaticCall( MakeGeneric(MethodBase.GetCurrentMethod(), typeof(TSource), typeof(TKey)), source.Expression, Expression.Quote(keySelector), Expression.Constant(comparer, typeof(IComparer)))); } public static IOrderedQueryable ThenByDescending(this IOrderedQueryable source, Expression> keySelector) { LinqCheck.SourceAndKeySelector(source, keySelector); return (IOrderedQueryable)source.Provider.CreateQuery( StaticCall( MakeGeneric(MethodBase.GetCurrentMethod(), typeof(TSource), typeof(TKey)), source.Expression, Expression.Quote(keySelector))); } public static IOrderedQueryable ThenByDescending(this IOrderedQueryable source, Expression> keySelector, IComparer comparer) { LinqCheck.SourceAndKeySelector(source, keySelector); return (IOrderedQueryable)source.Provider.CreateQuery( StaticCall( MakeGeneric(MethodBase.GetCurrentMethod(), typeof(TSource), typeof(TKey)), source.Expression, Expression.Quote(keySelector), Expression.Constant(comparer, typeof(IComparer)))); } public static IQueryable Union(this IQueryable source1, IEnumerable source2) { LinqCheck.Source1AndSource2(source1, source2); return source1.Provider.CreateQuery( StaticCall( MakeGeneric(MethodBase.GetCurrentMethod(), typeof(TSource)), source1.Expression, Expression.Constant(source2, typeof(IEnumerable)))); } public static IQueryable Union(this IQueryable source1, IEnumerable source2, IEqualityComparer comparer) { LinqCheck.Source1AndSource2(source1, source2); return source1.Provider.CreateQuery( StaticCall( MakeGeneric(MethodBase.GetCurrentMethod(), typeof(TSource)), source1.Expression, Expression.Constant(source2, typeof(IEnumerable)), Expression.Constant(comparer, typeof(IEqualityComparer)))); } public static IQueryable Where(this IQueryable source, Expression> predicate) { LinqCheck.SourceAndPredicate(source, predicate); return source.Provider.CreateQuery( StaticCall( MakeGeneric(MethodBase.GetCurrentMethod(), typeof(TSource)), source.Expression, Expression.Quote(predicate))); } public static IQueryable Where(this IQueryable source, Expression> predicate) { LinqCheck.SourceAndPredicate(source, predicate); return source.Provider.CreateQuery( StaticCall( MakeGeneric(MethodBase.GetCurrentMethod(), typeof(TSource)), source.Expression, Expression.Quote(predicate))); } private static TRet Execute(this IQueryable source, MethodBase current) { return source.Provider.Execute( StaticCall( MakeGeneric(current, typeof(TSource)), source.Expression)); } private static MethodInfo MakeGeneric(MethodBase method, params Type[] parameters) { return ((MethodInfo)method).MakeGenericMethod(parameters); } private static Expression StaticCall(MethodInfo method, params Expression[] expressions) { return Expression.Call(null, method, expressions); } } } #endif