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)
142 var queryable = source as IQueryable<TElement>;
143 if (queryable != null)
146 return new QueryableEnumerable<TElement> (source);
150 public static IQueryable AsQueryable (this IEnumerable source)
152 var queryable = source as IQueryable;
153 if (queryable != null)
156 throw new NotImplementedException ();
163 public static double Average (this IQueryable<int> source)
165 Check.Source (source);
167 return source.Provider.Execute<double>(
169 (MethodInfo) MethodBase.GetCurrentMethod (),
173 public static double? Average(this IQueryable<int?> source)
175 Check.Source (source);
177 return source.Provider.Execute<double?>(
179 (MethodInfo) MethodBase.GetCurrentMethod (),
183 public static double Average(this IQueryable<long> source)
185 Check.Source (source);
187 return source.Provider.Execute<double>(
189 (MethodInfo) MethodBase.GetCurrentMethod (),
193 public static double? Average(this IQueryable<long?> source)
195 Check.Source (source);
197 return source.Provider.Execute<double?>(
199 (MethodInfo) MethodBase.GetCurrentMethod (),
203 public static float Average(this IQueryable<float> source)
205 Check.Source (source);
207 return source.Provider.Execute<float>(
209 (MethodInfo) MethodBase.GetCurrentMethod (),
213 public static float? Average(this IQueryable<float?> source)
215 Check.Source (source);
217 return source.Provider.Execute<float?>(
219 (MethodInfo) MethodBase.GetCurrentMethod (),
223 public static double Average (this IQueryable<double> source)
225 Check.Source (source);
227 return source.Provider.Execute<double> (
229 (MethodInfo) MethodBase.GetCurrentMethod (),
233 public static double? Average (this IQueryable<double?> source)
235 Check.Source (source);
237 return source.Provider.Execute<double?> (
239 (MethodInfo) MethodBase.GetCurrentMethod (),
243 public static decimal Average(this IQueryable<decimal> source)
245 Check.Source (source);
247 return source.Provider.Execute<decimal>(
249 (MethodInfo) MethodBase.GetCurrentMethod (),
253 public static decimal? Average(this IQueryable<decimal?> source)
255 Check.Source (source);
257 return source.Provider.Execute<decimal?>(
259 (MethodInfo) MethodBase.GetCurrentMethod (),
263 public static double Average<TSource>(this IQueryable<TSource> source, Expression<Func<TSource, int>> selector)
265 Check.SourceAndSelector (source, selector);
267 return source.Provider.Execute<double>(
269 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
271 Expression.Quote (selector)));
274 public static double? Average<TSource>(this IQueryable<TSource> source, Expression<Func<TSource, int?>> selector)
276 Check.SourceAndSelector (source, selector);
278 return source.Provider.Execute<double?>(
280 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
282 Expression.Quote (selector)));
285 public static double Average<TSource>(this IQueryable<TSource> source, Expression<Func<TSource, long>> selector)
287 Check.SourceAndSelector (source, selector);
289 return source.Provider.Execute<double>(
291 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
293 Expression.Quote (selector)));
296 public static double? Average<TSource>(this IQueryable<TSource> source, Expression<Func<TSource, long?>> selector)
298 Check.SourceAndSelector (source, selector);
300 return source.Provider.Execute<double?>(
302 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
304 Expression.Quote (selector)));
307 public static float Average<TSource>(this IQueryable<TSource> source, Expression<Func<TSource, float>> selector)
309 Check.SourceAndSelector (source, selector);
311 return source.Provider.Execute<float>(
313 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
315 Expression.Quote (selector)));
318 public static float? Average<TSource>(this IQueryable<TSource> source, Expression<Func<TSource, float?>> selector)
320 Check.SourceAndSelector (source, selector);
322 return source.Provider.Execute<float?>(
324 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
326 Expression.Quote (selector)));
329 public static double Average<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, double>> selector)
331 Check.SourceAndSelector (source, selector);
333 return source.Provider.Execute<double> (
335 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
337 Expression.Quote (selector)));
340 public static double? Average<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, double?>> selector)
342 Check.SourceAndSelector (source, selector);
344 return source.Provider.Execute<double?> (
346 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
348 Expression.Quote (selector)));
351 public static decimal Average<TSource>(this IQueryable<TSource> source, Expression<Func<TSource, decimal>> selector)
353 Check.SourceAndSelector (source, selector);
355 return source.Provider.Execute<decimal>(
357 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
359 Expression.Quote (selector)));
362 public static decimal? Average<TSource>(this IQueryable<TSource> source, Expression<Func<TSource, decimal?>> selector)
364 Check.SourceAndSelector (source, selector);
366 return source.Provider.Execute<decimal?>(
368 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
370 Expression.Quote (selector)));
377 public static IQueryable<TResult> Cast<TResult> (this IQueryable source)
379 Check.Source (source);
381 return (IQueryable<TResult>) source.Provider.CreateQuery (
382 StaticCall (MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TResult)),
390 public static IQueryable<TSource> Concat<TSource> (this IQueryable<TSource> source1, IEnumerable<TSource> source2)
392 Check.Source1AndSource2 (source1, source2);
394 return source1.Provider.CreateQuery<TSource> (
396 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
398 Expression.Constant (source2)));
405 public static bool Contains<TSource> (this IQueryable<TSource> source, TSource item)
407 Check.Source (source);
409 return source.Provider.Execute<bool> (
411 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
413 Expression.Constant (item)));
416 public static bool Contains<TSource> (this IQueryable<TSource> source, TSource item, IEqualityComparer<TSource> comparer)
418 Check.Source (source);
420 return source.Provider.Execute<bool> (
422 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
424 Expression.Constant (item),
425 Expression.Constant (comparer)));
432 public static int Count<TSource> (this IQueryable<TSource> source)
434 Check.Source (source);
436 return source.Execute<int, TSource> (MethodBase.GetCurrentMethod ());
439 public static int Count<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, bool>> predicate)
441 Check.SourceAndPredicate (source, predicate);
443 return source.Provider.Execute<int> (
445 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
447 Expression.Quote (predicate)));
452 #region DefaultIfEmpty
454 public static IQueryable<TSource> DefaultIfEmpty<TSource> (this IQueryable<TSource> source)
456 Check.Source (source);
458 return source.Provider.CreateQuery<TSource> (
460 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
464 public static IQueryable<TSource> DefaultIfEmpty<TSource> (this IQueryable<TSource> source, TSource defaultValue)
466 Check.Source (source);
468 return source.Provider.CreateQuery<TSource> (
470 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
472 Expression.Constant (defaultValue)));
479 public static IQueryable<TSource> Distinct<TSource> (this IQueryable<TSource> source)
481 Check.Source (source);
483 return source.Provider.CreateQuery<TSource> (
485 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
489 public static IQueryable<TSource> Distinct<TSource> (this IQueryable<TSource> source, IEqualityComparer<TSource> comparer)
491 Check.Source (source);
493 return source.Provider.CreateQuery<TSource> (
495 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
497 Expression.Constant (comparer)));
504 public static TSource ElementAt<TSource> (this IQueryable<TSource> source, int index)
506 Check.Source (source);
508 return source.Provider.Execute<TSource> (
510 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
512 Expression.Constant (index)));
517 #region ElementAtOrDefault
519 public static TSource ElementAtOrDefault<TSource> (this IQueryable<TSource> source, int index)
521 Check.Source (source);
523 return source.Provider.Execute<TSource> (
525 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
527 Expression.Constant (index)));
534 public static IQueryable<TSource> Except<TSource> (this IQueryable<TSource> source1, IEnumerable<TSource> source2)
536 Check.Source1AndSource2 (source1, source2);
538 return source1.Provider.CreateQuery<TSource> (
540 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
542 Expression.Constant (source2)));
545 public static IQueryable<TSource> Except<TSource> (this IQueryable<TSource> source1, IEnumerable<TSource> source2, IEqualityComparer<TSource> comparer)
547 Check.Source1AndSource2 (source1, source2);
549 return source1.Provider.CreateQuery<TSource> (
551 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
553 Expression.Constant (source2),
554 Expression.Constant (comparer)));
561 public static TSource First<TSource> (this IQueryable<TSource> source)
563 Check.Source (source);
565 return source.Provider.Execute<TSource> (
567 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
571 public static TSource First<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, bool>> predicate)
573 Check.SourceAndPredicate (source, predicate);
575 return source.Provider.Execute<TSource> (
577 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
579 Expression.Quote (predicate)));
584 #region FirstOrDefault
586 public static TSource FirstOrDefault<TSource> (this IQueryable<TSource> source)
588 Check.Source (source);
590 return source.Provider.Execute<TSource> (
592 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
596 public static TSource FirstOrDefault<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, bool>> predicate)
598 Check.SourceAndPredicate (source, predicate);
600 return source.Provider.Execute<TSource> (
602 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
604 Expression.Quote (predicate)));
611 public static IQueryable<IGrouping<TKey, TSource>> GroupBy<TSource, TKey> (this IQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector)
613 Check.SourceAndKeySelector (source, keySelector);
615 return source.Provider.CreateQuery<IGrouping<TKey, TSource>> (
617 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource), typeof (TKey)),
619 Expression.Quote (keySelector)));
621 public static IQueryable<IGrouping<TKey, TSource>> GroupBy<TSource, TKey> (this IQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector, IEqualityComparer<TKey> comparer)
623 Check.SourceAndKeySelector (source, keySelector);
625 return source.Provider.CreateQuery<IGrouping<TKey, TSource>> (
627 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource), typeof (TKey)),
629 Expression.Quote (keySelector),
630 Expression.Constant (comparer)));
632 public static IQueryable<IGrouping<TKey, TElement>> GroupBy<TSource, TKey, TElement> (this IQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector, Expression<Func<TSource, TElement>> elementSelector)
634 Check.SourceAndKeyElementSelectors (source, keySelector, elementSelector);
636 return source.Provider.CreateQuery<IGrouping<TKey, TElement>>(
638 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource), typeof (TKey), typeof (TElement)),
640 Expression.Quote (keySelector),
641 Expression.Quote (elementSelector)));
643 public static IQueryable<TResult> GroupBy<TSource, TKey, TResult> (this IQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector, Expression<Func<TKey, IEnumerable<TSource>, TResult>> resultSelector)
645 Check.SourceAndKeyResultSelectors (source, keySelector, resultSelector);
647 return source.Provider.CreateQuery<TResult> (
649 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource), typeof (TKey), typeof (TResult)),
651 Expression.Quote (keySelector),
652 Expression.Quote (resultSelector)));
654 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)
656 Check.SourceAndKeyElementSelectors (source, keySelector, elementSelector);
658 return source.Provider.CreateQuery<IGrouping<TKey, TElement>> (
660 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource), typeof (TKey), typeof (TElement)),
662 Expression.Quote (keySelector),
663 Expression.Quote (elementSelector),
664 Expression.Constant (comparer)));
666 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)
668 Check.GroupBySelectors (source, keySelector, elementSelector, resultSelector);
670 return source.Provider.CreateQuery<TResult> (
672 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource), typeof (TKey), typeof (TElement), typeof (TResult)),
674 Expression.Quote (keySelector),
675 Expression.Quote (elementSelector),
676 Expression.Quote (resultSelector)));
679 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)
681 Check.SourceAndKeyResultSelectors (source, keySelector, resultSelector);
683 return source.Provider.CreateQuery<TResult> (
685 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource), typeof (TKey), typeof (TResult)),
687 Expression.Quote (keySelector),
688 Expression.Quote (resultSelector),
689 Expression.Constant (comparer)));
691 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)
693 Check.GroupBySelectors (source, keySelector, elementSelector, resultSelector);
695 return source.Provider.CreateQuery<TResult> (
697 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource), typeof (TKey), typeof (TElement), typeof (TResult)),
699 Expression.Quote (keySelector),
700 Expression.Quote (elementSelector),
701 Expression.Quote (resultSelector),
702 Expression.Constant (comparer)));
708 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)
711 throw new ArgumentNullException ("outer");
713 throw new ArgumentNullException ("inner");
714 if (outerKeySelector == null)
715 throw new ArgumentNullException ("outerKeySelector");
716 if (innerKeySelector == null)
717 throw new ArgumentNullException ("innerKeySelector");
718 if (resultSelector == null)
719 throw new ArgumentNullException ("resultSelector");
721 return outer.Provider.CreateQuery<TResult> (
723 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TOuter), typeof (TInner), typeof (TKey), typeof (TResult)),
725 Expression.Constant (inner),
726 Expression.Quote (outerKeySelector),
727 Expression.Quote (innerKeySelector),
728 Expression.Quote (resultSelector)));
731 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)
734 throw new ArgumentNullException ("outer");
736 throw new ArgumentNullException ("inner");
737 if (outerKeySelector == null)
738 throw new ArgumentNullException ("outerKeySelector");
739 if (innerKeySelector == null)
740 throw new ArgumentNullException ("innerKeySelector");
741 if (resultSelector == null)
742 throw new ArgumentNullException ("resultSelector");
744 return outer.Provider.CreateQuery<TResult> (
746 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TOuter), typeof (TInner), typeof (TKey), typeof (TResult)),
748 Expression.Constant (inner),
749 Expression.Quote (outerKeySelector),
750 Expression.Quote (innerKeySelector),
751 Expression.Quote (resultSelector),
752 Expression.Constant (comparer)));
759 public static IQueryable<TSource> Intersect<TSource> (this IQueryable<TSource> source1, IEnumerable<TSource> source2)
761 Check.Source1AndSource2 (source1, source2);
763 return source1.Provider.CreateQuery<TSource> (
765 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
767 Expression.Constant (source2)));
770 public static IQueryable<TSource> Intersect<TSource> (this IQueryable<TSource> source1, IEnumerable<TSource> source2, IEqualityComparer<TSource> comparer)
772 Check.Source1AndSource2 (source1, source2);
774 return source1.Provider.CreateQuery<TSource> (
776 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
778 Expression.Constant (source2),
779 Expression.Constant (comparer)));
786 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)
788 Check.JoinSelectors (outer, inner, outerKeySelector, innerKeySelector, resultSelector);
790 return outer.Provider.CreateQuery<TResult> (
792 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TOuter), typeof (TInner), typeof (TKey), typeof (TResult)),
794 Expression.Constant (inner),
795 Expression.Quote (outerKeySelector),
796 Expression.Quote (innerKeySelector),
797 Expression.Quote (resultSelector)));
800 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)
802 Check.JoinSelectors (outer, inner, outerKeySelector, innerKeySelector, resultSelector);
804 return outer.Provider.CreateQuery<TResult> (
806 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TOuter), typeof (TInner), typeof (TKey), typeof (TResult)),
808 Expression.Constant (inner),
809 Expression.Quote (outerKeySelector),
810 Expression.Quote (innerKeySelector),
811 Expression.Quote (resultSelector),
812 Expression.Constant (comparer)));
820 public static TSource Last<TSource> (this IQueryable<TSource> source)
822 Check.Source (source);
824 return source.Provider.Execute<TSource> (
826 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
830 public static TSource Last<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, bool>> predicate)
832 Check.SourceAndPredicate (source, predicate);
834 return source.Provider.Execute<TSource> (
836 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
838 Expression.Quote (predicate)));
843 #region LastOrDefault
845 public static TSource LastOrDefault<TSource> (this IQueryable<TSource> source)
847 Check.Source (source);
849 return source.Provider.Execute<TSource> (
851 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
855 public static TSource LastOrDefault<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, bool>> predicate)
857 Check.SourceAndPredicate (source, predicate);
859 return source.Provider.Execute<TSource> (
861 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
863 Expression.Quote (predicate)));
870 public static long LongCount<TSource> (this IQueryable<TSource> source)
872 Check.Source (source);
874 return source.Execute<long, TSource> (MethodBase.GetCurrentMethod ());
877 public static long LongCount<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, bool>> predicate)
879 Check.SourceAndPredicate (source, predicate);
881 return source.Provider.Execute<long> (
883 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
885 Expression.Quote (predicate)));
892 public static TSource Max<TSource> (this IQueryable<TSource> source)
894 Check.Source (source);
896 return source.Provider.Execute<TSource> (
898 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
902 public static TResult Max<TSource, TResult> (this IQueryable<TSource> source, Expression<Func<TSource, TResult>> selector)
904 Check.Source (source);
906 return source.Provider.Execute<TResult> (
908 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource), typeof (TResult)),
910 Expression.Quote (selector)));
918 public static TSource Min<TSource> (this IQueryable<TSource> source)
920 Check.Source (source);
922 return source.Provider.Execute<TSource> (
924 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
928 public static TResult Min<TSource, TResult> (this IQueryable<TSource> source, Expression<Func<TSource, TResult>> selector)
930 Check.SourceAndSelector (source, selector);
932 return source.Provider.Execute<TResult> (
934 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource), typeof (TResult)),
936 Expression.Quote (selector)));
944 public static IQueryable<TResult> OfType<TResult> (this IQueryable source)
946 Check.Source (source);
948 return (IQueryable<TResult>) source.Provider.CreateQuery (
950 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TResult)),
958 public static IOrderedQueryable<TSource> OrderBy<TSource, TKey> (this IQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector)
960 Check.SourceAndKeySelector (source, keySelector);
962 return (IOrderedQueryable<TSource>) source.Provider.CreateQuery<TSource> (
964 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource), typeof (TKey)),
966 Expression.Quote (keySelector)));
969 public static IOrderedQueryable<TSource> OrderBy<TSource, TKey> (this IQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector, IComparer<TKey> comparer)
971 Check.SourceAndKeySelector (source, keySelector);
973 return (IOrderedQueryable<TSource>) source.Provider.CreateQuery<TSource> (
975 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource), typeof (TKey)),
977 Expression.Quote (keySelector),
978 Expression.Constant (comparer)));
983 #region OrderByDescending
985 public static IOrderedQueryable<TSource> OrderByDescending<TSource, TKey> (this IQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector)
987 Check.SourceAndKeySelector (source, keySelector);
989 return (IOrderedQueryable<TSource>) source.Provider.CreateQuery<TSource> (
991 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource), typeof (TKey)),
993 Expression.Quote (keySelector)));
996 public static IOrderedQueryable<TSource> OrderByDescending<TSource, TKey> (this IQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector, IComparer<TKey> comparer)
998 Check.SourceAndKeySelector (source, keySelector);
1000 return (IOrderedQueryable<TSource>) source.Provider.CreateQuery<TSource> (
1002 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource), typeof (TKey)),
1004 Expression.Quote (keySelector),
1005 Expression.Constant (comparer)));
1012 public static IQueryable<TSource> Reverse<TSource> (this IQueryable<TSource> source)
1014 Check.Source (source);
1016 return source.Provider.CreateQuery<TSource> (
1018 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
1019 source.Expression));
1026 public static IQueryable<TResult> Select<TSource, TResult> (this IQueryable<TSource> source, Expression<Func<TSource, TResult>> selector)
1028 Check.SourceAndSelector (source, selector);
1030 return source.Provider.CreateQuery<TResult> (
1032 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource), typeof (TResult)),
1034 Expression.Quote (selector)));
1037 public static IQueryable<TResult> Select<TSource, TResult> (this IQueryable<TSource> source, Expression<Func<TSource, int, TResult>> selector)
1039 Check.SourceAndSelector (source, selector);
1041 return source.Provider.CreateQuery<TResult> (
1043 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource), typeof (TResult)),
1045 Expression.Quote (selector)));
1052 public static IQueryable<TResult> SelectMany<TSource, TResult> (this IQueryable<TSource> source, Expression<Func<TSource, IEnumerable<TResult>>> selector)
1054 Check.SourceAndSelector (source, selector);
1056 return source.Provider.CreateQuery<TResult> (
1058 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource), typeof (TResult)),
1060 Expression.Quote (selector)));
1063 public static IQueryable<TResult> SelectMany<TSource, TResult> (this IQueryable<TSource> source, Expression<Func<TSource, int, IEnumerable<TResult>>> selector)
1065 Check.SourceAndSelector (source, selector);
1067 return source.Provider.CreateQuery<TResult> (
1069 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource), typeof (TResult)),
1071 Expression.Quote (selector)));
1074 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)
1076 Check.SourceAndCollectionSelectorAndResultSelector (source, collectionSelector, resultSelector);
1078 return source.Provider.CreateQuery<TResult> (
1080 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource), typeof (TCollection), typeof (TResult)),
1082 Expression.Quote (collectionSelector),
1083 Expression.Quote (resultSelector)));
1086 public static IQueryable<TResult> SelectMany<TSource, TCollection, TResult> (this IQueryable<TSource> source, Expression<Func<TSource, 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)));
1100 #region SequenceEqual
1102 public static bool SequenceEqual<TSource> (this IQueryable<TSource> source1, IEnumerable<TSource> source2)
1104 Check.Source1AndSource2 (source1, source2);
1106 return source1.Provider.Execute<bool> (
1108 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
1110 Expression.Constant (source2)));
1113 public static bool SequenceEqual<TSource> (this IQueryable<TSource> source1, IEnumerable<TSource> source2, IEqualityComparer<TSource> comparer)
1115 Check.Source1AndSource2 (source1, source2);
1117 return source1.Provider.Execute<bool> (
1119 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
1121 Expression.Constant (source2),
1122 Expression.Constant (comparer)));
1129 public static TSource Single<TSource> (this IQueryable<TSource> source)
1131 Check.Source (source);
1133 return source.Provider.Execute<TSource> (
1135 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
1136 source.Expression));
1139 public static TSource Single<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, bool>> predicate)
1141 Check.SourceAndPredicate (source, predicate);
1143 return source.Provider.Execute<TSource> (
1145 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
1147 Expression.Quote (predicate)));
1152 #region SingleOrDefault
1154 public static TSource SingleOrDefault<TSource> (this IQueryable<TSource> source)
1156 Check.Source (source);
1158 return source.Provider.Execute<TSource> (
1160 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
1161 source.Expression));
1164 public static TSource SingleOrDefault<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, bool>> predicate)
1166 Check.SourceAndPredicate (source, predicate);
1168 return source.Provider.Execute<TSource> (
1170 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
1172 Expression.Quote (predicate)));
1179 public static IQueryable<TSource> Skip<TSource> (this IQueryable<TSource> source, int count)
1181 Check.Source (source);
1183 return source.Provider.CreateQuery<TSource> (
1185 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
1187 Expression.Constant (count)));
1194 public static IQueryable<TSource> SkipWhile<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, bool>> predicate)
1196 Check.SourceAndPredicate (source, predicate);
1198 return source.Provider.CreateQuery<TSource> (
1200 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
1202 Expression.Quote (predicate)));
1205 public static IQueryable<TSource> SkipWhile<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, int, bool>> predicate)
1207 Check.SourceAndPredicate (source, predicate);
1209 return source.Provider.CreateQuery<TSource> (
1211 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
1213 Expression.Quote (predicate)));
1222 public static int Sum<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, int>> selector)
1224 Check.SourceAndSelector (source, selector);
1226 return source.Provider.Execute<int> (
1228 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
1230 Expression.Quote (selector)));
1233 public static int? Sum<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, int?>> selector)
1235 Check.SourceAndSelector (source, selector);
1237 return source.Provider.Execute<int?> (
1239 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
1241 Expression.Quote (selector)));
1244 public static long Sum<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, long>> selector)
1246 Check.SourceAndSelector (source, selector);
1248 return source.Provider.Execute<long> (
1250 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
1252 Expression.Quote (selector)));
1255 public static long? Sum<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, long?>> selector)
1257 Check.SourceAndSelector (source, selector);
1259 return source.Provider.Execute<long?> (
1261 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
1263 Expression.Quote (selector)));
1266 public static float Sum<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, float>> selector)
1268 Check.SourceAndSelector (source, selector);
1270 return source.Provider.Execute<float> (
1272 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
1274 Expression.Quote (selector)));
1277 public static float? Sum<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, float?>> selector)
1279 Check.SourceAndSelector (source, selector);
1281 return source.Provider.Execute<float?> (
1283 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
1285 Expression.Quote (selector)));
1288 public static double Sum<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, double>> selector)
1290 Check.SourceAndSelector (source, selector);
1292 return source.Provider.Execute<double> (
1294 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
1296 Expression.Quote (selector)));
1299 public static double? Sum<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, double?>> selector)
1301 Check.SourceAndSelector (source, selector);
1303 return source.Provider.Execute<double?> (
1305 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
1307 Expression.Quote (selector)));
1310 public static decimal Sum<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, decimal>> selector)
1312 Check.SourceAndSelector (source, selector);
1314 return source.Provider.Execute<decimal> (
1316 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
1318 Expression.Quote (selector)));
1321 public static decimal? Sum<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, decimal?>> selector)
1323 Check.SourceAndSelector (source, selector);
1325 return source.Provider.Execute<decimal?> (
1327 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
1329 Expression.Quote (selector)));
1332 public static int Sum (this IQueryable<int> source)
1334 Check.Source (source);
1336 return source.Provider.Execute<int> (
1338 (MethodInfo) MethodBase.GetCurrentMethod (),
1339 source.Expression));
1342 public static int? Sum (this IQueryable<int?> source)
1344 Check.Source (source);
1346 return source.Provider.Execute<int?> (
1348 (MethodInfo) MethodBase.GetCurrentMethod (),
1349 source.Expression));
1354 public static long Sum (this IQueryable<long> source)
1356 Check.Source (source);
1358 return source.Provider.Execute<long> (
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));
1376 public static float Sum (this IQueryable<float> source)
1378 Check.Source (source);
1380 return source.Provider.Execute<float> (
1382 (MethodInfo) MethodBase.GetCurrentMethod (),
1383 source.Expression));
1386 public static float? Sum (this IQueryable<float?> source)
1388 Check.Source (source);
1390 return source.Provider.Execute<float?> (
1392 (MethodInfo) MethodBase.GetCurrentMethod (),
1393 source.Expression));
1397 public static double Sum (this IQueryable<double> source)
1399 Check.Source (source);
1401 return source.Provider.Execute<double> (
1403 (MethodInfo) MethodBase.GetCurrentMethod (),
1404 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));
1420 public static decimal Sum (this IQueryable<decimal> source)
1422 Check.Source (source);
1424 return source.Provider.Execute<decimal> (
1426 (MethodInfo) MethodBase.GetCurrentMethod (),
1427 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));
1448 public static IQueryable<TSource> Take<TSource> (this IQueryable<TSource> source, int count)
1450 Check.Source (source);
1452 return source.Provider.CreateQuery<TSource> (
1454 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
1456 Expression.Constant (count)));
1463 public static IQueryable<TSource> TakeWhile<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, bool>> predicate)
1465 Check.SourceAndPredicate (source, predicate);
1467 return source.Provider.CreateQuery<TSource> (
1469 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
1471 Expression.Quote (predicate)));
1474 public static IQueryable<TSource> TakeWhile<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, int, bool>> predicate)
1476 Check.SourceAndPredicate (source, predicate);
1478 return source.Provider.CreateQuery<TSource> (
1480 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
1482 Expression.Quote (predicate)));
1489 public static IOrderedQueryable<TSource> ThenBy<TSource, TKey> (this IOrderedQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector)
1491 Check.SourceAndKeySelector (source, keySelector);
1493 return (IOrderedQueryable<TSource>) source.Provider.CreateQuery (
1495 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource), typeof (TKey)),
1497 Expression.Quote (keySelector)));
1500 public static IOrderedQueryable<TSource> ThenBy<TSource, TKey> (this IOrderedQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector, IComparer<TKey> comparer)
1502 Check.SourceAndKeySelector (source, keySelector);
1504 return (IOrderedQueryable<TSource>) source.Provider.CreateQuery (
1506 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource), typeof (TKey)),
1508 Expression.Quote (keySelector),
1509 Expression.Constant (comparer)));
1514 #region ThenByDescending
1516 public static IOrderedQueryable<TSource> ThenByDescending<TSource, TKey> (this IOrderedQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector)
1518 Check.SourceAndKeySelector (source, keySelector);
1520 return (IOrderedQueryable<TSource>) source.Provider.CreateQuery (
1522 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource), typeof (TKey)),
1524 Expression.Quote (keySelector)));
1527 public static IOrderedQueryable<TSource> ThenByDescending<TSource, TKey> (this IOrderedQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector, IComparer<TKey> comparer)
1529 Check.SourceAndKeySelector (source, keySelector);
1531 return (IOrderedQueryable<TSource>) source.Provider.CreateQuery (
1533 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource), typeof (TKey)),
1535 Expression.Quote (keySelector),
1536 Expression.Constant (comparer)));
1543 public static IQueryable<TSource> Union<TSource> (this IQueryable<TSource> source1, IEnumerable<TSource> source2)
1545 Check.Source1AndSource2 (source1, source2);
1547 return source1.Provider.CreateQuery<TSource> (
1549 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
1551 Expression.Constant (source2)));
1554 public static IQueryable<TSource> Union<TSource>(this IQueryable<TSource> source1, IEnumerable<TSource> source2, IEqualityComparer<TSource> comparer)
1556 Check.Source1AndSource2 (source1, source2);
1558 return source1.Provider.CreateQuery<TSource> (
1560 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
1562 Expression.Constant (source2),
1563 Expression.Constant (comparer)));
1571 public static IQueryable<TSource> Where<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, bool>> predicate)
1573 Check.SourceAndPredicate (source, predicate);
1575 return source.Provider.CreateQuery<TSource> (
1577 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
1579 Expression.Quote (predicate)));
1582 public static IQueryable<TSource> Where<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, int, bool>> predicate)
1584 Check.SourceAndPredicate (source, predicate);
1586 return source.Provider.CreateQuery<TSource> (
1588 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
1590 Expression.Quote (predicate)));