using System; using System.Collections; using System.Collections.Generic; using System.Linq.Expressions; using System.Reflection; using System.Text; // Include Silverlight's managed resources #if SILVERLIGHT using System.Core; #endif //SILVERLIGHT namespace System.Linq { public interface IQueryable : IEnumerable { Expression Expression { get; } Type ElementType { get; } // the provider that created this query IQueryProvider Provider { get; } } public interface IQueryable : IEnumerable, IQueryable { } public interface IQueryProvider{ IQueryable CreateQuery(Expression expression); IQueryable CreateQuery(Expression expression); object Execute(Expression expression); TResult Execute(Expression expression); } public interface IOrderedQueryable : IQueryable { } public interface IOrderedQueryable : IQueryable, IOrderedQueryable { } public static class Queryable { #region Helper methods to obtain MethodInfo in a safe way private static MethodInfo GetMethodInfo(Func f, T1 unused1) { return f.Method; } private static MethodInfo GetMethodInfo(Func f, T1 unused1, T2 unused2) { return f.Method; } private static MethodInfo GetMethodInfo(Func f, T1 unused1, T2 unused2, T3 unused3) { return f.Method; } private static MethodInfo GetMethodInfo(Func f, T1 unused1, T2 unused2, T3 unused3, T4 unused4) { return f.Method; } private static MethodInfo GetMethodInfo(Func f, T1 unused1, T2 unused2, T3 unused3, T4 unused4, T5 unused5) { return f.Method; } private static MethodInfo GetMethodInfo(Func f, T1 unused1, T2 unused2, T3 unused3, T4 unused4, T5 unused5, T6 unused6) { return f.Method; } #endregion public static IQueryable AsQueryable(this IEnumerable source) { if (source == null) throw Error.ArgumentNull("source"); if (source is IQueryable) return (IQueryable)source; return new EnumerableQuery(source); } public static IQueryable AsQueryable(this IEnumerable source) { if (source == null) throw Error.ArgumentNull("source"); if (source is IQueryable) return (IQueryable)source; Type enumType = TypeHelper.FindGenericType(typeof(IEnumerable<>), source.GetType()); if (enumType == null) throw Error.ArgumentNotIEnumerableGeneric("source"); return EnumerableQuery.Create(enumType.GetGenericArguments()[0], source); } public static IQueryable Where(this IQueryable source, Expression> predicate) { if (source == null) throw Error.ArgumentNull("source"); if (predicate == null) throw Error.ArgumentNull("predicate"); return source.Provider.CreateQuery( Expression.Call( null, GetMethodInfo(Queryable.Where, source, predicate), new Expression[] { source.Expression, Expression.Quote(predicate) } )); } public static IQueryable Where(this IQueryable source, Expression> predicate) { if (source == null) throw Error.ArgumentNull("source"); if (predicate == null) throw Error.ArgumentNull("predicate"); return source.Provider.CreateQuery( Expression.Call( null, GetMethodInfo(Queryable.Where, source, predicate), new Expression[] { source.Expression, Expression.Quote(predicate) } )); } public static IQueryable OfType(this IQueryable source) { if (source == null) throw Error.ArgumentNull("source"); return source.Provider.CreateQuery( Expression.Call( null, GetMethodInfo(Queryable.OfType, source), new Expression[] { source.Expression } )); } public static IQueryable Cast(this IQueryable source) { if (source == null) throw Error.ArgumentNull("source"); return source.Provider.CreateQuery( Expression.Call( null, GetMethodInfo(Queryable.Cast, source), new Expression[] { source.Expression } )); } public static IQueryable Select(this IQueryable source, Expression> selector) { if (source == null) throw Error.ArgumentNull("source"); if (selector == null) throw Error.ArgumentNull("selector"); return source.Provider.CreateQuery( Expression.Call( null, GetMethodInfo(Queryable.Select, source, selector), new Expression[] { source.Expression, Expression.Quote(selector) } )); } public static IQueryable Select(this IQueryable source, Expression> selector) { if (source == null) throw Error.ArgumentNull("source"); if (selector == null) throw Error.ArgumentNull("selector"); return source.Provider.CreateQuery( Expression.Call( null, GetMethodInfo(Queryable.Select, source, selector), new Expression[] { source.Expression, Expression.Quote(selector) } )); } public static IQueryable SelectMany(this IQueryable source, Expression>> selector) { if (source == null) throw Error.ArgumentNull("source"); if (selector == null) throw Error.ArgumentNull("selector"); return source.Provider.CreateQuery( Expression.Call( null, GetMethodInfo(Queryable.SelectMany, source, selector), new Expression[] { source.Expression, Expression.Quote(selector) } )); } public static IQueryable SelectMany(this IQueryable source, Expression>> selector) { if (source == null) throw Error.ArgumentNull("source"); if (selector == null) throw Error.ArgumentNull("selector"); return source.Provider.CreateQuery( Expression.Call( null, GetMethodInfo(Queryable.SelectMany, source, selector), new Expression[] { source.Expression, Expression.Quote(selector) } )); } public static IQueryable SelectMany(this IQueryable source, Expression>> collectionSelector, Expression> resultSelector){ if (source == null) throw Error.ArgumentNull("source"); if (collectionSelector == null) throw Error.ArgumentNull("collectionSelector"); if (resultSelector == null) throw Error.ArgumentNull("resultSelector"); return source.Provider.CreateQuery( Expression.Call( null, GetMethodInfo(Queryable.SelectMany, source, collectionSelector, resultSelector), new Expression[] { source.Expression, Expression.Quote(collectionSelector), Expression.Quote(resultSelector) } )); } public static IQueryable SelectMany(this IQueryable source, Expression>> collectionSelector, Expression> resultSelector) { if (source == null) throw Error.ArgumentNull("source"); if (collectionSelector == null) throw Error.ArgumentNull("collectionSelector"); if (resultSelector == null) throw Error.ArgumentNull("resultSelector"); return source.Provider.CreateQuery( Expression.Call( null, GetMethodInfo(Queryable.SelectMany, source, collectionSelector, resultSelector), new Expression[] { source.Expression, Expression.Quote(collectionSelector), Expression.Quote(resultSelector) } )); } private static Expression GetSourceExpression(IEnumerable source) { IQueryable q = source as IQueryable; if (q != null) return q.Expression; return Expression.Constant(source, typeof(IEnumerable)); } public static IQueryable Join(this IQueryable outer, IEnumerable inner, Expression> outerKeySelector, Expression> innerKeySelector, Expression> resultSelector) { if (outer == null) throw Error.ArgumentNull("outer"); if (inner == null) throw Error.ArgumentNull("inner"); if (outerKeySelector == null) throw Error.ArgumentNull("outerKeySelector"); if (innerKeySelector == null) throw Error.ArgumentNull("innerKeySelector"); if (resultSelector == null) throw Error.ArgumentNull("resultSelector"); return outer.Provider.CreateQuery( Expression.Call( null, GetMethodInfo(Queryable.Join, outer, inner, outerKeySelector, innerKeySelector, resultSelector), new Expression[] { outer.Expression, GetSourceExpression(inner), 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) { if (outer == null) throw Error.ArgumentNull("outer"); if (inner == null) throw Error.ArgumentNull("inner"); if (outerKeySelector == null) throw Error.ArgumentNull("outerKeySelector"); if (innerKeySelector == null) throw Error.ArgumentNull("innerKeySelector"); if (resultSelector == null) throw Error.ArgumentNull("resultSelector"); return outer.Provider.CreateQuery( Expression.Call( null, GetMethodInfo(Queryable.Join, outer, inner, outerKeySelector, innerKeySelector, resultSelector, comparer), new Expression[] { outer.Expression, GetSourceExpression(inner), Expression.Quote(outerKeySelector), Expression.Quote(innerKeySelector), 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 Error.ArgumentNull("outer"); if (inner == null) throw Error.ArgumentNull("inner"); if (outerKeySelector == null) throw Error.ArgumentNull("outerKeySelector"); if (innerKeySelector == null) throw Error.ArgumentNull("innerKeySelector"); if (resultSelector == null) throw Error.ArgumentNull("resultSelector"); return outer.Provider.CreateQuery( Expression.Call( null, GetMethodInfo(Queryable.GroupJoin, outer, inner, outerKeySelector, innerKeySelector, resultSelector), new Expression[] { outer.Expression, GetSourceExpression(inner), 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 Error.ArgumentNull("outer"); if (inner == null) throw Error.ArgumentNull("inner"); if (outerKeySelector == null) throw Error.ArgumentNull("outerKeySelector"); if (innerKeySelector == null) throw Error.ArgumentNull("innerKeySelector"); if (resultSelector == null) throw Error.ArgumentNull("resultSelector"); return outer.Provider.CreateQuery( Expression.Call( null, GetMethodInfo(Queryable.GroupJoin, outer, inner, outerKeySelector, innerKeySelector, resultSelector, comparer), new Expression[] { outer.Expression, GetSourceExpression(inner), Expression.Quote(outerKeySelector), Expression.Quote(innerKeySelector), Expression.Quote(resultSelector), Expression.Constant(comparer, typeof(IEqualityComparer)) } )); } public static IOrderedQueryable OrderBy(this IQueryable source, Expression> keySelector) { if (source == null) throw Error.ArgumentNull("source"); if (keySelector == null) throw Error.ArgumentNull("keySelector"); return (IOrderedQueryable) source.Provider.CreateQuery( Expression.Call( null, GetMethodInfo(Queryable.OrderBy, source, keySelector), new Expression[] { source.Expression, Expression.Quote(keySelector) } )); } public static IOrderedQueryable OrderBy(this IQueryable source, Expression> keySelector, IComparer comparer) { if (source == null) throw Error.ArgumentNull("source"); if (keySelector == null) throw Error.ArgumentNull("keySelector"); return (IOrderedQueryable) source.Provider.CreateQuery( Expression.Call( null, GetMethodInfo(Queryable.OrderBy, source, keySelector, comparer), new Expression[] { source.Expression, Expression.Quote(keySelector), Expression.Constant(comparer, typeof(IComparer)) } )); } public static IOrderedQueryable OrderByDescending(this IQueryable source, Expression> keySelector) { if (source == null) throw Error.ArgumentNull("source"); if (keySelector == null) throw Error.ArgumentNull("keySelector"); return (IOrderedQueryable)source.Provider.CreateQuery( Expression.Call( null, GetMethodInfo(Queryable.OrderByDescending, source, keySelector), new Expression[] { source.Expression, Expression.Quote(keySelector) } )); } public static IOrderedQueryable OrderByDescending(this IQueryable source, Expression> keySelector, IComparer comparer) { if (source == null) throw Error.ArgumentNull("source"); if (keySelector == null) throw Error.ArgumentNull("keySelector"); return (IOrderedQueryable) source.Provider.CreateQuery( Expression.Call( null, GetMethodInfo(Queryable.OrderByDescending, source, keySelector, comparer), new Expression[] { source.Expression, Expression.Quote(keySelector), Expression.Constant(comparer, typeof(IComparer)) } )); } public static IOrderedQueryable ThenBy(this IOrderedQueryable source, Expression> keySelector) { if (source == null) throw Error.ArgumentNull("source"); if (keySelector == null) throw Error.ArgumentNull("keySelector"); return (IOrderedQueryable) source.Provider.CreateQuery( Expression.Call( null, GetMethodInfo(Queryable.ThenBy, source, keySelector), new Expression[] { source.Expression, Expression.Quote(keySelector) } )); } public static IOrderedQueryable ThenBy(this IOrderedQueryable source, Expression> keySelector, IComparer comparer) { if (source == null) throw Error.ArgumentNull("source"); if (keySelector == null) throw Error.ArgumentNull("keySelector"); return (IOrderedQueryable) source.Provider.CreateQuery( Expression.Call( null, GetMethodInfo(Queryable.ThenBy, source, keySelector, comparer), new Expression[] { source.Expression, Expression.Quote(keySelector), Expression.Constant(comparer, typeof(IComparer)) } )); } public static IOrderedQueryable ThenByDescending(this IOrderedQueryable source, Expression> keySelector) { if (source == null) throw Error.ArgumentNull("source"); if (keySelector == null) throw Error.ArgumentNull("keySelector"); return (IOrderedQueryable) source.Provider.CreateQuery( Expression.Call( null, GetMethodInfo(Queryable.ThenByDescending, source, keySelector), new Expression[] { source.Expression, Expression.Quote(keySelector) } )); } public static IOrderedQueryable ThenByDescending(this IOrderedQueryable source, Expression> keySelector, IComparer comparer) { if (source == null) throw Error.ArgumentNull("source"); if (keySelector == null) throw Error.ArgumentNull("keySelector"); return (IOrderedQueryable) source.Provider.CreateQuery( Expression.Call( null, GetMethodInfo(Queryable.ThenByDescending, source, keySelector, comparer), new Expression[] { source.Expression, Expression.Quote(keySelector), Expression.Constant(comparer, typeof(IComparer)) } )); } public static IQueryable Take(this IQueryable source, int count) { if (source == null) throw Error.ArgumentNull("source"); return source.Provider.CreateQuery( Expression.Call( null, GetMethodInfo(Queryable.Take, source, count), new Expression[] { source.Expression, Expression.Constant(count) } )); } public static IQueryable TakeWhile(this IQueryable source, Expression> predicate) { if (source == null) throw Error.ArgumentNull("source"); if (predicate == null) throw Error.ArgumentNull("predicate"); return source.Provider.CreateQuery( Expression.Call( null, GetMethodInfo(Queryable.TakeWhile, source, predicate), new Expression[] { source.Expression, Expression.Quote(predicate) } )); } public static IQueryable TakeWhile(this IQueryable source, Expression> predicate) { if (source == null) throw Error.ArgumentNull("source"); if (predicate == null) throw Error.ArgumentNull("predicate"); return source.Provider.CreateQuery( Expression.Call( null, GetMethodInfo(Queryable.TakeWhile, source, predicate), new Expression[] { source.Expression, Expression.Quote(predicate) } )); } public static IQueryable Skip(this IQueryable source, int count) { if (source == null) throw Error.ArgumentNull("source"); return source.Provider.CreateQuery( Expression.Call( null, GetMethodInfo(Queryable.Skip, source, count), new Expression[] { source.Expression, Expression.Constant(count) } )); } public static IQueryable SkipWhile(this IQueryable source, Expression> predicate) { if (source == null) throw Error.ArgumentNull("source"); if (predicate == null) throw Error.ArgumentNull("predicate"); return source.Provider.CreateQuery( Expression.Call( null, GetMethodInfo(Queryable.SkipWhile, source, predicate), new Expression[] { source.Expression, Expression.Quote(predicate) } )); } public static IQueryable SkipWhile(this IQueryable source, Expression> predicate) { if (source == null) throw Error.ArgumentNull("source"); if (predicate == null) throw Error.ArgumentNull("predicate"); return source.Provider.CreateQuery( Expression.Call( null, GetMethodInfo(Queryable.SkipWhile, source, predicate), new Expression[] { source.Expression, Expression.Quote(predicate) } )); } public static IQueryable> GroupBy(this IQueryable source, Expression> keySelector) { if (source == null) throw Error.ArgumentNull("source"); if (keySelector == null) throw Error.ArgumentNull("keySelector"); return source.Provider.CreateQuery>( Expression.Call( null, GetMethodInfo(Queryable.GroupBy, source, keySelector), new Expression[] { source.Expression, Expression.Quote(keySelector) } )); } public static IQueryable> GroupBy(this IQueryable source, Expression> keySelector, Expression> elementSelector) { if (source == null) throw Error.ArgumentNull("source"); if (keySelector == null) throw Error.ArgumentNull("keySelector"); if (elementSelector == null) throw Error.ArgumentNull("elementSelector"); return source.Provider.CreateQuery>( Expression.Call( null, GetMethodInfo(Queryable.GroupBy, source, keySelector, elementSelector), new Expression[] { source.Expression, Expression.Quote(keySelector), Expression.Quote(elementSelector) } )); } public static IQueryable> GroupBy(this IQueryable source, Expression> keySelector, IEqualityComparer comparer) { if (source == null) throw Error.ArgumentNull("source"); if (keySelector == null) throw Error.ArgumentNull("keySelector"); return source.Provider.CreateQuery>( Expression.Call( null, GetMethodInfo(Queryable.GroupBy, source, keySelector, comparer), new Expression[] { source.Expression, Expression.Quote(keySelector), Expression.Constant(comparer, typeof(IEqualityComparer)) } )); } public static IQueryable> GroupBy(this IQueryable source, Expression> keySelector, Expression> elementSelector, IEqualityComparer comparer) { if (source == null) throw Error.ArgumentNull("source"); if (keySelector == null) throw Error.ArgumentNull("keySelector"); if (elementSelector == null) throw Error.ArgumentNull("elementSelector"); return source.Provider.CreateQuery>( Expression.Call( null, GetMethodInfo(Queryable.GroupBy, source, keySelector, elementSelector, comparer), new Expression[] { 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) { if (source == null) throw Error.ArgumentNull("source"); if (keySelector == null) throw Error.ArgumentNull("keySelector"); if (elementSelector == null) throw Error.ArgumentNull("elementSelector"); if (resultSelector == null) throw Error.ArgumentNull("resultSelector"); return source.Provider.CreateQuery( Expression.Call( null, GetMethodInfo(Queryable.GroupBy, source, keySelector, elementSelector, resultSelector), new Expression[] { source.Expression, Expression.Quote(keySelector), Expression.Quote(elementSelector), Expression.Quote(resultSelector) } )); } public static IQueryable GroupBy(this IQueryable source, Expression> keySelector,Expression, TResult>> resultSelector) { if (source == null) throw Error.ArgumentNull("source"); if (keySelector == null) throw Error.ArgumentNull("keySelector"); if (resultSelector == null) throw Error.ArgumentNull("resultSelector"); return source.Provider.CreateQuery( Expression.Call( null, GetMethodInfo(Queryable.GroupBy, source, keySelector, resultSelector), new Expression[] { source.Expression, Expression.Quote(keySelector), Expression.Quote(resultSelector) } )); } public static IQueryable GroupBy(this IQueryable source, Expression> keySelector, Expression, TResult>> resultSelector, IEqualityComparer comparer) { if (source == null) throw Error.ArgumentNull("source"); if (keySelector == null) throw Error.ArgumentNull("keySelector"); if (resultSelector == null) throw Error.ArgumentNull("resultSelector"); return source.Provider.CreateQuery( Expression.Call( null, GetMethodInfo(Queryable.GroupBy, source, keySelector, resultSelector, comparer), new Expression[] { 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) { if (source == null) throw Error.ArgumentNull("source"); if (keySelector == null) throw Error.ArgumentNull("keySelector"); if (elementSelector == null) throw Error.ArgumentNull("elementSelector"); if (resultSelector == null) throw Error.ArgumentNull("resultSelector"); return source.Provider.CreateQuery( Expression.Call( null, GetMethodInfo(Queryable.GroupBy, source, keySelector, elementSelector, resultSelector, comparer), new Expression[] { source.Expression, Expression.Quote(keySelector), Expression.Quote(elementSelector), Expression.Quote(resultSelector), Expression.Constant(comparer, typeof(IEqualityComparer)) } )); } public static IQueryable Distinct(this IQueryable source) { if (source == null) throw Error.ArgumentNull("source"); return source.Provider.CreateQuery( Expression.Call( null, GetMethodInfo(Queryable.Distinct, source), new Expression[] { source.Expression } )); } public static IQueryable Distinct(this IQueryable source, IEqualityComparer comparer) { if (source == null) throw Error.ArgumentNull("source"); return source.Provider.CreateQuery( Expression.Call( null, GetMethodInfo(Queryable.Distinct, source, comparer), new Expression[] { source.Expression, Expression.Constant(comparer, typeof(IEqualityComparer)) } )); } public static IQueryable Concat(this IQueryable source1, IEnumerable source2) { if (source1 == null) throw Error.ArgumentNull("source1"); if (source2 == null) throw Error.ArgumentNull("source2"); return source1.Provider.CreateQuery( Expression.Call( null, GetMethodInfo(Queryable.Concat, source1, source2), new Expression[] { source1.Expression, GetSourceExpression(source2) } )); } public static IQueryable Zip(this IQueryable source1, IEnumerable source2, Expression> resultSelector) { if (source1 == null) throw Error.ArgumentNull("source1"); if (source2 == null) throw Error.ArgumentNull("source2"); if (resultSelector == null) throw Error.ArgumentNull("resultSelector"); return source1.Provider.CreateQuery( Expression.Call( null, GetMethodInfo(Queryable.Zip, source1, source2, resultSelector), new Expression[] { source1.Expression, GetSourceExpression(source2), Expression.Quote(resultSelector) } )); } public static IQueryable Union(this IQueryable source1, IEnumerable source2) { if (source1 == null) throw Error.ArgumentNull("source1"); if (source2 == null) throw Error.ArgumentNull("source2"); return source1.Provider.CreateQuery( Expression.Call( null, GetMethodInfo(Queryable.Union, source1, source2), new Expression[] { source1.Expression, GetSourceExpression(source2) } )); } public static IQueryable Union(this IQueryable source1, IEnumerable source2, IEqualityComparer comparer) { if (source1 == null) throw Error.ArgumentNull("source1"); if (source2 == null) throw Error.ArgumentNull("source2"); return source1.Provider.CreateQuery( Expression.Call( null, GetMethodInfo(Queryable.Union, source1, source2, comparer), new Expression[] { source1.Expression, GetSourceExpression(source2), Expression.Constant(comparer, typeof(IEqualityComparer)) } )); } public static IQueryable Intersect(this IQueryable source1, IEnumerable source2) { if (source1 == null) throw Error.ArgumentNull("source1"); if (source2 == null) throw Error.ArgumentNull("source2"); return source1.Provider.CreateQuery( Expression.Call( null, GetMethodInfo(Queryable.Intersect, source1, source2), new Expression[] { source1.Expression, GetSourceExpression(source2) } )); } public static IQueryable Intersect(this IQueryable source1, IEnumerable source2, IEqualityComparer comparer) { if (source1 == null) throw Error.ArgumentNull("source1"); if (source2 == null) throw Error.ArgumentNull("source2"); return source1.Provider.CreateQuery( Expression.Call( null, GetMethodInfo(Queryable.Intersect, source1, source2, comparer), new Expression[] { source1.Expression, GetSourceExpression(source2), Expression.Constant(comparer, typeof(IEqualityComparer)) } )); } public static IQueryable Except(this IQueryable source1, IEnumerable source2) { if (source1 == null) throw Error.ArgumentNull("source1"); if (source2 == null) throw Error.ArgumentNull("source2"); return source1.Provider.CreateQuery( Expression.Call( null, GetMethodInfo(Queryable.Except, source1, source2), new Expression[] { source1.Expression, GetSourceExpression(source2) } )); } public static IQueryable Except(this IQueryable source1, IEnumerable source2, IEqualityComparer comparer) { if (source1 == null) throw Error.ArgumentNull("source1"); if (source2 == null) throw Error.ArgumentNull("source2"); return source1.Provider.CreateQuery( Expression.Call( null, GetMethodInfo(Queryable.Except, source1, source2, comparer), new Expression[] { source1.Expression, GetSourceExpression(source2), Expression.Constant(comparer, typeof(IEqualityComparer)) } )); } public static TSource First(this IQueryable source) { if (source == null) throw Error.ArgumentNull("source"); return source.Provider.Execute( Expression.Call( null, GetMethodInfo(Queryable.First, source), new Expression[] { source.Expression } )); } public static TSource First(this IQueryable source, Expression> predicate) { if (source == null) throw Error.ArgumentNull("source"); if (predicate == null) throw Error.ArgumentNull("predicate"); return source.Provider.Execute( Expression.Call( null, GetMethodInfo(Queryable.First, source, predicate), new Expression[] { source.Expression, Expression.Quote(predicate) } )); } public static TSource FirstOrDefault(this IQueryable source) { if (source == null) throw Error.ArgumentNull("source"); return source.Provider.Execute( Expression.Call( null, GetMethodInfo(Queryable.FirstOrDefault, source), new Expression[] { source.Expression } )); } public static TSource FirstOrDefault(this IQueryable source, Expression> predicate) { if (source == null) throw Error.ArgumentNull("source"); if (predicate == null) throw Error.ArgumentNull("predicate"); return source.Provider.Execute( Expression.Call( null, GetMethodInfo(Queryable.FirstOrDefault, source, predicate), new Expression[] { source.Expression, Expression.Quote(predicate) } )); } public static TSource Last(this IQueryable source) { if (source == null) throw Error.ArgumentNull("source"); return source.Provider.Execute( Expression.Call( null, GetMethodInfo(Queryable.Last, source), new Expression[] { source.Expression } )); } public static TSource Last(this IQueryable source, Expression> predicate) { if (source == null) throw Error.ArgumentNull("source"); if (predicate == null) throw Error.ArgumentNull("predicate"); return source.Provider.Execute( Expression.Call( null, GetMethodInfo(Queryable.Last, source, predicate), new Expression[] { source.Expression, Expression.Quote(predicate) } )); } public static TSource LastOrDefault(this IQueryable source) { if (source == null) throw Error.ArgumentNull("source"); return source.Provider.Execute( Expression.Call( null, GetMethodInfo(Queryable.LastOrDefault, source), new Expression[] { source.Expression } )); } public static TSource LastOrDefault(this IQueryable source, Expression> predicate) { if (source == null) throw Error.ArgumentNull("source"); if (predicate == null) throw Error.ArgumentNull("predicate"); return source.Provider.Execute( Expression.Call( null, GetMethodInfo(Queryable.LastOrDefault, source, predicate), new Expression[] { source.Expression, Expression.Quote(predicate) } )); } public static TSource Single(this IQueryable source) { if (source == null) throw Error.ArgumentNull("source"); return source.Provider.Execute( Expression.Call( null, GetMethodInfo(Queryable.Single, source), new Expression[] { source.Expression } )); } public static TSource Single(this IQueryable source, Expression> predicate) { if (source == null) throw Error.ArgumentNull("source"); if (predicate == null) throw Error.ArgumentNull("predicate"); return source.Provider.Execute( Expression.Call( null, GetMethodInfo(Queryable.Single, source, predicate), new Expression[] { source.Expression, Expression.Quote(predicate) } )); } public static TSource SingleOrDefault(this IQueryable source) { if (source == null) throw Error.ArgumentNull("source"); return source.Provider.Execute( Expression.Call( null, GetMethodInfo(Queryable.SingleOrDefault, source), new Expression[] { source.Expression } )); } public static TSource SingleOrDefault(this IQueryable source, Expression> predicate) { if (source == null) throw Error.ArgumentNull("source"); if (predicate == null) throw Error.ArgumentNull("predicate"); return source.Provider.Execute( Expression.Call( null, GetMethodInfo(Queryable.SingleOrDefault, source, predicate), new Expression[] { source.Expression, Expression.Quote(predicate) } )); } public static TSource ElementAt(this IQueryable source, int index) { if (source == null) throw Error.ArgumentNull("source"); if (index < 0) throw Error.ArgumentOutOfRange("index"); return source.Provider.Execute( Expression.Call( null, GetMethodInfo(Queryable.ElementAt, source, index), new Expression[] { source.Expression, Expression.Constant(index) } )); } public static TSource ElementAtOrDefault(this IQueryable source, int index) { if (source == null) throw Error.ArgumentNull("source"); return source.Provider.Execute( Expression.Call( null, GetMethodInfo(Queryable.ElementAtOrDefault, source, index), new Expression[] { source.Expression, Expression.Constant(index) } )); } public static IQueryable DefaultIfEmpty(this IQueryable source) { if (source == null) throw Error.ArgumentNull("source"); return source.Provider.CreateQuery( Expression.Call( null, GetMethodInfo(Queryable.DefaultIfEmpty, source), new Expression[] { source.Expression } )); } public static IQueryable DefaultIfEmpty(this IQueryable source, TSource defaultValue) { if (source == null) throw Error.ArgumentNull("source"); return source.Provider.CreateQuery( Expression.Call( null, GetMethodInfo(Queryable.DefaultIfEmpty, source, defaultValue), new Expression[] { source.Expression, Expression.Constant(defaultValue, typeof(TSource)) } )); } public static bool Contains(this IQueryable source, TSource item) { if (source == null) throw Error.ArgumentNull("source"); return source.Provider.Execute( Expression.Call( null, GetMethodInfo(Queryable.Contains, source, item), new Expression[] { source.Expression, Expression.Constant(item, typeof(TSource)) } )); } public static bool Contains(this IQueryable source, TSource item, IEqualityComparer comparer) { if (source == null) throw Error.ArgumentNull("source"); return source.Provider.Execute( Expression.Call( null, GetMethodInfo(Queryable.Contains, source, item, comparer), new Expression[] { source.Expression, Expression.Constant(item, typeof(TSource)), Expression.Constant(comparer, typeof(IEqualityComparer)) } )); } public static IQueryable Reverse(this IQueryable source) { if (source == null) throw Error.ArgumentNull("source"); return source.Provider.CreateQuery( Expression.Call( null, GetMethodInfo(Queryable.Reverse, source), new Expression[] { source.Expression } )); } public static bool SequenceEqual(this IQueryable source1, IEnumerable source2) { if (source1 == null) throw Error.ArgumentNull("source1"); if (source2 == null) throw Error.ArgumentNull("source2"); return source1.Provider.Execute( Expression.Call( null, GetMethodInfo(Queryable.SequenceEqual, source1, source2), new Expression[] { source1.Expression, GetSourceExpression(source2) } )); } public static bool SequenceEqual(this IQueryable source1, IEnumerable source2, IEqualityComparer comparer) { if (source1 == null) throw Error.ArgumentNull("source1"); if (source2 == null) throw Error.ArgumentNull("source2"); return source1.Provider.Execute( Expression.Call( null, GetMethodInfo(Queryable.SequenceEqual, source1, source2, comparer), new Expression[] { source1.Expression, GetSourceExpression(source2), Expression.Constant(comparer, typeof(IEqualityComparer)) } )); } public static bool Any(this IQueryable source) { if (source == null) throw Error.ArgumentNull("source"); return source.Provider.Execute( Expression.Call( null, GetMethodInfo(Queryable.Any, source), new Expression[] { source.Expression } )); } public static bool Any(this IQueryable source, Expression> predicate) { if (source == null) throw Error.ArgumentNull("source"); if (predicate == null) throw Error.ArgumentNull("predicate"); return source.Provider.Execute( Expression.Call( null, GetMethodInfo(Queryable.Any, source, predicate), new Expression[] { source.Expression, Expression.Quote(predicate) } )); } public static bool All(this IQueryable source, Expression> predicate) { if (source == null) throw Error.ArgumentNull("source"); if (predicate == null) throw Error.ArgumentNull("predicate"); return source.Provider.Execute( Expression.Call( null, GetMethodInfo(Queryable.All, source, predicate), new Expression[] { source.Expression, Expression.Quote(predicate) } )); } public static int Count(this IQueryable source) { if (source == null) throw Error.ArgumentNull("source"); return source.Provider.Execute( Expression.Call( null, GetMethodInfo(Queryable.Count, source), new Expression[] { source.Expression } )); } public static int Count(this IQueryable source, Expression> predicate) { if (source == null) throw Error.ArgumentNull("source"); if (predicate == null) throw Error.ArgumentNull("predicate"); return source.Provider.Execute( Expression.Call( null, GetMethodInfo(Queryable.Count, source, predicate), new Expression[] { source.Expression, Expression.Quote(predicate) } )); } public static long LongCount(this IQueryable source) { if (source == null) throw Error.ArgumentNull("source"); return source.Provider.Execute( Expression.Call( null, GetMethodInfo(Queryable.LongCount, source), new Expression[] { source.Expression } )); } public static long LongCount(this IQueryable source, Expression> predicate) { if (source == null) throw Error.ArgumentNull("source"); if (predicate == null) throw Error.ArgumentNull("predicate"); return source.Provider.Execute( Expression.Call( null, GetMethodInfo(Queryable.LongCount, source, predicate), new Expression[] { source.Expression, Expression.Quote(predicate) } )); } public static TSource Min(this IQueryable source) { if (source == null) throw Error.ArgumentNull("source"); return source.Provider.Execute( Expression.Call( null, GetMethodInfo(Queryable.Min, source), new Expression[] { source.Expression } )); } public static TResult Min(this IQueryable source, Expression> selector) { if (source == null) throw Error.ArgumentNull("source"); if (selector == null) throw Error.ArgumentNull("selector"); return source.Provider.Execute( Expression.Call( null, GetMethodInfo(Queryable.Min, source, selector), new Expression[] { source.Expression, Expression.Quote(selector) } )); } public static TSource Max(this IQueryable source) { if (source == null) throw Error.ArgumentNull("source"); return source.Provider.Execute( Expression.Call( null, GetMethodInfo(Queryable.Max, source), new Expression[] { source.Expression } )); } public static TResult Max(this IQueryable source, Expression> selector) { if (source == null) throw Error.ArgumentNull("source"); if (selector == null) throw Error.ArgumentNull("selector"); return source.Provider.Execute( Expression.Call( null, GetMethodInfo(Queryable.Max, source, selector), new Expression[] { source.Expression, Expression.Quote(selector) } )); } public static int Sum(this IQueryable source) { if (source == null) throw Error.ArgumentNull("source"); return source.Provider.Execute( Expression.Call( null, GetMethodInfo(Queryable.Sum, source), new Expression[] { source.Expression } )); } public static int? Sum(this IQueryable source) { if (source == null) throw Error.ArgumentNull("source"); return source.Provider.Execute( Expression.Call( null, GetMethodInfo(Queryable.Sum, source), new Expression[] { source.Expression } )); } public static long Sum(this IQueryable source) { if (source == null) throw Error.ArgumentNull("source"); return source.Provider.Execute( Expression.Call( null, GetMethodInfo(Queryable.Sum, source), new Expression[] { source.Expression } )); } public static long? Sum(this IQueryable source) { if (source == null) throw Error.ArgumentNull("source"); return source.Provider.Execute( Expression.Call( null, GetMethodInfo(Queryable.Sum, source), new Expression[] { source.Expression } )); } public static float Sum(this IQueryable source) { if (source == null) throw Error.ArgumentNull("source"); return source.Provider.Execute( Expression.Call( null, GetMethodInfo(Queryable.Sum, source), new Expression[] { source.Expression } )); } public static float? Sum(this IQueryable source) { if (source == null) throw Error.ArgumentNull("source"); return source.Provider.Execute( Expression.Call( null, GetMethodInfo(Queryable.Sum, source), new Expression[] { source.Expression } )); } public static double Sum(this IQueryable source) { if (source == null) throw Error.ArgumentNull("source"); return source.Provider.Execute( Expression.Call( null, GetMethodInfo(Queryable.Sum, source), new Expression[] { source.Expression } )); } public static double? Sum(this IQueryable source) { if (source == null) throw Error.ArgumentNull("source"); return source.Provider.Execute( Expression.Call( null, GetMethodInfo(Queryable.Sum, source), new Expression[] { source.Expression } )); } public static decimal Sum(this IQueryable source) { if (source == null) throw Error.ArgumentNull("source"); return source.Provider.Execute( Expression.Call( null, GetMethodInfo(Queryable.Sum, source), new Expression[] { source.Expression } )); } public static decimal? Sum(this IQueryable source) { if (source == null) throw Error.ArgumentNull("source"); return source.Provider.Execute( Expression.Call( null, GetMethodInfo(Queryable.Sum, source), new Expression[] { source.Expression } )); } public static int Sum(this IQueryable source, Expression> selector) { if (source == null) throw Error.ArgumentNull("source"); if (selector == null) throw Error.ArgumentNull("selector"); return source.Provider.Execute( Expression.Call( null, GetMethodInfo(Queryable.Sum, source, selector), new Expression[] { source.Expression, Expression.Quote(selector) } )); } public static int? Sum(this IQueryable source, Expression> selector) { if (source == null) throw Error.ArgumentNull("source"); if (selector == null) throw Error.ArgumentNull("selector"); return source.Provider.Execute( Expression.Call( null, GetMethodInfo(Queryable.Sum, source, selector), new Expression[] { source.Expression, Expression.Quote(selector) } )); } public static long Sum(this IQueryable source, Expression> selector) { if (source == null) throw Error.ArgumentNull("source"); if (selector == null) throw Error.ArgumentNull("selector"); return source.Provider.Execute( Expression.Call( null, GetMethodInfo(Queryable.Sum, source, selector), new Expression[] { source.Expression, Expression.Quote(selector) } )); } public static long? Sum(this IQueryable source, Expression> selector) { if (source == null) throw Error.ArgumentNull("source"); if (selector == null) throw Error.ArgumentNull("selector"); return source.Provider.Execute( Expression.Call( null, GetMethodInfo(Queryable.Sum, source, selector), new Expression[] { source.Expression, Expression.Quote(selector) } )); } public static float Sum(this IQueryable source, Expression> selector) { if (source == null) throw Error.ArgumentNull("source"); if (selector == null) throw Error.ArgumentNull("selector"); return source.Provider.Execute( Expression.Call( null, GetMethodInfo(Queryable.Sum, source, selector), new Expression[] { source.Expression, Expression.Quote(selector) } )); } public static float? Sum(this IQueryable source, Expression> selector) { if (source == null) throw Error.ArgumentNull("source"); if (selector == null) throw Error.ArgumentNull("selector"); return source.Provider.Execute( Expression.Call( null, GetMethodInfo(Queryable.Sum, source, selector), new Expression[] { source.Expression, Expression.Quote(selector) } )); } public static double Sum(this IQueryable source, Expression> selector) { if (source == null) throw Error.ArgumentNull("source"); if (selector == null) throw Error.ArgumentNull("selector"); return source.Provider.Execute( Expression.Call( null, GetMethodInfo(Queryable.Sum, source, selector), new Expression[] { source.Expression, Expression.Quote(selector) } )); } public static double? Sum(this IQueryable source, Expression> selector) { if (source == null) throw Error.ArgumentNull("source"); if (selector == null) throw Error.ArgumentNull("selector"); return source.Provider.Execute( Expression.Call( null, GetMethodInfo(Queryable.Sum, source, selector), new Expression[] { source.Expression, Expression.Quote(selector) } )); } public static decimal Sum(this IQueryable source, Expression> selector) { if (source == null) throw Error.ArgumentNull("source"); if (selector == null) throw Error.ArgumentNull("selector"); return source.Provider.Execute( Expression.Call( null, GetMethodInfo(Queryable.Sum, source, selector), new Expression[] { source.Expression, Expression.Quote(selector) } )); } public static decimal? Sum(this IQueryable source, Expression> selector) { if (source == null) throw Error.ArgumentNull("source"); if (selector == null) throw Error.ArgumentNull("selector"); return source.Provider.Execute( Expression.Call( null, GetMethodInfo(Queryable.Sum, source, selector), new Expression[] { source.Expression, Expression.Quote(selector) } )); } public static double Average(this IQueryable source) { if (source == null) throw Error.ArgumentNull("source"); return source.Provider.Execute( Expression.Call( null, GetMethodInfo(Queryable.Average, source), new Expression[] { source.Expression } )); } public static double? Average(this IQueryable source) { if (source == null) throw Error.ArgumentNull("source"); return source.Provider.Execute( Expression.Call( null, GetMethodInfo(Queryable.Average, source), new Expression[] { source.Expression } )); } public static double Average(this IQueryable source) { if (source == null) throw Error.ArgumentNull("source"); return source.Provider.Execute( Expression.Call( null, GetMethodInfo(Queryable.Average, source), new Expression[] { source.Expression } )); } public static double? Average(this IQueryable source) { if (source == null) throw Error.ArgumentNull("source"); return source.Provider.Execute( Expression.Call( null, GetMethodInfo(Queryable.Average, source), new Expression[] { source.Expression } )); } public static float Average(this IQueryable source) { if (source == null) throw Error.ArgumentNull("source"); return source.Provider.Execute( Expression.Call( null, GetMethodInfo(Queryable.Average, source), new Expression[] { source.Expression } )); } public static float? Average(this IQueryable source) { if (source == null) throw Error.ArgumentNull("source"); return source.Provider.Execute( Expression.Call( null, GetMethodInfo(Queryable.Average, source), new Expression[] { source.Expression } )); } public static double Average(this IQueryable source) { if (source == null) throw Error.ArgumentNull("source"); return source.Provider.Execute( Expression.Call( null, GetMethodInfo(Queryable.Average, source), new Expression[] { source.Expression } )); } public static double? Average(this IQueryable source) { if (source == null) throw Error.ArgumentNull("source"); return source.Provider.Execute( Expression.Call( null, GetMethodInfo(Queryable.Average, source), new Expression[] { source.Expression } )); } public static decimal Average(this IQueryable source) { if (source == null) throw Error.ArgumentNull("source"); return source.Provider.Execute( Expression.Call( null, GetMethodInfo(Queryable.Average, source), new Expression[] { source.Expression } )); } public static decimal? Average(this IQueryable source) { if (source == null) throw Error.ArgumentNull("source"); return source.Provider.Execute( Expression.Call( null, GetMethodInfo(Queryable.Average, source), new Expression[] { source.Expression } )); } public static double Average(this IQueryable source, Expression> selector) { if (source == null) throw Error.ArgumentNull("source"); if (selector == null) throw Error.ArgumentNull("selector"); return source.Provider.Execute( Expression.Call( null, GetMethodInfo(Queryable.Average, source, selector), new Expression[] { source.Expression, Expression.Quote(selector) } )); } public static double? Average(this IQueryable source, Expression> selector) { if (source == null) throw Error.ArgumentNull("source"); if (selector == null) throw Error.ArgumentNull("selector"); return source.Provider.Execute( Expression.Call( null, GetMethodInfo(Queryable.Average, source, selector), new Expression[] { source.Expression, Expression.Quote(selector) } )); } public static float Average(this IQueryable source, Expression> selector) { if (source == null) throw Error.ArgumentNull("source"); if (selector == null) throw Error.ArgumentNull("selector"); return source.Provider.Execute( Expression.Call( null, GetMethodInfo(Queryable.Average, source, selector), new Expression[] { source.Expression, Expression.Quote(selector) } )); } public static float? Average(this IQueryable source, Expression> selector) { if (source == null) throw Error.ArgumentNull("source"); if (selector == null) throw Error.ArgumentNull("selector"); return source.Provider.Execute( Expression.Call( null, GetMethodInfo(Queryable.Average, source, selector), new Expression[] { source.Expression, Expression.Quote(selector) } )); } public static double Average(this IQueryable source, Expression> selector) { if (source == null) throw Error.ArgumentNull("source"); if (selector == null) throw Error.ArgumentNull("selector"); return source.Provider.Execute( Expression.Call( null, GetMethodInfo(Queryable.Average, source, selector), new Expression[] { source.Expression, Expression.Quote(selector) } )); } public static double? Average(this IQueryable source, Expression> selector) { if (source == null) throw Error.ArgumentNull("source"); if (selector == null) throw Error.ArgumentNull("selector"); return source.Provider.Execute( Expression.Call( null, GetMethodInfo(Queryable.Average, source, selector), new Expression[] { source.Expression, Expression.Quote(selector) } )); } public static double Average(this IQueryable source, Expression> selector) { if (source == null) throw Error.ArgumentNull("source"); if (selector == null) throw Error.ArgumentNull("selector"); return source.Provider.Execute( Expression.Call( null, GetMethodInfo(Queryable.Average, source, selector), new Expression[] { source.Expression, Expression.Quote(selector) } )); } public static double? Average(this IQueryable source, Expression> selector) { if (source == null) throw Error.ArgumentNull("source"); if (selector == null) throw Error.ArgumentNull("selector"); return source.Provider.Execute( Expression.Call( null, GetMethodInfo(Queryable.Average, source, selector), new Expression[] { source.Expression, Expression.Quote(selector) } )); } public static decimal Average(this IQueryable source, Expression> selector) { if (source == null) throw Error.ArgumentNull("source"); if (selector == null) throw Error.ArgumentNull("selector"); return source.Provider.Execute( Expression.Call( null, GetMethodInfo(Queryable.Average, source, selector), new Expression[] { source.Expression, Expression.Quote(selector) } )); } public static decimal? Average(this IQueryable source, Expression> selector) { if (source == null) throw Error.ArgumentNull("source"); if (selector == null) throw Error.ArgumentNull("selector"); return source.Provider.Execute( Expression.Call( null, GetMethodInfo(Queryable.Average, source, selector), new Expression[] { source.Expression, Expression.Quote(selector) } )); } public static TSource Aggregate(this IQueryable source, Expression> func) { if (source == null) throw Error.ArgumentNull("source"); if (func == null) throw Error.ArgumentNull("func"); return source.Provider.Execute( Expression.Call( null, GetMethodInfo(Queryable.Aggregate, source, func), new Expression[] { source.Expression, Expression.Quote(func) } )); } public static TAccumulate Aggregate(this IQueryable source, TAccumulate seed, Expression> func) { if (source == null) throw Error.ArgumentNull("source"); if (func == null) throw Error.ArgumentNull("func"); return source.Provider.Execute( Expression.Call( null, GetMethodInfo(Queryable.Aggregate, source, seed, func), new Expression[] { source.Expression, Expression.Constant(seed), Expression.Quote(func) } )); } public static TResult Aggregate(this IQueryable source, TAccumulate seed, Expression> func, Expression> selector) { if (source == null) throw Error.ArgumentNull("source"); if (func == null) throw Error.ArgumentNull("func"); if (selector == null) throw Error.ArgumentNull("selector"); return source.Provider.Execute( Expression.Call( null, GetMethodInfo(Queryable.Aggregate, source, seed, func, selector), new Expression[] { source.Expression, Expression.Constant(seed), Expression.Quote(func), Expression.Quote(selector) } )); } } }