5 // Marek Safar (marek.safar@gmail.com)
6 // Alejandro Serrano "Serras" (trupill@yahoo.es)
7 // Jb Evain (jbevain@novell.com)
8 // Andreas Noever (andreas.noever@gmail.com)
10 // Copyright (C) 2008 Novell, Inc (http://www.novell.com)
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 using System.Collections;
34 using System.Collections.Generic;
35 using System.Linq.Expressions;
36 using System.Reflection;
39 namespace System.Linq {
41 public static class Queryable {
43 static MethodInfo MakeGeneric (MethodBase method, params Type [] parameters)
45 return ((MethodInfo) method).MakeGenericMethod (parameters);
48 static Expression StaticCall (MethodInfo method, params Expression [] expressions)
50 return Expression.Call (null, method, expressions);
53 static TRet Execute<TRet, TSource> (this IQueryable<TSource> source, MethodBase current)
55 return source.Provider.Execute<TRet> (
57 MakeGeneric (current, typeof (TSource)),
63 public static TSource Aggregate<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, TSource, TSource>> func)
65 Check.SourceAndFunc (source, func);
66 return source.Provider.Execute<TSource> (
68 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
70 Expression.Quote (func)));
73 public static TAccumulate Aggregate<TSource, TAccumulate> (this IQueryable<TSource> source, TAccumulate seed, Expression<Func<TAccumulate, TSource, TAccumulate>> func)
75 Check.SourceAndFunc (source, func);
76 return source.Provider.Execute<TAccumulate> (
78 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource), typeof (TAccumulate)),
80 Expression.Constant (seed),
81 Expression.Quote (func)));
84 public static TResult Aggregate<TSource, TAccumulate, TResult> (this IQueryable<TSource> source, TAccumulate seed, Expression<Func<TAccumulate, TSource, TAccumulate>> func, Expression<Func<TAccumulate, TResult>> selector)
86 Check.SourceAndFuncAndSelector (source, func, selector);
87 return source.Provider.Execute<TResult> (
89 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource), typeof (TAccumulate), typeof (TResult)),
91 Expression.Constant (seed),
92 Expression.Quote (func),
93 Expression.Quote (selector)));
100 public static bool All<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, bool>> predicate)
102 Check.SourceAndPredicate (source, predicate);
104 return source.Provider.Execute<bool> (
106 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
108 Expression.Quote (predicate)));
115 public static bool Any<TSource> (this IQueryable<TSource> source)
117 Check.Source (source);
119 return source.Provider.Execute<bool> (
121 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
125 public static bool Any<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, bool>> predicate)
127 Check.SourceAndPredicate (source, predicate);
129 return source.Provider.Execute<bool> (
131 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
133 Expression.Quote (predicate)));
141 public static IQueryable<TElement> AsQueryable<TElement> (this IEnumerable<TElement> source)
143 var queryable = source as IQueryable<TElement>;
144 if (queryable != null)
147 return new QueryableEnumerable<TElement> (new ConstantExpression (source, typeof (IQueryable<TElement>)));
151 public static IQueryable AsQueryable (this IEnumerable source)
153 var queryable = source as IQueryable;
154 if (queryable != null)
157 throw new NotImplementedException ();
164 public static double Average (this IQueryable<int> source)
166 Check.Source (source);
168 return source.Provider.Execute<double>(
170 (MethodInfo) MethodBase.GetCurrentMethod (),
174 public static double? Average(this IQueryable<int?> source)
176 Check.Source (source);
178 return source.Provider.Execute<double?>(
180 (MethodInfo) MethodBase.GetCurrentMethod (),
184 public static double Average(this IQueryable<long> source)
186 Check.Source (source);
188 return source.Provider.Execute<double>(
190 (MethodInfo) MethodBase.GetCurrentMethod (),
194 public static double? Average(this IQueryable<long?> source)
196 Check.Source (source);
198 return source.Provider.Execute<double?>(
200 (MethodInfo) MethodBase.GetCurrentMethod (),
204 public static float Average(this IQueryable<float> source)
206 Check.Source (source);
208 return source.Provider.Execute<float>(
210 (MethodInfo) MethodBase.GetCurrentMethod (),
214 public static float? Average(this IQueryable<float?> source)
216 Check.Source (source);
218 return source.Provider.Execute<float?>(
220 (MethodInfo) MethodBase.GetCurrentMethod (),
224 public static double Average (this IQueryable<double> source)
226 Check.Source (source);
228 return source.Provider.Execute<double> (
230 (MethodInfo) MethodBase.GetCurrentMethod (),
234 public static double? Average (this IQueryable<double?> source)
236 Check.Source (source);
238 return source.Provider.Execute<double?> (
240 (MethodInfo) MethodBase.GetCurrentMethod (),
244 public static decimal Average(this IQueryable<decimal> source)
246 Check.Source (source);
248 return source.Provider.Execute<decimal>(
250 (MethodInfo) MethodBase.GetCurrentMethod (),
254 public static decimal? Average(this IQueryable<decimal?> source)
256 Check.Source (source);
258 return source.Provider.Execute<decimal?>(
260 (MethodInfo) MethodBase.GetCurrentMethod (),
264 public static double Average<TSource>(this IQueryable<TSource> source, Expression<Func<TSource, int>> selector)
266 Check.SourceAndSelector (source, selector);
268 return source.Provider.Execute<double>(
270 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
272 Expression.Quote (selector)));
275 public static double? Average<TSource>(this IQueryable<TSource> source, Expression<Func<TSource, int?>> selector)
277 Check.SourceAndSelector (source, selector);
279 return source.Provider.Execute<double?>(
281 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
283 Expression.Quote (selector)));
286 public static double Average<TSource>(this IQueryable<TSource> source, Expression<Func<TSource, long>> selector)
288 Check.SourceAndSelector (source, selector);
290 return source.Provider.Execute<double>(
292 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
294 Expression.Quote (selector)));
297 public static double? Average<TSource>(this IQueryable<TSource> source, Expression<Func<TSource, long?>> selector)
299 Check.SourceAndSelector (source, selector);
301 return source.Provider.Execute<double?>(
303 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
305 Expression.Quote (selector)));
308 public static float Average<TSource>(this IQueryable<TSource> source, Expression<Func<TSource, float>> selector)
310 Check.SourceAndSelector (source, selector);
312 return source.Provider.Execute<float>(
314 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
316 Expression.Quote (selector)));
319 public static float? Average<TSource>(this IQueryable<TSource> source, Expression<Func<TSource, float?>> selector)
321 Check.SourceAndSelector (source, selector);
323 return source.Provider.Execute<float?>(
325 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
327 Expression.Quote (selector)));
330 public static double Average<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, double>> selector)
332 Check.SourceAndSelector (source, selector);
334 return source.Provider.Execute<double> (
336 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
338 Expression.Quote (selector)));
341 public static double? Average<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, double?>> selector)
343 Check.SourceAndSelector (source, selector);
345 return source.Provider.Execute<double?> (
347 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
349 Expression.Quote (selector)));
352 public static decimal Average<TSource>(this IQueryable<TSource> source, Expression<Func<TSource, decimal>> selector)
354 Check.SourceAndSelector (source, selector);
356 return source.Provider.Execute<decimal>(
358 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
360 Expression.Quote (selector)));
363 public static decimal? Average<TSource>(this IQueryable<TSource> source, Expression<Func<TSource, decimal?>> selector)
365 Check.SourceAndSelector (source, selector);
367 return source.Provider.Execute<decimal?>(
369 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
371 Expression.Quote (selector)));
378 public static IQueryable<TResult> Cast<TResult> (this IQueryable source)
380 Check.Source (source);
382 return (IQueryable<TResult>) source.Provider.CreateQuery (
383 StaticCall (MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TResult)),
391 public static IQueryable<TSource> Concat<TSource> (this IQueryable<TSource> source1, IEnumerable<TSource> source2)
393 Check.Source1AndSource2 (source1, source2);
395 return source1.Provider.CreateQuery<TSource> (
397 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
399 Expression.Constant (source2)));
406 public static bool Contains<TSource> (this IQueryable<TSource> source, TSource item)
408 Check.Source (source);
410 return source.Provider.Execute<bool> (
412 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
414 Expression.Constant (item)));
417 public static bool Contains<TSource> (this IQueryable<TSource> source, TSource item, IEqualityComparer<TSource> comparer)
419 Check.Source (source);
421 return source.Provider.Execute<bool> (
423 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
425 Expression.Constant (item),
426 Expression.Constant (comparer)));
433 public static int Count<TSource> (this IQueryable<TSource> source)
435 Check.Source (source);
437 return source.Execute<int, TSource> (MethodBase.GetCurrentMethod ());
440 public static int Count<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, bool>> predicate)
442 Check.SourceAndPredicate (source, predicate);
444 return source.Provider.Execute<int> (
446 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
448 Expression.Quote (predicate)));
453 #region DefaultIfEmpty
455 public static IQueryable<TSource> DefaultIfEmpty<TSource> (this IQueryable<TSource> source)
457 Check.Source (source);
459 return source.Provider.CreateQuery<TSource> (
461 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
465 public static IQueryable<TSource> DefaultIfEmpty<TSource> (this IQueryable<TSource> source, TSource defaultValue)
467 Check.Source (source);
469 return source.Provider.CreateQuery<TSource> (
471 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
473 Expression.Constant (defaultValue)));
480 public static IQueryable<TSource> Distinct<TSource> (this IQueryable<TSource> source)
482 Check.Source (source);
484 return source.Provider.CreateQuery<TSource> (
486 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
490 public static IQueryable<TSource> Distinct<TSource> (this IQueryable<TSource> source, IEqualityComparer<TSource> comparer)
492 Check.Source (source);
494 return source.Provider.CreateQuery<TSource> (
496 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
498 Expression.Constant (comparer)));
505 public static TSource ElementAt<TSource> (this IQueryable<TSource> source, int index)
507 Check.Source (source);
509 return source.Provider.Execute<TSource> (
511 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
513 Expression.Constant (index)));
518 #region ElementAtOrDefault
520 public static TSource ElementAtOrDefault<TSource> (this IQueryable<TSource> source, int index)
522 Check.Source (source);
524 return source.Provider.Execute<TSource> (
526 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
528 Expression.Constant (index)));
535 public static IQueryable<TSource> Except<TSource> (this IQueryable<TSource> source1, IEnumerable<TSource> source2)
537 Check.Source1AndSource2 (source1, source2);
539 return source1.Provider.CreateQuery<TSource> (
541 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
543 Expression.Constant (source2)));
546 public static IQueryable<TSource> Except<TSource> (this IQueryable<TSource> source1, IEnumerable<TSource> source2, IEqualityComparer<TSource> comparer)
548 Check.Source1AndSource2 (source1, source2);
550 return source1.Provider.CreateQuery<TSource> (
552 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
554 Expression.Constant (source2),
555 Expression.Constant (comparer)));
562 public static TSource First<TSource> (this IQueryable<TSource> source)
564 Check.Source (source);
566 return source.Provider.Execute<TSource> (
568 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
572 public static TSource First<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, bool>> predicate)
574 Check.SourceAndPredicate (source, predicate);
576 return source.Provider.Execute<TSource> (
578 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
580 Expression.Quote (predicate)));
585 #region FirstOrDefault
587 public static TSource FirstOrDefault<TSource> (this IQueryable<TSource> source)
589 Check.Source (source);
591 return source.Provider.Execute<TSource> (
593 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
597 public static TSource FirstOrDefault<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, bool>> predicate)
599 Check.SourceAndPredicate (source, predicate);
601 return source.Provider.Execute<TSource> (
603 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
605 Expression.Quote (predicate)));
612 public static IQueryable<IGrouping<TKey, TSource>> GroupBy<TSource, TKey> (this IQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector)
614 Check.SourceAndKeySelector (source, keySelector);
616 return source.Provider.CreateQuery<IGrouping<TKey, TSource>> (
618 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource), typeof (TKey)),
620 Expression.Quote (keySelector)));
622 public static IQueryable<IGrouping<TKey, TSource>> GroupBy<TSource, TKey> (this IQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector, IEqualityComparer<TKey> comparer)
624 Check.SourceAndKeySelector (source, keySelector);
626 return source.Provider.CreateQuery<IGrouping<TKey, TSource>> (
628 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource), typeof (TKey)),
630 Expression.Quote (keySelector),
631 Expression.Constant (comparer)));
633 public static IQueryable<IGrouping<TKey, TElement>> GroupBy<TSource, TKey, TElement> (this IQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector, Expression<Func<TSource, TElement>> elementSelector)
635 Check.SourceAndKeyElementSelectors (source, keySelector, elementSelector);
637 return source.Provider.CreateQuery<IGrouping<TKey, TElement>>(
639 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource), typeof (TKey), typeof (TElement)),
641 Expression.Quote (keySelector),
642 Expression.Quote (elementSelector)));
644 public static IQueryable<TResult> GroupBy<TSource, TKey, TResult> (this IQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector, Expression<Func<TKey, IEnumerable<TSource>, TResult>> resultSelector)
646 Check.SourceAndKeyResultSelectors (source, keySelector, resultSelector);
648 return source.Provider.CreateQuery<TResult> (
650 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource), typeof (TKey), typeof (TResult)),
652 Expression.Quote (keySelector),
653 Expression.Quote (resultSelector)));
655 public static IQueryable<IGrouping<TKey, TElement>> GroupBy<TSource, TKey, TElement> (this IQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector, Expression<Func<TSource, TElement>> elementSelector, IEqualityComparer<TKey> comparer)
657 Check.SourceAndKeyElementSelectors (source, keySelector, elementSelector);
659 return source.Provider.CreateQuery<IGrouping<TKey, TElement>> (
661 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource), typeof (TKey), typeof (TElement)),
663 Expression.Quote (keySelector),
664 Expression.Quote (elementSelector),
665 Expression.Constant (comparer)));
667 public static IQueryable<TResult> GroupBy<TSource, TKey, TElement, TResult> (this IQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector, Expression<Func<TSource, TElement>> elementSelector, Expression<Func<TKey, IEnumerable<TElement>, TResult>> resultSelector)
669 Check.GroupBySelectors (source, keySelector, elementSelector, resultSelector);
671 return source.Provider.CreateQuery<TResult> (
673 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource), typeof (TKey), typeof (TElement), typeof (TResult)),
675 Expression.Quote (keySelector),
676 Expression.Quote (elementSelector),
677 Expression.Quote (resultSelector)));
680 public static IQueryable<TResult> GroupBy<TSource, TKey, TResult> (this IQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector, Expression<Func<TKey, IEnumerable<TSource>, TResult>> resultSelector, IEqualityComparer<TKey> comparer)
682 Check.SourceAndKeyResultSelectors (source, keySelector, resultSelector);
684 return source.Provider.CreateQuery<TResult> (
686 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource), typeof (TKey), typeof (TResult)),
688 Expression.Quote (keySelector),
689 Expression.Quote (resultSelector),
690 Expression.Constant (comparer)));
692 public static IQueryable<TResult> GroupBy<TSource, TKey, TElement, TResult> (this IQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector, Expression<Func<TSource, TElement>> elementSelector, Expression<Func<TKey, IEnumerable<TElement>, TResult>> resultSelector, IEqualityComparer<TKey> comparer)
694 Check.GroupBySelectors (source, keySelector, elementSelector, resultSelector);
696 return source.Provider.CreateQuery<TResult> (
698 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource), typeof (TKey), typeof (TElement), typeof (TResult)),
700 Expression.Quote (keySelector),
701 Expression.Quote (elementSelector),
702 Expression.Quote (resultSelector),
703 Expression.Constant (comparer)));
709 public static IQueryable<TResult> GroupJoin<TOuter, TInner, TKey, TResult> (this IQueryable<TOuter> outer, IEnumerable<TInner> inner, Expression<Func<TOuter, TKey>> outerKeySelector, Expression<Func<TInner, TKey>> innerKeySelector, Expression<Func<TOuter, IEnumerable<TInner>, TResult>> resultSelector)
712 throw new ArgumentNullException ("outer");
714 throw new ArgumentNullException ("inner");
715 if (outerKeySelector == null)
716 throw new ArgumentNullException ("outerKeySelector");
717 if (innerKeySelector == null)
718 throw new ArgumentNullException ("innerKeySelector");
719 if (resultSelector == null)
720 throw new ArgumentNullException ("resultSelector");
722 return outer.Provider.CreateQuery<TResult> (
724 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TOuter), typeof (TInner), typeof (TKey), typeof (TResult)),
726 Expression.Constant (inner),
727 Expression.Quote (outerKeySelector),
728 Expression.Quote (innerKeySelector),
729 Expression.Quote (resultSelector)));
732 public static IQueryable<TResult> GroupJoin<TOuter, TInner, TKey, TResult> (this IQueryable<TOuter> outer, IEnumerable<TInner> inner, Expression<Func<TOuter, TKey>> outerKeySelector, Expression<Func<TInner, TKey>> innerKeySelector, Expression<Func<TOuter, IEnumerable<TInner>, TResult>> resultSelector, IEqualityComparer<TKey> comparer)
735 throw new ArgumentNullException ("outer");
737 throw new ArgumentNullException ("inner");
738 if (outerKeySelector == null)
739 throw new ArgumentNullException ("outerKeySelector");
740 if (innerKeySelector == null)
741 throw new ArgumentNullException ("innerKeySelector");
742 if (resultSelector == null)
743 throw new ArgumentNullException ("resultSelector");
745 return outer.Provider.CreateQuery<TResult> (
747 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TOuter), typeof (TInner), typeof (TKey), typeof (TResult)),
749 Expression.Constant (inner),
750 Expression.Quote (outerKeySelector),
751 Expression.Quote (innerKeySelector),
752 Expression.Quote (resultSelector),
753 Expression.Constant (comparer)));
760 public static IQueryable<TSource> Intersect<TSource> (this IQueryable<TSource> source1, IEnumerable<TSource> source2)
762 Check.Source1AndSource2 (source1, source2);
764 return source1.Provider.CreateQuery<TSource> (
766 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
768 Expression.Constant (source2)));
771 public static IQueryable<TSource> Intersect<TSource> (this IQueryable<TSource> source1, IEnumerable<TSource> source2, IEqualityComparer<TSource> comparer)
773 Check.Source1AndSource2 (source1, source2);
775 return source1.Provider.CreateQuery<TSource> (
777 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
779 Expression.Constant (source2),
780 Expression.Constant (comparer)));
787 public static IQueryable<TResult> Join<TOuter, TInner, TKey, TResult> (this IQueryable<TOuter> outer, IEnumerable<TInner> inner, Expression<Func<TOuter, TKey>> outerKeySelector, Expression<Func<TInner, TKey>> innerKeySelector, Expression<Func<TOuter, TInner, TResult>> resultSelector)
789 Check.JoinSelectors (outer, inner, outerKeySelector, innerKeySelector, resultSelector);
791 return outer.Provider.CreateQuery<TResult> (
793 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TOuter), typeof (TInner), typeof (TKey), typeof (TResult)),
795 Expression.Constant (inner),
796 Expression.Quote (outerKeySelector),
797 Expression.Quote (innerKeySelector),
798 Expression.Quote (resultSelector)));
801 public static IQueryable<TResult> Join<TOuter, TInner, TKey, TResult> (this IQueryable<TOuter> outer, IEnumerable<TInner> inner, Expression<Func<TOuter, TKey>> outerKeySelector, Expression<Func<TInner, TKey>> innerKeySelector, Expression<Func<TOuter, TInner, TResult>> resultSelector, IEqualityComparer<TKey> comparer)
803 Check.JoinSelectors (outer, inner, outerKeySelector, innerKeySelector, resultSelector);
805 return outer.Provider.CreateQuery<TResult> (
807 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TOuter), typeof (TInner), typeof (TKey), typeof (TResult)),
809 Expression.Constant (inner),
810 Expression.Quote (outerKeySelector),
811 Expression.Quote (innerKeySelector),
812 Expression.Quote (resultSelector),
813 Expression.Constant (comparer)));
821 public static TSource Last<TSource> (this IQueryable<TSource> source)
823 Check.Source (source);
825 return source.Provider.Execute<TSource> (
827 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
831 public static TSource Last<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, bool>> predicate)
833 Check.SourceAndPredicate (source, predicate);
835 return source.Provider.Execute<TSource> (
837 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
839 Expression.Quote (predicate)));
844 #region LastOrDefault
846 public static TSource LastOrDefault<TSource> (this IQueryable<TSource> source)
848 Check.Source (source);
850 return source.Provider.Execute<TSource> (
852 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
856 public static TSource LastOrDefault<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, bool>> predicate)
858 Check.SourceAndPredicate (source, predicate);
860 return source.Provider.Execute<TSource> (
862 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
864 Expression.Quote (predicate)));
871 public static long LongCount<TSource> (this IQueryable<TSource> source)
873 Check.Source (source);
875 return source.Execute<long, TSource> (MethodBase.GetCurrentMethod ());
878 public static long LongCount<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, bool>> predicate)
880 Check.SourceAndPredicate (source, predicate);
882 return source.Provider.Execute<long> (
884 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
886 Expression.Quote (predicate)));
893 public static TSource Max<TSource> (this IQueryable<TSource> source)
895 Check.Source (source);
897 return source.Provider.Execute<TSource> (
899 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
903 public static TResult Max<TSource, TResult> (this IQueryable<TSource> source, Expression<Func<TSource, TResult>> selector)
905 Check.Source (source);
907 return source.Provider.Execute<TResult> (
909 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource), typeof (TResult)),
911 Expression.Quote (selector)));
919 public static TSource Min<TSource> (this IQueryable<TSource> source)
921 Check.Source (source);
923 return source.Provider.Execute<TSource> (
925 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
929 public static TResult Min<TSource, TResult> (this IQueryable<TSource> source, Expression<Func<TSource, TResult>> selector)
931 Check.SourceAndSelector (source, selector);
933 return source.Provider.Execute<TResult> (
935 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource), typeof (TResult)),
937 Expression.Quote (selector)));
945 public static IQueryable<TResult> OfType<TResult> (this IQueryable source)
947 Check.Source (source);
949 return (IQueryable<TResult>) source.Provider.CreateQuery (
951 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TResult)),
959 public static IOrderedQueryable<TSource> OrderBy<TSource, TKey> (this IQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector)
961 Check.SourceAndKeySelector (source, keySelector);
963 return (IOrderedQueryable<TSource>) source.Provider.CreateQuery<TSource> (
965 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource), typeof (TKey)),
967 Expression.Quote (keySelector)));
970 public static IOrderedQueryable<TSource> OrderBy<TSource, TKey> (this IQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector, IComparer<TKey> comparer)
972 Check.SourceAndKeySelector (source, keySelector);
974 return (IOrderedQueryable<TSource>) source.Provider.CreateQuery<TSource> (
976 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource), typeof (TKey)),
978 Expression.Quote (keySelector),
979 Expression.Constant (comparer)));
984 #region OrderByDescending
986 public static IOrderedQueryable<TSource> OrderByDescending<TSource, TKey> (this IQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector)
988 Check.SourceAndKeySelector (source, keySelector);
990 return (IOrderedQueryable<TSource>) source.Provider.CreateQuery<TSource> (
992 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource), typeof (TKey)),
994 Expression.Quote (keySelector)));
997 public static IOrderedQueryable<TSource> OrderByDescending<TSource, TKey> (this IQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector, IComparer<TKey> comparer)
999 Check.SourceAndKeySelector (source, keySelector);
1001 return (IOrderedQueryable<TSource>) source.Provider.CreateQuery<TSource> (
1003 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource), typeof (TKey)),
1005 Expression.Quote (keySelector),
1006 Expression.Constant (comparer)));
1013 public static IQueryable<TSource> Reverse<TSource> (this IQueryable<TSource> source)
1015 Check.Source (source);
1017 return source.Provider.CreateQuery<TSource> (
1019 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
1020 source.Expression));
1027 public static IQueryable<TResult> Select<TSource, TResult> (this IQueryable<TSource> source, Expression<Func<TSource, TResult>> selector)
1029 Check.SourceAndSelector (source, selector);
1031 return source.Provider.CreateQuery<TResult> (
1033 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource), typeof (TResult)),
1035 Expression.Quote (selector)));
1038 public static IQueryable<TResult> Select<TSource, TResult> (this IQueryable<TSource> source, Expression<Func<TSource, int, TResult>> selector)
1040 Check.SourceAndSelector (source, selector);
1042 return source.Provider.CreateQuery<TResult> (
1044 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource), typeof (TResult)),
1046 Expression.Quote (selector)));
1053 public static IQueryable<TResult> SelectMany<TSource, TResult> (this IQueryable<TSource> source, Expression<Func<TSource, IEnumerable<TResult>>> selector)
1055 Check.SourceAndSelector (source, selector);
1057 return source.Provider.CreateQuery<TResult> (
1059 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource), typeof (TResult)),
1061 Expression.Quote (selector)));
1064 public static IQueryable<TResult> SelectMany<TSource, TResult> (this IQueryable<TSource> source, Expression<Func<TSource, int, IEnumerable<TResult>>> selector)
1066 Check.SourceAndSelector (source, selector);
1068 return source.Provider.CreateQuery<TResult> (
1070 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource), typeof (TResult)),
1072 Expression.Quote (selector)));
1075 public static IQueryable<TResult> SelectMany<TSource, TCollection, TResult> (this IQueryable<TSource> source, Expression<Func<TSource, int, IEnumerable<TCollection>>> collectionSelector, Expression<Func<TSource, TCollection, TResult>> resultSelector)
1077 Check.SourceAndCollectionSelectorAndResultSelector (source, collectionSelector, resultSelector);
1079 return source.Provider.CreateQuery<TResult> (
1081 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource), typeof (TCollection), typeof (TResult)),
1083 Expression.Quote (collectionSelector),
1084 Expression.Quote (resultSelector)));
1087 public static IQueryable<TResult> SelectMany<TSource, TCollection, TResult> (this IQueryable<TSource> source, Expression<Func<TSource, IEnumerable<TCollection>>> collectionSelector, Expression<Func<TSource, TCollection, TResult>> resultSelector)
1089 Check.SourceAndCollectionSelectorAndResultSelector (source, collectionSelector, resultSelector);
1091 return source.Provider.CreateQuery<TResult> (
1093 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource), typeof (TCollection), typeof (TResult)),
1095 Expression.Quote (collectionSelector),
1096 Expression.Quote (resultSelector)));
1101 #region SequenceEqual
1103 public static bool SequenceEqual<TSource> (this IQueryable<TSource> source1, IEnumerable<TSource> source2)
1105 Check.Source1AndSource2 (source1, source2);
1107 return source1.Provider.Execute<bool> (
1109 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
1111 Expression.Constant (source2)));
1114 public static bool SequenceEqual<TSource> (this IQueryable<TSource> source1, IEnumerable<TSource> source2, IEqualityComparer<TSource> comparer)
1116 Check.Source1AndSource2 (source1, source2);
1118 return source1.Provider.Execute<bool> (
1120 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
1122 Expression.Constant (source2),
1123 Expression.Constant (comparer)));
1130 public static TSource Single<TSource> (this IQueryable<TSource> source)
1132 Check.Source (source);
1134 return source.Provider.Execute<TSource> (
1136 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
1137 source.Expression));
1140 public static TSource Single<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, bool>> predicate)
1142 Check.SourceAndPredicate (source, predicate);
1144 return source.Provider.Execute<TSource> (
1146 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
1148 Expression.Quote (predicate)));
1153 #region SingleOrDefault
1155 public static TSource SingleOrDefault<TSource> (this IQueryable<TSource> source)
1157 Check.Source (source);
1159 return source.Provider.Execute<TSource> (
1161 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
1162 source.Expression));
1165 public static TSource SingleOrDefault<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, bool>> predicate)
1167 Check.SourceAndPredicate (source, predicate);
1169 return source.Provider.Execute<TSource> (
1171 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
1173 Expression.Quote (predicate)));
1180 public static IQueryable<TSource> Skip<TSource> (this IQueryable<TSource> source, int count)
1182 Check.Source (source);
1184 return source.Provider.CreateQuery<TSource> (
1186 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
1188 Expression.Constant (count)));
1195 public static IQueryable<TSource> SkipWhile<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, bool>> predicate)
1197 Check.SourceAndPredicate (source, predicate);
1199 return source.Provider.CreateQuery<TSource> (
1201 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
1203 Expression.Quote (predicate)));
1206 public static IQueryable<TSource> SkipWhile<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, int, bool>> predicate)
1208 Check.SourceAndPredicate (source, predicate);
1210 return source.Provider.CreateQuery<TSource> (
1212 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
1214 Expression.Quote (predicate)));
1223 public static int Sum<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, int>> selector)
1225 Check.SourceAndSelector (source, selector);
1227 return source.Provider.Execute<int> (
1229 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
1231 Expression.Quote (selector)));
1234 public static int? Sum<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, int?>> selector)
1236 Check.SourceAndSelector (source, selector);
1238 return source.Provider.Execute<int?> (
1240 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
1242 Expression.Quote (selector)));
1245 public static long Sum<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, long>> selector)
1247 Check.SourceAndSelector (source, selector);
1249 return source.Provider.Execute<long> (
1251 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
1253 Expression.Quote (selector)));
1256 public static long? Sum<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, long?>> selector)
1258 Check.SourceAndSelector (source, selector);
1260 return source.Provider.Execute<long?> (
1262 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
1264 Expression.Quote (selector)));
1267 public static float Sum<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, float>> selector)
1269 Check.SourceAndSelector (source, selector);
1271 return source.Provider.Execute<float> (
1273 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
1275 Expression.Quote (selector)));
1278 public static float? Sum<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, float?>> selector)
1280 Check.SourceAndSelector (source, selector);
1282 return source.Provider.Execute<float?> (
1284 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
1286 Expression.Quote (selector)));
1289 public static double Sum<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, double>> selector)
1291 Check.SourceAndSelector (source, selector);
1293 return source.Provider.Execute<double> (
1295 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
1297 Expression.Quote (selector)));
1300 public static double? Sum<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, double?>> selector)
1302 Check.SourceAndSelector (source, selector);
1304 return source.Provider.Execute<double?> (
1306 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
1308 Expression.Quote (selector)));
1311 public static decimal Sum<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, decimal>> selector)
1313 Check.SourceAndSelector (source, selector);
1315 return source.Provider.Execute<decimal> (
1317 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
1319 Expression.Quote (selector)));
1322 public static decimal? Sum<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, decimal?>> selector)
1324 Check.SourceAndSelector (source, selector);
1326 return source.Provider.Execute<decimal?> (
1328 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
1330 Expression.Quote (selector)));
1333 public static int Sum (this IQueryable<int> source)
1335 Check.Source (source);
1337 return source.Provider.Execute<int> (
1339 (MethodInfo) MethodBase.GetCurrentMethod (),
1340 source.Expression));
1343 public static int? Sum (this IQueryable<int?> source)
1345 Check.Source (source);
1347 return source.Provider.Execute<int?> (
1349 (MethodInfo) MethodBase.GetCurrentMethod (),
1350 source.Expression));
1355 public static long Sum (this IQueryable<long> source)
1357 Check.Source (source);
1359 return source.Provider.Execute<long> (
1361 (MethodInfo) MethodBase.GetCurrentMethod (),
1362 source.Expression));
1367 public static long? Sum (this IQueryable<long?> source)
1369 Check.Source (source);
1371 return source.Provider.Execute<long?> (
1373 (MethodInfo) MethodBase.GetCurrentMethod (),
1374 source.Expression));
1377 public static float Sum (this IQueryable<float> source)
1379 Check.Source (source);
1381 return source.Provider.Execute<float> (
1383 (MethodInfo) MethodBase.GetCurrentMethod (),
1384 source.Expression));
1387 public static float? Sum (this IQueryable<float?> source)
1389 Check.Source (source);
1391 return source.Provider.Execute<float?> (
1393 (MethodInfo) MethodBase.GetCurrentMethod (),
1394 source.Expression));
1398 public static double Sum (this IQueryable<double> source)
1400 Check.Source (source);
1402 return source.Provider.Execute<double> (
1404 (MethodInfo) MethodBase.GetCurrentMethod (),
1405 source.Expression));
1410 public static double? Sum (this IQueryable<double?> source)
1412 Check.Source (source);
1414 return source.Provider.Execute<double?> (
1416 (MethodInfo) MethodBase.GetCurrentMethod (),
1417 source.Expression));
1421 public static decimal Sum (this IQueryable<decimal> source)
1423 Check.Source (source);
1425 return source.Provider.Execute<decimal> (
1427 (MethodInfo) MethodBase.GetCurrentMethod (),
1428 source.Expression));
1433 public static decimal? Sum (this IQueryable<decimal?> source)
1435 Check.Source (source);
1437 return source.Provider.Execute<decimal?> (
1439 (MethodInfo) MethodBase.GetCurrentMethod (),
1440 source.Expression));
1449 public static IQueryable<TSource> Take<TSource> (this IQueryable<TSource> source, int count)
1451 Check.Source (source);
1453 return source.Provider.CreateQuery<TSource> (
1455 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
1457 Expression.Constant (count)));
1464 public static IQueryable<TSource> TakeWhile<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, bool>> predicate)
1466 Check.SourceAndPredicate (source, predicate);
1468 return source.Provider.CreateQuery<TSource> (
1470 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
1472 Expression.Quote (predicate)));
1475 public static IQueryable<TSource> TakeWhile<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, int, bool>> predicate)
1477 Check.SourceAndPredicate (source, predicate);
1479 return source.Provider.CreateQuery<TSource> (
1481 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
1483 Expression.Quote (predicate)));
1490 public static IOrderedQueryable<TSource> ThenBy<TSource, TKey> (this IOrderedQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector)
1492 Check.SourceAndKeySelector (source, keySelector);
1494 return (IOrderedQueryable<TSource>) source.Provider.CreateQuery (
1496 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource), typeof (TKey)),
1498 Expression.Quote (keySelector)));
1501 public static IOrderedQueryable<TSource> ThenBy<TSource, TKey> (this IOrderedQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector, IComparer<TKey> comparer)
1503 Check.SourceAndKeySelector (source, keySelector);
1505 return (IOrderedQueryable<TSource>) source.Provider.CreateQuery (
1507 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource), typeof (TKey)),
1509 Expression.Quote (keySelector),
1510 Expression.Constant (comparer)));
1515 #region ThenByDescending
1517 public static IOrderedQueryable<TSource> ThenByDescending<TSource, TKey> (this IOrderedQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector)
1519 Check.SourceAndKeySelector (source, keySelector);
1521 return (IOrderedQueryable<TSource>) source.Provider.CreateQuery (
1523 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource), typeof (TKey)),
1525 Expression.Quote (keySelector)));
1528 public static IOrderedQueryable<TSource> ThenByDescending<TSource, TKey> (this IOrderedQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector, IComparer<TKey> comparer)
1530 Check.SourceAndKeySelector (source, keySelector);
1532 return (IOrderedQueryable<TSource>) source.Provider.CreateQuery (
1534 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource), typeof (TKey)),
1536 Expression.Quote (keySelector),
1537 Expression.Constant (comparer)));
1544 public static IQueryable<TSource> Union<TSource> (this IQueryable<TSource> source1, IEnumerable<TSource> source2)
1546 Check.Source1AndSource2 (source1, source2);
1548 return source1.Provider.CreateQuery<TSource> (
1550 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
1552 Expression.Constant (source2)));
1555 public static IQueryable<TSource> Union<TSource>(this IQueryable<TSource> source1, IEnumerable<TSource> source2, IEqualityComparer<TSource> comparer)
1557 Check.Source1AndSource2 (source1, source2);
1559 return source1.Provider.CreateQuery<TSource> (
1561 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
1563 Expression.Constant (source2),
1564 Expression.Constant (comparer)));
1572 public static IQueryable<TSource> Where<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, bool>> predicate)
1574 Check.SourceAndPredicate (source, predicate);
1576 return source.Provider.CreateQuery<TSource> (
1578 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
1580 Expression.Quote (predicate)));
1583 public static IQueryable<TSource> Where<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, int, bool>> predicate)
1585 Check.SourceAndPredicate (source, predicate);
1587 return source.Provider.CreateQuery<TSource> (
1589 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
1591 Expression.Quote (predicate)));