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)));
140 public static IQueryable<TElement> AsQueryable<TElement> (this IEnumerable<TElement> source)
143 throw new ArgumentNullException ("source");
145 var queryable = source as IQueryable<TElement>;
146 if (queryable != null)
149 return new QueryableEnumerable<TElement> (source);
153 public static IQueryable AsQueryable (this IEnumerable source)
156 throw new ArgumentNullException ("source");
158 var queryable = source as IQueryable;
159 if (queryable != null)
162 Type sourceType = source.GetType ();
163 if (!sourceType.IsGenericImplementationOf (typeof (IEnumerable<>)))
164 throw new ArgumentException ("source is not IEnumerable<>");
166 Type sourceArgType = sourceType.GetFirstGenericArgument ();
167 return (IQueryable) Activator.CreateInstance (typeof (QueryableEnumerable<>)
168 .MakeGenericType (sourceArgType), source);
175 public static double Average (this IQueryable<int> source)
177 Check.Source (source);
179 return source.Provider.Execute<double>(
181 (MethodInfo) MethodBase.GetCurrentMethod (),
185 public static double? Average(this IQueryable<int?> source)
187 Check.Source (source);
189 return source.Provider.Execute<double?>(
191 (MethodInfo) MethodBase.GetCurrentMethod (),
195 public static double Average(this IQueryable<long> source)
197 Check.Source (source);
199 return source.Provider.Execute<double>(
201 (MethodInfo) MethodBase.GetCurrentMethod (),
205 public static double? Average(this IQueryable<long?> source)
207 Check.Source (source);
209 return source.Provider.Execute<double?>(
211 (MethodInfo) MethodBase.GetCurrentMethod (),
215 public static float Average(this IQueryable<float> source)
217 Check.Source (source);
219 return source.Provider.Execute<float>(
221 (MethodInfo) MethodBase.GetCurrentMethod (),
225 public static float? Average(this IQueryable<float?> source)
227 Check.Source (source);
229 return source.Provider.Execute<float?>(
231 (MethodInfo) MethodBase.GetCurrentMethod (),
235 public static double Average (this IQueryable<double> source)
237 Check.Source (source);
239 return source.Provider.Execute<double> (
241 (MethodInfo) MethodBase.GetCurrentMethod (),
245 public static double? Average (this IQueryable<double?> source)
247 Check.Source (source);
249 return source.Provider.Execute<double?> (
251 (MethodInfo) MethodBase.GetCurrentMethod (),
255 public static decimal Average(this IQueryable<decimal> source)
257 Check.Source (source);
259 return source.Provider.Execute<decimal>(
261 (MethodInfo) MethodBase.GetCurrentMethod (),
265 public static decimal? Average(this IQueryable<decimal?> source)
267 Check.Source (source);
269 return source.Provider.Execute<decimal?>(
271 (MethodInfo) MethodBase.GetCurrentMethod (),
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, int?>> 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 double? Average<TSource>(this IQueryable<TSource> source, Expression<Func<TSource, long?>> selector)
310 Check.SourceAndSelector (source, selector);
312 return source.Provider.Execute<double?>(
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 float? Average<TSource>(this IQueryable<TSource> source, Expression<Func<TSource, float?>> selector)
332 Check.SourceAndSelector (source, selector);
334 return source.Provider.Execute<float?>(
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 double? Average<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, double?>> selector)
354 Check.SourceAndSelector (source, selector);
356 return source.Provider.Execute<double?> (
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)));
374 public static decimal? Average<TSource>(this IQueryable<TSource> source, Expression<Func<TSource, decimal?>> selector)
376 Check.SourceAndSelector (source, selector);
378 return source.Provider.Execute<decimal?>(
380 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
382 Expression.Quote (selector)));
389 public static IQueryable<TResult> Cast<TResult> (this IQueryable source)
391 Check.Source (source);
393 return (IQueryable<TResult>) source.Provider.CreateQuery (
394 StaticCall (MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TResult)),
402 public static IQueryable<TSource> Concat<TSource> (this IQueryable<TSource> source1, IEnumerable<TSource> source2)
404 Check.Source1AndSource2 (source1, source2);
406 return source1.Provider.CreateQuery<TSource> (
408 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
410 Expression.Constant (source2)));
417 public static bool Contains<TSource> (this IQueryable<TSource> source, TSource item)
419 Check.Source (source);
421 return source.Provider.Execute<bool> (
423 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
425 Expression.Constant (item)));
428 public static bool Contains<TSource> (this IQueryable<TSource> source, TSource item, IEqualityComparer<TSource> comparer)
430 Check.Source (source);
432 return source.Provider.Execute<bool> (
434 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
436 Expression.Constant (item),
437 Expression.Constant (comparer)));
444 public static int Count<TSource> (this IQueryable<TSource> source)
446 Check.Source (source);
448 return source.Execute<int, TSource> (MethodBase.GetCurrentMethod ());
451 public static int Count<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, bool>> predicate)
453 Check.SourceAndPredicate (source, predicate);
455 return source.Provider.Execute<int> (
457 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
459 Expression.Quote (predicate)));
464 #region DefaultIfEmpty
466 public static IQueryable<TSource> DefaultIfEmpty<TSource> (this IQueryable<TSource> source)
468 Check.Source (source);
470 return source.Provider.CreateQuery<TSource> (
472 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
476 public static IQueryable<TSource> DefaultIfEmpty<TSource> (this IQueryable<TSource> source, TSource defaultValue)
478 Check.Source (source);
480 return source.Provider.CreateQuery<TSource> (
482 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
484 Expression.Constant (defaultValue)));
491 public static IQueryable<TSource> Distinct<TSource> (this IQueryable<TSource> source)
493 Check.Source (source);
495 return source.Provider.CreateQuery<TSource> (
497 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
501 public static IQueryable<TSource> Distinct<TSource> (this IQueryable<TSource> source, IEqualityComparer<TSource> comparer)
503 Check.Source (source);
505 return source.Provider.CreateQuery<TSource> (
507 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
509 Expression.Constant (comparer)));
516 public static TSource ElementAt<TSource> (this IQueryable<TSource> source, int index)
518 Check.Source (source);
520 return source.Provider.Execute<TSource> (
522 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
524 Expression.Constant (index)));
529 #region ElementAtOrDefault
531 public static TSource ElementAtOrDefault<TSource> (this IQueryable<TSource> source, int index)
533 Check.Source (source);
535 return source.Provider.Execute<TSource> (
537 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
539 Expression.Constant (index)));
546 public static IQueryable<TSource> Except<TSource> (this IQueryable<TSource> source1, IEnumerable<TSource> source2)
548 Check.Source1AndSource2 (source1, source2);
550 return source1.Provider.CreateQuery<TSource> (
552 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
554 Expression.Constant (source2)));
557 public static IQueryable<TSource> Except<TSource> (this IQueryable<TSource> source1, IEnumerable<TSource> source2, IEqualityComparer<TSource> comparer)
559 Check.Source1AndSource2 (source1, source2);
561 return source1.Provider.CreateQuery<TSource> (
563 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
565 Expression.Constant (source2),
566 Expression.Constant (comparer)));
573 public static TSource First<TSource> (this IQueryable<TSource> source)
575 Check.Source (source);
577 return source.Provider.Execute<TSource> (
579 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
583 public static TSource First<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, bool>> predicate)
585 Check.SourceAndPredicate (source, predicate);
587 return source.Provider.Execute<TSource> (
589 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
591 Expression.Quote (predicate)));
596 #region FirstOrDefault
598 public static TSource FirstOrDefault<TSource> (this IQueryable<TSource> source)
600 Check.Source (source);
602 return source.Provider.Execute<TSource> (
604 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
608 public static TSource FirstOrDefault<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, bool>> predicate)
610 Check.SourceAndPredicate (source, predicate);
612 return source.Provider.Execute<TSource> (
614 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
616 Expression.Quote (predicate)));
623 public static IQueryable<IGrouping<TKey, TSource>> GroupBy<TSource, TKey> (this IQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector)
625 Check.SourceAndKeySelector (source, keySelector);
627 return source.Provider.CreateQuery<IGrouping<TKey, TSource>> (
629 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource), typeof (TKey)),
631 Expression.Quote (keySelector)));
633 public static IQueryable<IGrouping<TKey, TSource>> GroupBy<TSource, TKey> (this IQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector, IEqualityComparer<TKey> comparer)
635 Check.SourceAndKeySelector (source, keySelector);
637 return source.Provider.CreateQuery<IGrouping<TKey, TSource>> (
639 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource), typeof (TKey)),
641 Expression.Quote (keySelector),
642 Expression.Constant (comparer)));
644 public static IQueryable<IGrouping<TKey, TElement>> GroupBy<TSource, TKey, TElement> (this IQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector, Expression<Func<TSource, TElement>> elementSelector)
646 Check.SourceAndKeyElementSelectors (source, keySelector, elementSelector);
648 return source.Provider.CreateQuery<IGrouping<TKey, TElement>>(
650 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource), typeof (TKey), typeof (TElement)),
652 Expression.Quote (keySelector),
653 Expression.Quote (elementSelector)));
655 public static IQueryable<TResult> GroupBy<TSource, TKey, TResult> (this IQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector, Expression<Func<TKey, IEnumerable<TSource>, TResult>> resultSelector)
657 Check.SourceAndKeyResultSelectors (source, keySelector, resultSelector);
659 return source.Provider.CreateQuery<TResult> (
661 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource), typeof (TKey), typeof (TResult)),
663 Expression.Quote (keySelector),
664 Expression.Quote (resultSelector)));
666 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)
668 Check.SourceAndKeyElementSelectors (source, keySelector, elementSelector);
670 return source.Provider.CreateQuery<IGrouping<TKey, TElement>> (
672 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource), typeof (TKey), typeof (TElement)),
674 Expression.Quote (keySelector),
675 Expression.Quote (elementSelector),
676 Expression.Constant (comparer)));
678 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)
680 Check.GroupBySelectors (source, keySelector, elementSelector, resultSelector);
682 return source.Provider.CreateQuery<TResult> (
684 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource), typeof (TKey), typeof (TElement), typeof (TResult)),
686 Expression.Quote (keySelector),
687 Expression.Quote (elementSelector),
688 Expression.Quote (resultSelector)));
691 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)
693 Check.SourceAndKeyResultSelectors (source, keySelector, resultSelector);
695 return source.Provider.CreateQuery<TResult> (
697 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource), typeof (TKey), typeof (TResult)),
699 Expression.Quote (keySelector),
700 Expression.Quote (resultSelector),
701 Expression.Constant (comparer)));
703 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)
705 Check.GroupBySelectors (source, keySelector, elementSelector, resultSelector);
707 return source.Provider.CreateQuery<TResult> (
709 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource), typeof (TKey), typeof (TElement), typeof (TResult)),
711 Expression.Quote (keySelector),
712 Expression.Quote (elementSelector),
713 Expression.Quote (resultSelector),
714 Expression.Constant (comparer)));
720 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)
723 throw new ArgumentNullException ("outer");
725 throw new ArgumentNullException ("inner");
726 if (outerKeySelector == null)
727 throw new ArgumentNullException ("outerKeySelector");
728 if (innerKeySelector == null)
729 throw new ArgumentNullException ("innerKeySelector");
730 if (resultSelector == null)
731 throw new ArgumentNullException ("resultSelector");
733 return outer.Provider.CreateQuery<TResult> (
735 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TOuter), typeof (TInner), typeof (TKey), typeof (TResult)),
737 Expression.Constant (inner),
738 Expression.Quote (outerKeySelector),
739 Expression.Quote (innerKeySelector),
740 Expression.Quote (resultSelector)));
743 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)
746 throw new ArgumentNullException ("outer");
748 throw new ArgumentNullException ("inner");
749 if (outerKeySelector == null)
750 throw new ArgumentNullException ("outerKeySelector");
751 if (innerKeySelector == null)
752 throw new ArgumentNullException ("innerKeySelector");
753 if (resultSelector == null)
754 throw new ArgumentNullException ("resultSelector");
756 return outer.Provider.CreateQuery<TResult> (
758 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TOuter), typeof (TInner), typeof (TKey), typeof (TResult)),
760 Expression.Constant (inner),
761 Expression.Quote (outerKeySelector),
762 Expression.Quote (innerKeySelector),
763 Expression.Quote (resultSelector),
764 Expression.Constant (comparer)));
771 public static IQueryable<TSource> Intersect<TSource> (this IQueryable<TSource> source1, IEnumerable<TSource> source2)
773 Check.Source1AndSource2 (source1, source2);
775 return source1.Provider.CreateQuery<TSource> (
777 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
779 Expression.Constant (source2)));
782 public static IQueryable<TSource> Intersect<TSource> (this IQueryable<TSource> source1, IEnumerable<TSource> source2, IEqualityComparer<TSource> comparer)
784 Check.Source1AndSource2 (source1, source2);
786 return source1.Provider.CreateQuery<TSource> (
788 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
790 Expression.Constant (source2),
791 Expression.Constant (comparer)));
798 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)
800 Check.JoinSelectors (outer, inner, outerKeySelector, innerKeySelector, resultSelector);
802 return outer.Provider.CreateQuery<TResult> (
804 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TOuter), typeof (TInner), typeof (TKey), typeof (TResult)),
806 Expression.Constant (inner),
807 Expression.Quote (outerKeySelector),
808 Expression.Quote (innerKeySelector),
809 Expression.Quote (resultSelector)));
812 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)
814 Check.JoinSelectors (outer, inner, outerKeySelector, innerKeySelector, resultSelector);
816 return outer.Provider.CreateQuery<TResult> (
818 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TOuter), typeof (TInner), typeof (TKey), typeof (TResult)),
820 Expression.Constant (inner),
821 Expression.Quote (outerKeySelector),
822 Expression.Quote (innerKeySelector),
823 Expression.Quote (resultSelector),
824 Expression.Constant (comparer)));
832 public static TSource Last<TSource> (this IQueryable<TSource> source)
834 Check.Source (source);
836 return source.Provider.Execute<TSource> (
838 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
842 public static TSource Last<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, bool>> predicate)
844 Check.SourceAndPredicate (source, predicate);
846 return source.Provider.Execute<TSource> (
848 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
850 Expression.Quote (predicate)));
855 #region LastOrDefault
857 public static TSource LastOrDefault<TSource> (this IQueryable<TSource> source)
859 Check.Source (source);
861 return source.Provider.Execute<TSource> (
863 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
867 public static TSource LastOrDefault<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, bool>> predicate)
869 Check.SourceAndPredicate (source, predicate);
871 return source.Provider.Execute<TSource> (
873 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
875 Expression.Quote (predicate)));
882 public static long LongCount<TSource> (this IQueryable<TSource> source)
884 Check.Source (source);
886 return source.Execute<long, TSource> (MethodBase.GetCurrentMethod ());
889 public static long LongCount<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, bool>> predicate)
891 Check.SourceAndPredicate (source, predicate);
893 return source.Provider.Execute<long> (
895 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
897 Expression.Quote (predicate)));
904 public static TSource Max<TSource> (this IQueryable<TSource> source)
906 Check.Source (source);
908 return source.Provider.Execute<TSource> (
910 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
914 public static TResult Max<TSource, TResult> (this IQueryable<TSource> source, Expression<Func<TSource, TResult>> selector)
916 Check.Source (source);
918 return source.Provider.Execute<TResult> (
920 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource), typeof (TResult)),
922 Expression.Quote (selector)));
930 public static TSource Min<TSource> (this IQueryable<TSource> source)
932 Check.Source (source);
934 return source.Provider.Execute<TSource> (
936 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
940 public static TResult Min<TSource, TResult> (this IQueryable<TSource> source, Expression<Func<TSource, TResult>> selector)
942 Check.SourceAndSelector (source, selector);
944 return source.Provider.Execute<TResult> (
946 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource), typeof (TResult)),
948 Expression.Quote (selector)));
956 public static IQueryable<TResult> OfType<TResult> (this IQueryable source)
958 Check.Source (source);
960 return (IQueryable<TResult>) source.Provider.CreateQuery (
962 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TResult)),
970 public static IOrderedQueryable<TSource> OrderBy<TSource, TKey> (this IQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector)
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)));
981 public static IOrderedQueryable<TSource> OrderBy<TSource, TKey> (this IQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector, IComparer<TKey> comparer)
983 Check.SourceAndKeySelector (source, keySelector);
985 return (IOrderedQueryable<TSource>) source.Provider.CreateQuery<TSource> (
987 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource), typeof (TKey)),
989 Expression.Quote (keySelector),
990 Expression.Constant (comparer)));
995 #region OrderByDescending
997 public static IOrderedQueryable<TSource> OrderByDescending<TSource, TKey> (this IQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector)
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)));
1008 public static IOrderedQueryable<TSource> OrderByDescending<TSource, TKey> (this IQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector, IComparer<TKey> comparer)
1010 Check.SourceAndKeySelector (source, keySelector);
1012 return (IOrderedQueryable<TSource>) source.Provider.CreateQuery<TSource> (
1014 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource), typeof (TKey)),
1016 Expression.Quote (keySelector),
1017 Expression.Constant (comparer)));
1024 public static IQueryable<TSource> Reverse<TSource> (this IQueryable<TSource> source)
1026 Check.Source (source);
1028 return source.Provider.CreateQuery<TSource> (
1030 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
1031 source.Expression));
1038 public static IQueryable<TResult> Select<TSource, TResult> (this IQueryable<TSource> source, Expression<Func<TSource, 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)));
1049 public static IQueryable<TResult> Select<TSource, TResult> (this IQueryable<TSource> source, Expression<Func<TSource, int, TResult>> selector)
1051 Check.SourceAndSelector (source, selector);
1053 return source.Provider.CreateQuery<TResult> (
1055 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource), typeof (TResult)),
1057 Expression.Quote (selector)));
1064 public static IQueryable<TResult> SelectMany<TSource, TResult> (this IQueryable<TSource> source, Expression<Func<TSource, 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, TResult> (this IQueryable<TSource> source, Expression<Func<TSource, int, IEnumerable<TResult>>> selector)
1077 Check.SourceAndSelector (source, selector);
1079 return source.Provider.CreateQuery<TResult> (
1081 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource), typeof (TResult)),
1083 Expression.Quote (selector)));
1086 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)
1088 Check.SourceAndCollectionSelectorAndResultSelector (source, collectionSelector, resultSelector);
1090 return source.Provider.CreateQuery<TResult> (
1092 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource), typeof (TCollection), typeof (TResult)),
1094 Expression.Quote (collectionSelector),
1095 Expression.Quote (resultSelector)));
1098 public static IQueryable<TResult> SelectMany<TSource, TCollection, TResult> (this IQueryable<TSource> source, Expression<Func<TSource, IEnumerable<TCollection>>> collectionSelector, Expression<Func<TSource, TCollection, TResult>> resultSelector)
1100 Check.SourceAndCollectionSelectorAndResultSelector (source, collectionSelector, resultSelector);
1102 return source.Provider.CreateQuery<TResult> (
1104 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource), typeof (TCollection), typeof (TResult)),
1106 Expression.Quote (collectionSelector),
1107 Expression.Quote (resultSelector)));
1112 #region SequenceEqual
1114 public static bool SequenceEqual<TSource> (this IQueryable<TSource> source1, IEnumerable<TSource> source2)
1116 Check.Source1AndSource2 (source1, source2);
1118 return source1.Provider.Execute<bool> (
1120 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
1122 Expression.Constant (source2)));
1125 public static bool SequenceEqual<TSource> (this IQueryable<TSource> source1, IEnumerable<TSource> source2, IEqualityComparer<TSource> comparer)
1127 Check.Source1AndSource2 (source1, source2);
1129 return source1.Provider.Execute<bool> (
1131 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
1133 Expression.Constant (source2),
1134 Expression.Constant (comparer)));
1141 public static TSource Single<TSource> (this IQueryable<TSource> source)
1143 Check.Source (source);
1145 return source.Provider.Execute<TSource> (
1147 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
1148 source.Expression));
1151 public static TSource Single<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, bool>> predicate)
1153 Check.SourceAndPredicate (source, predicate);
1155 return source.Provider.Execute<TSource> (
1157 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
1159 Expression.Quote (predicate)));
1164 #region SingleOrDefault
1166 public static TSource SingleOrDefault<TSource> (this IQueryable<TSource> source)
1168 Check.Source (source);
1170 return source.Provider.Execute<TSource> (
1172 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
1173 source.Expression));
1176 public static TSource SingleOrDefault<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, bool>> predicate)
1178 Check.SourceAndPredicate (source, predicate);
1180 return source.Provider.Execute<TSource> (
1182 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
1184 Expression.Quote (predicate)));
1191 public static IQueryable<TSource> Skip<TSource> (this IQueryable<TSource> source, int count)
1193 Check.Source (source);
1195 return source.Provider.CreateQuery<TSource> (
1197 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
1199 Expression.Constant (count)));
1206 public static IQueryable<TSource> SkipWhile<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, bool>> predicate)
1208 Check.SourceAndPredicate (source, predicate);
1210 return source.Provider.CreateQuery<TSource> (
1212 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
1214 Expression.Quote (predicate)));
1217 public static IQueryable<TSource> SkipWhile<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, int, bool>> predicate)
1219 Check.SourceAndPredicate (source, predicate);
1221 return source.Provider.CreateQuery<TSource> (
1223 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
1225 Expression.Quote (predicate)));
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 int? Sum<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, int?>> selector)
1247 Check.SourceAndSelector (source, selector);
1249 return source.Provider.Execute<int?> (
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 long? Sum<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, long?>> selector)
1269 Check.SourceAndSelector (source, selector);
1271 return source.Provider.Execute<long?> (
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 float? Sum<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, float?>> selector)
1291 Check.SourceAndSelector (source, selector);
1293 return source.Provider.Execute<float?> (
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 double? Sum<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, double?>> selector)
1313 Check.SourceAndSelector (source, selector);
1315 return source.Provider.Execute<double?> (
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 decimal? Sum<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, decimal?>> selector)
1335 Check.SourceAndSelector (source, selector);
1337 return source.Provider.Execute<decimal?> (
1339 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
1341 Expression.Quote (selector)));
1344 public static int Sum (this IQueryable<int> source)
1346 Check.Source (source);
1348 return source.Provider.Execute<int> (
1350 (MethodInfo) MethodBase.GetCurrentMethod (),
1351 source.Expression));
1354 public static int? Sum (this IQueryable<int?> source)
1356 Check.Source (source);
1358 return source.Provider.Execute<int?> (
1360 (MethodInfo) MethodBase.GetCurrentMethod (),
1361 source.Expression));
1366 public static long Sum (this IQueryable<long> source)
1368 Check.Source (source);
1370 return source.Provider.Execute<long> (
1372 (MethodInfo) MethodBase.GetCurrentMethod (),
1373 source.Expression));
1378 public static long? Sum (this IQueryable<long?> source)
1380 Check.Source (source);
1382 return source.Provider.Execute<long?> (
1384 (MethodInfo) MethodBase.GetCurrentMethod (),
1385 source.Expression));
1388 public static float Sum (this IQueryable<float> source)
1390 Check.Source (source);
1392 return source.Provider.Execute<float> (
1394 (MethodInfo) MethodBase.GetCurrentMethod (),
1395 source.Expression));
1398 public static float? Sum (this IQueryable<float?> source)
1400 Check.Source (source);
1402 return source.Provider.Execute<float?> (
1404 (MethodInfo) MethodBase.GetCurrentMethod (),
1405 source.Expression));
1409 public static double Sum (this IQueryable<double> source)
1411 Check.Source (source);
1413 return source.Provider.Execute<double> (
1415 (MethodInfo) MethodBase.GetCurrentMethod (),
1416 source.Expression));
1421 public static double? Sum (this IQueryable<double?> source)
1423 Check.Source (source);
1425 return source.Provider.Execute<double?> (
1427 (MethodInfo) MethodBase.GetCurrentMethod (),
1428 source.Expression));
1432 public static decimal Sum (this IQueryable<decimal> source)
1434 Check.Source (source);
1436 return source.Provider.Execute<decimal> (
1438 (MethodInfo) MethodBase.GetCurrentMethod (),
1439 source.Expression));
1444 public static decimal? Sum (this IQueryable<decimal?> source)
1446 Check.Source (source);
1448 return source.Provider.Execute<decimal?> (
1450 (MethodInfo) MethodBase.GetCurrentMethod (),
1451 source.Expression));
1460 public static IQueryable<TSource> Take<TSource> (this IQueryable<TSource> source, int count)
1462 Check.Source (source);
1464 return source.Provider.CreateQuery<TSource> (
1466 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
1468 Expression.Constant (count)));
1475 public static IQueryable<TSource> TakeWhile<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, bool>> predicate)
1477 Check.SourceAndPredicate (source, predicate);
1479 return source.Provider.CreateQuery<TSource> (
1481 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
1483 Expression.Quote (predicate)));
1486 public static IQueryable<TSource> TakeWhile<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, int, bool>> predicate)
1488 Check.SourceAndPredicate (source, predicate);
1490 return source.Provider.CreateQuery<TSource> (
1492 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
1494 Expression.Quote (predicate)));
1501 public static IOrderedQueryable<TSource> ThenBy<TSource, TKey> (this IOrderedQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector)
1503 Check.SourceAndKeySelector (source, keySelector);
1505 return (IOrderedQueryable<TSource>) source.Provider.CreateQuery (
1507 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource), typeof (TKey)),
1509 Expression.Quote (keySelector)));
1512 public static IOrderedQueryable<TSource> ThenBy<TSource, TKey> (this IOrderedQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector, IComparer<TKey> comparer)
1514 Check.SourceAndKeySelector (source, keySelector);
1516 return (IOrderedQueryable<TSource>) source.Provider.CreateQuery (
1518 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource), typeof (TKey)),
1520 Expression.Quote (keySelector),
1521 Expression.Constant (comparer)));
1526 #region ThenByDescending
1528 public static IOrderedQueryable<TSource> ThenByDescending<TSource, TKey> (this IOrderedQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector)
1530 Check.SourceAndKeySelector (source, keySelector);
1532 return (IOrderedQueryable<TSource>) source.Provider.CreateQuery (
1534 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource), typeof (TKey)),
1536 Expression.Quote (keySelector)));
1539 public static IOrderedQueryable<TSource> ThenByDescending<TSource, TKey> (this IOrderedQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector, IComparer<TKey> comparer)
1541 Check.SourceAndKeySelector (source, keySelector);
1543 return (IOrderedQueryable<TSource>) source.Provider.CreateQuery (
1545 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource), typeof (TKey)),
1547 Expression.Quote (keySelector),
1548 Expression.Constant (comparer)));
1555 public static IQueryable<TSource> Union<TSource> (this IQueryable<TSource> source1, IEnumerable<TSource> source2)
1557 Check.Source1AndSource2 (source1, source2);
1559 return source1.Provider.CreateQuery<TSource> (
1561 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
1563 Expression.Constant (source2)));
1566 public static IQueryable<TSource> Union<TSource>(this IQueryable<TSource> source1, IEnumerable<TSource> source2, IEqualityComparer<TSource> comparer)
1568 Check.Source1AndSource2 (source1, source2);
1570 return source1.Provider.CreateQuery<TSource> (
1572 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
1574 Expression.Constant (source2),
1575 Expression.Constant (comparer)));
1583 public static IQueryable<TSource> Where<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, bool>> predicate)
1585 Check.SourceAndPredicate (source, predicate);
1587 return source.Provider.CreateQuery<TSource> (
1589 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
1591 Expression.Quote (predicate)));
1594 public static IQueryable<TSource> Where<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, int, bool>> predicate)
1596 Check.SourceAndPredicate (source, predicate);
1598 return source.Provider.CreateQuery<TSource> (
1600 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
1602 Expression.Quote (predicate)));