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.Generic;
34 using System.Linq.Expressions;
35 using System.Reflection;
38 namespace System.Linq {
40 public static class Queryable {
42 static MethodInfo MakeGeneric (MethodBase method, params Type [] parameters)
44 return ((MethodInfo) method).MakeGenericMethod (parameters);
47 static Expression StaticCall (MethodInfo method, params Expression [] expressions)
49 return Expression.Call (null, method, expressions);
52 static TRet Execute<TRet, TSource> (this IQueryable<TSource> source, MethodBase current)
54 return source.Provider.Execute<TRet> (
56 MakeGeneric (current, typeof (TSource)),
62 public static TSource Aggregate<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, TSource, TSource>> func)
64 Check.SourceAndFunc (source, func);
65 return source.Provider.Execute<TSource> (
67 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
69 Expression.Quote (func)));
72 public static TAccumulate Aggregate<TSource, TAccumulate> (this IQueryable<TSource> source, TAccumulate seed, Expression<Func<TAccumulate, TSource, TAccumulate>> func)
74 Check.SourceAndFunc (source, func);
75 return source.Provider.Execute<TAccumulate> (
77 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource), typeof (TAccumulate)),
79 Expression.Constant (seed),
80 Expression.Quote (func)));
83 public static TResult Aggregate<TSource, TAccumulate, TResult> (this IQueryable<TSource> source, TAccumulate seed, Expression<Func<TAccumulate, TSource, TAccumulate>> func, Expression<Func<TAccumulate, TResult>> selector)
85 Check.SourceAndFuncAndSelector (source, func, selector);
86 return source.Provider.Execute<TResult> (
88 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource), typeof (TAccumulate), typeof (TResult)),
90 Expression.Constant (seed),
91 Expression.Quote (func),
92 Expression.Quote (selector)));
99 public static bool All<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, bool>> predicate)
101 Check.SourceAndPredicate (source, predicate);
103 return source.Provider.Execute<bool> (
105 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
107 Expression.Quote (predicate)));
114 public static bool Any<TSource> (this IQueryable<TSource> source)
116 Check.Source (source);
118 return source.Provider.Execute<bool> (
120 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
124 public static bool Any<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, bool>> predicate)
126 Check.SourceAndPredicate (source, predicate);
128 return source.Provider.Execute<bool> (
130 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
132 Expression.Quote (predicate)));
139 public static IQueryable<TElement> AsQueryable<TElement> (this IEnumerable<TElement> source)
141 if (source is IQueryable<TElement>)
142 return (IQueryable<TElement>) source;
143 throw new NotImplementedException ();
146 public static IQueryable AsQueryable (this IQueryable source)
148 throw new NotImplementedException ();
155 public static int Average (this IQueryable<int> source)
157 Check.Source (source);
159 return source.Provider.Execute<int> (
161 (MethodInfo) MethodBase.GetCurrentMethod (),
165 public static int? Average (this IQueryable<int?> source)
167 Check.Source (source);
169 return source.Provider.Execute<int?> (
171 (MethodInfo) MethodBase.GetCurrentMethod (),
175 public static long Average (this IQueryable<long> source)
177 Check.Source (source);
179 return source.Provider.Execute<long> (
181 (MethodInfo) MethodBase.GetCurrentMethod (),
185 public static long? Average (this IQueryable<long?> source)
187 Check.Source (source);
189 return source.Provider.Execute<long?> (
191 (MethodInfo) MethodBase.GetCurrentMethod (),
195 public static float Average (this IQueryable<float> source)
197 Check.Source (source);
199 return source.Provider.Execute<float> (
201 (MethodInfo) (MethodInfo) MethodBase.GetCurrentMethod (),
205 public static float? Average (this IQueryable<float?> source)
207 Check.Source (source);
209 return source.Provider.Execute<float?> (
211 (MethodInfo) MethodBase.GetCurrentMethod (),
215 public static double Average (this IQueryable<double> source)
217 Check.Source (source);
219 return source.Provider.Execute<double> (
221 (MethodInfo) MethodBase.GetCurrentMethod (),
225 public static double? Average (this IQueryable<double?> source)
227 Check.Source (source);
229 return source.Provider.Execute<double?> (
231 (MethodInfo) MethodBase.GetCurrentMethod (),
235 public static decimal Average (this IQueryable<decimal> source)
237 Check.Source (source);
239 return source.Provider.Execute<decimal> (
241 (MethodInfo) MethodBase.GetCurrentMethod (),
245 public static decimal? Average (this IQueryable<decimal?> source)
247 Check.Source (source);
249 return source.Provider.Execute<decimal?> (
251 (MethodInfo) MethodBase.GetCurrentMethod (),
255 public static int Average<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, int>> selector)
257 Check.SourceAndSelector (source, selector);
259 return source.Provider.Execute<int> (
261 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
263 Expression.Quote (selector)));
266 public static int? Average<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, int?>> selector)
268 Check.SourceAndSelector (source, selector);
270 return source.Provider.Execute<int?> (
272 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
274 Expression.Quote (selector)));
277 public static long Average<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, long>> selector)
279 Check.SourceAndSelector (source, selector);
281 return source.Provider.Execute<long> (
283 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
285 Expression.Quote (selector)));
288 public static long? Average<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, long?>> selector)
290 Check.SourceAndSelector (source, selector);
292 return source.Provider.Execute<long?> (
294 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
296 Expression.Quote (selector)));
299 public static float Average<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, float>> selector)
301 Check.SourceAndSelector (source, selector);
303 return source.Provider.Execute<float> (
305 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
307 Expression.Quote (selector)));
310 public static float? Average<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, float?>> selector)
312 Check.SourceAndSelector (source, selector);
314 return source.Provider.Execute<float?> (
316 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
318 Expression.Quote (selector)));
321 public static double Average<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, double>> selector)
323 Check.SourceAndSelector (source, selector);
325 return source.Provider.Execute<double> (
327 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
329 Expression.Quote (selector)));
332 public static double? Average<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, double?>> selector)
334 Check.SourceAndSelector (source, selector);
336 return source.Provider.Execute<double?> (
338 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
340 Expression.Quote (selector)));
343 public static decimal Average<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, decimal>> selector)
345 Check.SourceAndSelector (source, selector);
347 return source.Provider.Execute<decimal> (
349 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
351 Expression.Quote (selector)));
354 public static decimal? Average<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, decimal?>> selector)
356 Check.SourceAndSelector (source, selector);
358 return source.Provider.Execute<decimal?> (
360 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
362 Expression.Quote (selector)));
369 public static IQueryable<TResult> Cast<TResult> (this IQueryable source)
371 Check.Source (source);
373 return (IQueryable<TResult>) source.Provider.CreateQuery (
374 StaticCall (MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TResult)),
382 public static IQueryable<TSource> Concat<TSource> (this IQueryable<TSource> source1, IEnumerable<TSource> source2)
384 Check.Source1AndSource2 (source1, source2);
386 return source1.Provider.Execute<IQueryable<TSource>> (
388 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
390 Expression.Constant (source2)));
397 public static bool Contains<TSource> (this IQueryable<TSource> source, TSource item)
399 Check.Source (source);
401 return source.Provider.Execute<bool> (
403 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
405 Expression.Constant (item)));
408 public static bool Contains<TSource> (this IQueryable<TSource> source, TSource item, IEqualityComparer<TSource> comparer)
410 Check.Source (source);
412 return source.Provider.Execute<bool> (
414 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
416 Expression.Constant (item),
417 Expression.Constant (comparer)));
424 public static int Count<TSource> (this IQueryable<TSource> source)
426 Check.Source (source);
428 return source.Execute<int, TSource> (MethodBase.GetCurrentMethod ());
431 public static int Count<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, bool>> predicate)
433 Check.SourceAndPredicate (source, predicate);
435 return source.Provider.Execute<int> (
437 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
439 Expression.Quote (predicate)));
444 #region DefaultIfEmpty
446 public static TSource DefaultIfEmpty<TSource> (this IQueryable<TSource> source)
448 Check.Source (source);
450 return source.Provider.Execute<TSource> (
452 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
456 public static TSource DefaultIfEmpty<TSource> (this IQueryable<TSource> source, TSource defaultValue)
458 Check.Source (source);
460 return source.Provider.Execute<TSource> (
462 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
464 Expression.Constant (defaultValue)));
471 public static bool Distinct<TSource> (this IQueryable<TSource> source)
473 Check.Source (source);
475 return source.Provider.Execute<bool> (
477 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
481 public static bool Distinct<TSource> (this IQueryable<TSource> source, IEqualityComparer<TSource> comparer)
483 Check.Source (source);
485 return source.Provider.Execute<bool> (
487 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
489 Expression.Constant (comparer)));
496 public static TSource ElementAt<TSource> (this IQueryable<TSource> source, int index)
498 Check.Source (source);
500 return source.Provider.Execute<TSource> (
502 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
504 Expression.Constant (index)));
509 #region ElementAtOrDefault
511 public static TSource ElementAtOrDefault<TSource> (this IQueryable<TSource> source, int index)
513 Check.Source (source);
515 return source.Provider.Execute<TSource> (
517 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
519 Expression.Constant (index)));
526 public static IQueryable<TSource> Except<TSource> (this IQueryable<TSource> source1, IEnumerable<TSource> source2)
528 Check.Source1AndSource2 (source1, source2);
530 return source1.Provider.Execute<IQueryable<TSource>> (
532 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
534 Expression.Constant (source2)));
537 public static IQueryable<TSource> Except<TSource> (this IQueryable<TSource> source1, IEnumerable<TSource> source2, IEqualityComparer<TSource> comparer)
539 Check.Source1AndSource2 (source1, source2);
541 return source1.Provider.Execute<IQueryable<TSource>> (
543 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
545 Expression.Constant (source2),
546 Expression.Constant (comparer)));
553 public static TSource First<TSource> (this IQueryable<TSource> source)
555 Check.Source (source);
557 return source.Provider.Execute<TSource> (
559 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
563 public static TSource First<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, bool>> predicate)
565 Check.SourceAndPredicate (source, predicate);
567 return source.Provider.Execute<TSource> (
569 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
571 Expression.Quote (predicate)));
576 #region FirstOrDefault
578 public static TSource FirstOrDefault<TSource> (this IQueryable<TSource> source)
580 Check.Source (source);
582 return source.Provider.Execute<TSource> (
584 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
588 public static TSource FirstOrDefault<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, bool>> predicate)
590 Check.SourceAndPredicate (source, predicate);
592 return source.Provider.Execute<TSource> (
594 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
596 Expression.Quote (predicate)));
603 public static IQueryable<IGrouping<TKey, TSource>> GroupBy<TSource, TKey> (this IQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector)
605 Check.SourceAndKeySelector (source, keySelector);
607 return source.Provider.Execute<IQueryable<IGrouping<TKey, TSource>>> (
609 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource), typeof (TKey)),
611 Expression.Quote (keySelector)));
613 public static IQueryable<IGrouping<TKey, TSource>> GroupBy<TSource, TKey> (this IQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector, IEqualityComparer<TKey> comparer)
615 Check.SourceAndKeySelector (source, keySelector);
617 return source.Provider.Execute<IQueryable<IGrouping<TKey, TSource>>> (
619 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource), typeof (TKey)),
621 Expression.Quote (keySelector),
622 Expression.Constant (comparer)));
624 public static IQueryable<IGrouping<TKey, TSource>> GroupBy<TSource, TKey, TElement> (this IQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector, Expression<Func<TSource, TElement>> elementSelector)
626 Check.SourceAndKeyElementSelectors (source, keySelector, elementSelector);
628 return source.Provider.Execute<IQueryable<IGrouping<TKey, TSource>>> (
630 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource), typeof (TKey), typeof (TElement)),
632 Expression.Quote (keySelector),
633 Expression.Quote (elementSelector)));
635 public static IQueryable<TResult> GroupBy<TSource, TKey, TResult> (this IQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector, Expression<Func<TKey, IEnumerable<TSource>, TResult>> resultSelector)
637 Check.SourceAndKeyResultSelectors (source, keySelector, resultSelector);
639 return source.Provider.Execute<IQueryable<TResult>> (
641 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource), typeof (TKey), typeof (TResult)),
643 Expression.Quote (keySelector),
644 Expression.Quote (resultSelector)));
646 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)
648 Check.SourceAndKeyElementSelectors (source, keySelector, elementSelector);
650 return source.Provider.Execute<IQueryable<IGrouping<TKey, TElement>>> (
652 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource), typeof (TKey), typeof (TElement)),
654 Expression.Quote (keySelector),
655 Expression.Quote (elementSelector),
656 Expression.Constant (comparer)));
658 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)
660 Check.GroupBySelectors (source, keySelector, elementSelector, resultSelector);
662 return source.Provider.Execute<IQueryable<TResult>> (
664 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource), typeof (TKey), typeof (TElement), typeof (TResult)),
666 Expression.Quote (keySelector),
667 Expression.Quote (elementSelector),
668 Expression.Quote (resultSelector)));
671 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)
673 Check.SourceAndKeyResultSelectors (source, keySelector, resultSelector);
675 return source.Provider.Execute<IQueryable<TResult>> (
677 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource), typeof (TKey), typeof (TResult)),
679 Expression.Quote (keySelector),
680 Expression.Quote (resultSelector),
681 Expression.Constant (comparer)));
683 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)
685 Check.GroupBySelectors (source, keySelector, elementSelector, resultSelector);
687 return source.Provider.Execute<IQueryable<TResult>> (
689 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource), typeof (TKey), typeof (TElement), typeof (TResult)),
691 Expression.Quote (keySelector),
692 Expression.Quote (elementSelector),
693 Expression.Quote (resultSelector),
694 Expression.Constant (comparer)));
700 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)
703 throw new ArgumentNullException ("outer");
705 throw new ArgumentNullException ("inner");
706 if (outerKeySelector == null)
707 throw new ArgumentNullException ("outerKeySelector");
708 if (innerKeySelector == null)
709 throw new ArgumentNullException ("innerKeySelector");
710 if (resultSelector == null)
711 throw new ArgumentNullException ("resultSelector");
713 return outer.Provider.Execute<IQueryable<TResult>> (
715 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TOuter), typeof (TInner), typeof (TKey), typeof (TResult)),
717 Expression.Constant (inner),
718 Expression.Quote (outerKeySelector),
719 Expression.Quote (innerKeySelector),
720 Expression.Quote (resultSelector)));
723 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)
726 throw new ArgumentNullException ("outer");
728 throw new ArgumentNullException ("inner");
729 if (outerKeySelector == null)
730 throw new ArgumentNullException ("outerKeySelector");
731 if (innerKeySelector == null)
732 throw new ArgumentNullException ("innerKeySelector");
733 if (resultSelector == null)
734 throw new ArgumentNullException ("resultSelector");
736 return outer.Provider.Execute<IQueryable<TResult>> (
738 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TOuter), typeof (TInner), typeof (TKey), typeof (TResult)),
740 Expression.Constant (inner),
741 Expression.Quote (outerKeySelector),
742 Expression.Quote (innerKeySelector),
743 Expression.Quote (resultSelector),
744 Expression.Constant (comparer)));
751 public static IQueryable<TSource> Intersect<TSource> (this IQueryable<TSource> source1, IEnumerable<TSource> source2)
753 Check.Source1AndSource2 (source1, source2);
755 return source1.Provider.Execute<IQueryable<TSource>> (
757 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
759 Expression.Constant (source2)));
762 public static IQueryable<TSource> Intersect<TSource> (this IQueryable<TSource> source1, IEnumerable<TSource> source2, IEqualityComparer<TSource> comparer)
764 Check.Source1AndSource2 (source1, source2);
766 return source1.Provider.Execute<IQueryable<TSource>> (
768 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
770 Expression.Constant (source2),
771 Expression.Constant (comparer)));
778 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)
780 Check.JoinSelectors (outer, inner, outerKeySelector, innerKeySelector, resultSelector);
782 return outer.Provider.Execute<IQueryable<TResult>> (
784 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TOuter), typeof (TInner), typeof (TKey), typeof (TResult)),
786 Expression.Constant (inner),
787 Expression.Quote (outerKeySelector),
788 Expression.Quote (innerKeySelector),
789 Expression.Quote (resultSelector)));
792 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)
794 Check.JoinSelectors (outer, inner, outerKeySelector, innerKeySelector, resultSelector);
796 return outer.Provider.Execute<IQueryable<TResult>> (
798 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TOuter), typeof (TInner), typeof (TKey), typeof (TResult)),
800 Expression.Constant (inner),
801 Expression.Quote (outerKeySelector),
802 Expression.Quote (innerKeySelector),
803 Expression.Quote (resultSelector),
804 Expression.Constant (comparer)));
812 public static TSource Last<TSource> (this IQueryable<TSource> source)
814 Check.Source (source);
816 return source.Provider.Execute<TSource> (
818 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
822 public static TSource Last<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, bool>> predicate)
824 Check.SourceAndPredicate (source, predicate);
826 return source.Provider.Execute<TSource> (
828 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
830 Expression.Quote (predicate)));
835 #region LastOrDefault
837 public static TSource LastOrDefault<TSource> (this IQueryable<TSource> source)
839 Check.Source (source);
841 return source.Provider.Execute<TSource> (
843 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
847 public static TSource LastOrDefault<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, bool>> predicate)
849 Check.SourceAndPredicate (source, predicate);
851 return source.Provider.Execute<TSource> (
853 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
855 Expression.Quote (predicate)));
862 public static long LongCount<TSource> (this IQueryable<TSource> source)
864 Check.Source (source);
866 return source.Execute<long, TSource> (MethodBase.GetCurrentMethod ());
869 public static long LongCount<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, bool>> predicate)
871 Check.SourceAndPredicate (source, predicate);
873 return source.Provider.Execute<long> (
875 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
877 Expression.Quote (predicate)));
884 public static TSource Max<TSource> (this IQueryable<TSource> source)
886 Check.Source (source);
888 return source.Provider.Execute<TSource> (
890 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
894 public static TResult Max<TSource, TResult> (this IQueryable<TSource> source, Expression<Func<TSource, TResult>> selector)
896 Check.Source (source);
898 return source.Provider.Execute<TResult> (
900 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource), typeof (TResult)),
902 Expression.Quote (selector)));
910 public static TSource Min<TSource> (this IQueryable<TSource> source)
912 Check.Source (source);
914 return source.Provider.Execute<TSource> (
916 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
920 public static TResult Min<TSource, TResult> (this IQueryable<TSource> source, Expression<Func<TSource, TResult>> selector)
922 Check.SourceAndSelector (source, selector);
924 return source.Provider.Execute<TResult> (
926 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource), typeof (TResult)),
928 Expression.Quote (selector)));
936 public static IQueryable<TResult> OfType<TResult> (this IQueryable source)
938 Check.Source (source);
940 return (IQueryable<TResult>) source.Provider.CreateQuery (
942 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TResult)),
950 public static IQueryable<TSource> OrderBy<TSource, TKey> (this IQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector)
952 Check.SourceAndKeySelector (source, keySelector);
954 return source.Provider.Execute<IQueryable<TSource>> (
956 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource), typeof (TKey)),
958 Expression.Quote (keySelector)));
961 public static IQueryable<TSource> OrderBy<TSource, TKey> (this IQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector, IComparer<TKey> comparer)
963 Check.SourceAndKeySelector (source, keySelector);
965 return source.Provider.Execute<IQueryable<TSource>> (
967 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource), typeof (TKey)),
969 Expression.Quote (keySelector),
970 Expression.Constant (comparer)));
975 #region OrderByDescending
977 public static IQueryable<TSource> OrderByDescending<TSource, TKey> (this IQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector)
979 Check.SourceAndKeySelector (source, keySelector);
981 return source.Provider.Execute<IQueryable<TSource>> (
983 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource), typeof (TKey)),
985 Expression.Quote (keySelector)));
988 public static IQueryable<TSource> OrderByDescending<TSource, TKey> (this IQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector, IComparer<TKey> comparer)
990 Check.SourceAndKeySelector (source, keySelector);
992 return source.Provider.Execute<IQueryable<TSource>> (
994 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource), typeof (TKey)),
996 Expression.Quote (keySelector),
997 Expression.Constant (comparer)));
1004 public static IQueryable<TSource> Reverse<TSource> (this IQueryable<TSource> source)
1006 Check.Source (source);
1008 return source.Provider.Execute<IQueryable<TSource>> (
1010 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
1011 source.Expression));
1018 public static IQueryable<TResult> Select<TSource, TResult> (this IQueryable<TSource> source, Expression<Func<TSource, TResult>> selector)
1020 Check.SourceAndSelector (source, selector);
1022 return source.Provider.Execute<IQueryable<TResult>> (
1024 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource), typeof (TResult)),
1026 Expression.Quote (selector)));
1029 public static IQueryable<TResult> Select<TSource, TResult> (this IQueryable<TSource> source, Expression<Func<TSource, int, TResult>> selector)
1031 Check.SourceAndSelector (source, selector);
1033 return source.Provider.Execute<IQueryable<TResult>> (
1035 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource), typeof (TResult)),
1037 Expression.Quote (selector)));
1044 public static IQueryable<TResult> SelectMany<TSource, TResult> (this IQueryable<TSource> source, Expression<Func<TSource, IEnumerable<TResult>>> selector)
1046 Check.SourceAndSelector (source, selector);
1048 return source.Provider.Execute<IQueryable<TResult>> (
1050 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource), typeof (TResult)),
1052 Expression.Quote (selector)));
1055 public static IQueryable<TResult> SelectMany<TSource, TResult> (this IQueryable<TSource> source, Expression<Func<TSource, int, IEnumerable<TResult>>> selector)
1057 Check.SourceAndSelector (source, selector);
1059 return source.Provider.Execute<IQueryable<TResult>> (
1061 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource), typeof (TResult)),
1063 Expression.Quote (selector)));
1066 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)
1068 Check.SourceAndCollectionSelectorAndResultSelector (source, collectionSelector, resultSelector);
1070 return source.Provider.Execute<IQueryable<TResult>> (
1072 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource), typeof (TCollection), typeof (TResult)),
1074 Expression.Quote (collectionSelector),
1075 Expression.Quote (resultSelector)));
1078 public static IQueryable<TResult> SelectMany<TSource, TCollection, TResult> (this IQueryable<TSource> source, Expression<Func<TSource, IEnumerable<TCollection>>> collectionSelector, Expression<Func<TSource, TCollection, TResult>> resultSelector)
1080 Check.SourceAndCollectionSelectorAndResultSelector (source, collectionSelector, resultSelector);
1082 return source.Provider.Execute<IQueryable<TResult>> (
1084 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource), typeof (TCollection), typeof (TResult)),
1086 Expression.Quote (collectionSelector),
1087 Expression.Quote (resultSelector)));
1092 #region SequenceEqual
1094 public static bool SequenceEqual<TSource> (this IQueryable<TSource> source1, IEnumerable<TSource> source2)
1096 Check.Source1AndSource2 (source1, source2);
1098 return source1.Provider.Execute<bool> (
1100 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
1102 Expression.Constant (source2)));
1105 public static bool SequenceEqual<TSource> (this IQueryable<TSource> source1, IEnumerable<TSource> source2, IEqualityComparer<TSource> comparer)
1107 Check.Source1AndSource2 (source1, source2);
1109 return source1.Provider.Execute<bool> (
1111 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
1113 Expression.Constant (source2),
1114 Expression.Constant (comparer)));
1121 public static TSource Single<TSource> (this IQueryable<TSource> source)
1123 Check.Source (source);
1125 return source.Provider.Execute<TSource> (
1127 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
1128 source.Expression));
1131 public static TSource Single<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, bool>> predicate)
1133 Check.SourceAndPredicate (source, predicate);
1135 return source.Provider.Execute<TSource> (
1137 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
1139 Expression.Quote (predicate)));
1144 #region SingleOrDefault
1146 public static TSource SingleOrDefault<TSource> (this IQueryable<TSource> source)
1148 Check.Source (source);
1150 return source.Provider.Execute<TSource> (
1152 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
1153 source.Expression));
1156 public static TSource SingleOrDefault<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, bool>> predicate)
1158 Check.SourceAndPredicate (source, predicate);
1160 return source.Provider.Execute<TSource> (
1162 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
1164 Expression.Quote (predicate)));
1171 public static IQueryable<TSource> Skip<TSource> (this IQueryable<TSource> source, int count)
1173 Check.Source (source);
1175 return source.Provider.Execute<IQueryable<TSource>> (
1177 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
1179 Expression.Constant (count)));
1186 public static IQueryable<TSource> SkipWhile<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, bool>> predicate)
1188 Check.SourceAndPredicate (source, predicate);
1190 return source.Provider.Execute<IQueryable<TSource>> (
1192 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
1194 Expression.Quote (predicate)));
1197 public static IQueryable<TSource> SkipWhile<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, int, bool>> predicate)
1199 Check.SourceAndPredicate (source, predicate);
1201 return source.Provider.Execute<IQueryable<TSource>> (
1203 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
1205 Expression.Quote (predicate)));
1214 public static int Sum<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, int>> selector)
1216 Check.SourceAndSelector (source, selector);
1218 return source.Provider.Execute<int> (
1220 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
1222 Expression.Quote (selector)));
1225 public static int? Sum<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, int?>> selector)
1227 Check.SourceAndSelector (source, selector);
1229 return source.Provider.Execute<int?> (
1231 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
1233 Expression.Quote (selector)));
1236 public static long Sum<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, long>> selector)
1238 Check.SourceAndSelector (source, selector);
1240 return source.Provider.Execute<long> (
1242 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
1244 Expression.Quote (selector)));
1247 public static long? Sum<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, long?>> selector)
1249 Check.SourceAndSelector (source, selector);
1251 return source.Provider.Execute<long?> (
1253 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
1255 Expression.Quote (selector)));
1258 public static float Sum<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, float>> selector)
1260 Check.SourceAndSelector (source, selector);
1262 return source.Provider.Execute<float> (
1264 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
1266 Expression.Quote (selector)));
1269 public static float? Sum<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, float?>> selector)
1271 Check.SourceAndSelector (source, selector);
1273 return source.Provider.Execute<float?> (
1275 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
1277 Expression.Quote (selector)));
1280 public static double Sum<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, double>> selector)
1282 Check.SourceAndSelector (source, selector);
1284 return source.Provider.Execute<double> (
1286 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
1288 Expression.Quote (selector)));
1291 public static double? Sum<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, double?>> selector)
1293 Check.SourceAndSelector (source, selector);
1295 return source.Provider.Execute<double?> (
1297 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
1299 Expression.Quote (selector)));
1302 public static decimal Sum<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, decimal>> selector)
1304 Check.SourceAndSelector (source, selector);
1306 return source.Provider.Execute<decimal> (
1308 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
1310 Expression.Quote (selector)));
1313 public static decimal? Sum<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, decimal?>> selector)
1315 Check.SourceAndSelector (source, selector);
1317 return source.Provider.Execute<decimal?> (
1319 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
1321 Expression.Quote (selector)));
1324 public static int Sum (this IQueryable<int> source)
1326 Check.Source (source);
1328 return source.Provider.Execute<int> (
1330 (MethodInfo) MethodBase.GetCurrentMethod (),
1331 source.Expression));
1334 public static int? Sum (this IQueryable<int?> source)
1336 Check.Source (source);
1338 return source.Provider.Execute<int?> (
1340 (MethodInfo) MethodBase.GetCurrentMethod (),
1341 source.Expression));
1346 public static long Sum (this IQueryable<long> source)
1348 Check.Source (source);
1350 return source.Provider.Execute<long> (
1352 (MethodInfo) MethodBase.GetCurrentMethod (),
1353 source.Expression));
1358 public static long? Sum (this IQueryable<long?> source)
1360 Check.Source (source);
1362 return source.Provider.Execute<long?> (
1364 (MethodInfo) MethodBase.GetCurrentMethod (),
1365 source.Expression));
1368 public static float Sum (this IQueryable<float> source)
1370 Check.Source (source);
1372 return source.Provider.Execute<float> (
1374 (MethodInfo) MethodBase.GetCurrentMethod (),
1375 source.Expression));
1378 public static float? Sum (this IQueryable<float?> source)
1380 Check.Source (source);
1382 return source.Provider.Execute<float?> (
1384 (MethodInfo) MethodBase.GetCurrentMethod (),
1385 source.Expression));
1389 public static double Sum (this IQueryable<double> source)
1391 Check.Source (source);
1393 return source.Provider.Execute<double> (
1395 (MethodInfo) MethodBase.GetCurrentMethod (),
1396 source.Expression));
1401 public static double? Sum (this IQueryable<double?> source)
1403 Check.Source (source);
1405 return source.Provider.Execute<double?> (
1407 (MethodInfo) MethodBase.GetCurrentMethod (),
1408 source.Expression));
1412 public static decimal Sum (this IQueryable<decimal> source)
1414 Check.Source (source);
1416 return source.Provider.Execute<decimal> (
1418 (MethodInfo) MethodBase.GetCurrentMethod (),
1419 source.Expression));
1424 public static decimal? Sum (this IQueryable<decimal?> source)
1426 Check.Source (source);
1428 return source.Provider.Execute<decimal?> (
1430 (MethodInfo) MethodBase.GetCurrentMethod (),
1431 source.Expression));
1440 public static IQueryable<TSource> Take<TSource> (this IQueryable<TSource> source, int count)
1442 Check.Source (source);
1444 return source.Provider.Execute<IQueryable<TSource>> (
1446 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
1448 Expression.Constant (count)));
1455 public static IQueryable<TSource> TakeWhile<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, bool>> predicate)
1457 Check.SourceAndPredicate (source, predicate);
1459 return source.Provider.Execute<IQueryable<TSource>> (
1461 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
1463 Expression.Quote (predicate)));
1466 public static IQueryable<TSource> TakeWhile<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, int, bool>> predicate)
1468 Check.SourceAndPredicate (source, predicate);
1470 return source.Provider.Execute<IQueryable<TSource>> (
1472 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
1474 Expression.Quote (predicate)));
1481 public static IOrderedQueryable<TSource> ThenBy<TSource, TKey> (this IOrderedQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector)
1483 Check.SourceAndKeySelector (source, keySelector);
1485 return source.Provider.Execute<IOrderedQueryable<TSource>> (
1487 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource), typeof (TKey)),
1489 Expression.Quote (keySelector)));
1492 public static IOrderedQueryable<TSource> ThenBy<TSource, TKey> (this IOrderedQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector, IComparer<TKey> comparer)
1494 Check.SourceAndKeySelector (source, keySelector);
1496 return source.Provider.Execute<IOrderedQueryable<TSource>> (
1498 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource), typeof (TKey)),
1500 Expression.Quote (keySelector),
1501 Expression.Constant (comparer)));
1506 #region ThenByDescending
1508 public static IOrderedQueryable<TSource> ThenByDescending<TSource, TKey> (this IOrderedQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector)
1510 Check.SourceAndKeySelector (source, keySelector);
1512 return source.Provider.Execute<IOrderedQueryable<TSource>> (
1514 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource), typeof (TKey)),
1516 Expression.Quote (keySelector)));
1519 public static IOrderedQueryable<TSource> ThenByDescending<TSource, TKey> (this IOrderedQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector, IComparer<TKey> comparer)
1521 Check.SourceAndKeySelector (source, keySelector);
1523 return source.Provider.Execute<IOrderedQueryable<TSource>> (
1525 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource), typeof (TKey)),
1527 Expression.Quote (keySelector),
1528 Expression.Constant (comparer)));
1535 public static IEnumerable<TSource> Union<TSource> (this IQueryable<TSource> source1, IEnumerable<TSource> source2)
1537 Check.Source1AndSource2 (source1, source2);
1539 return source1.Provider.Execute<IOrderedQueryable<TSource>> (
1541 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
1543 Expression.Constant (source2)));
1546 public static IEnumerable<TSource> Union<TSource> (this IQueryable<TSource> source1, IEnumerable<TSource> source2, IEqualityComparer<TSource> comparer)
1548 Check.Source1AndSource2 (source1, source2);
1550 return source1.Provider.Execute<IOrderedQueryable<TSource>> (
1552 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
1554 Expression.Constant (source2),
1555 Expression.Constant (comparer))); throw new NotImplementedException ();
1563 public static IQueryable<TSource> Where<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, bool>> predicate)
1565 Check.SourceAndPredicate (source, predicate);
1567 return source.Provider.Execute<IQueryable<TSource>> (
1569 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
1571 Expression.Quote (predicate)));
1574 public static IQueryable<TSource> Where<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, int, bool>> predicate)
1576 Check.SourceAndPredicate (source, predicate);
1578 return source.Provider.Execute<IQueryable<TSource>> (
1580 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
1582 Expression.Quote (predicate)));