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;
41 public static class Queryable
44 static MethodInfo MakeGeneric (MethodBase method, params Type [] parameters)
46 return ((MethodInfo) method).MakeGenericMethod (parameters);
49 static Expression StaticCall (MethodInfo method, params Expression [] expressions)
51 return Expression.Call (null, method, expressions);
54 static TRet Execute<TRet, TSource> (this IQueryable<TSource> source, MethodBase current)
56 return source.Provider.Execute<TRet> (
58 MakeGeneric (current, typeof (TSource)),
64 public static TSource Aggregate<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, TSource, TSource>> func)
66 Check.SourceAndFunc (source, func);
67 return source.Provider.Execute<TSource> (
69 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
71 Expression.Quote (func)));
74 public static TAccumulate Aggregate<TSource, TAccumulate> (this IQueryable<TSource> source, TAccumulate seed, Expression<Func<TAccumulate, TSource, TAccumulate>> func)
76 Check.SourceAndFunc (source, func);
77 return source.Provider.Execute<TAccumulate> (
79 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource), typeof (TAccumulate)),
81 Expression.Constant (seed),
82 Expression.Quote (func)));
85 public static TResult Aggregate<TSource, TAccumulate, TResult> (this IQueryable<TSource> source, TAccumulate seed, Expression<Func<TAccumulate, TSource, TAccumulate>> func, Expression<Func<TAccumulate, TResult>> selector)
87 Check.SourceAndFuncAndSelector (source, func, selector);
88 return source.Provider.Execute<TResult> (
90 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource), typeof (TAccumulate), typeof (TResult)),
92 Expression.Constant (seed),
93 Expression.Quote (func),
94 Expression.Quote (selector)));
101 public static bool All<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, bool>> predicate)
103 Check.SourceAndPredicate (source, predicate);
105 return source.Provider.Execute<bool> (
107 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
109 Expression.Quote (predicate)));
116 public static bool Any<TSource> (this IQueryable<TSource> source)
118 Check.Source (source);
120 return source.Provider.Execute<bool> (
122 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
126 public static bool Any<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, bool>> predicate)
128 Check.SourceAndPredicate (source, predicate);
130 return source.Provider.Execute<bool> (
132 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
134 Expression.Quote (predicate)));
141 public static IQueryable<TElement> AsQueryable<TElement> (this IEnumerable<TElement> source)
143 if (source is IQueryable<TElement>)
144 return (IQueryable<TElement>) source;
145 throw new NotImplementedException ();
148 public static IQueryable AsQueryable (this IQueryable source)
150 throw new NotImplementedException ();
157 public static int Average (this IQueryable<int> source)
159 Check.Source (source);
161 return source.Provider.Execute<int> (
163 (MethodInfo) MethodBase.GetCurrentMethod (),
167 public static int? Average (this IQueryable<int?> source)
169 Check.Source (source);
171 return source.Provider.Execute<int?> (
173 (MethodInfo) MethodBase.GetCurrentMethod (),
177 public static long Average (this IQueryable<long> source)
179 Check.Source (source);
181 return source.Provider.Execute<long> (
183 (MethodInfo) MethodBase.GetCurrentMethod (),
187 public static long? Average (this IQueryable<long?> source)
189 Check.Source (source);
191 return source.Provider.Execute<long?> (
193 (MethodInfo) MethodBase.GetCurrentMethod (),
197 public static float Average (this IQueryable<float> source)
199 Check.Source (source);
201 return source.Provider.Execute<float> (
203 (MethodInfo) (MethodInfo) MethodBase.GetCurrentMethod (),
207 public static float? Average (this IQueryable<float?> source)
209 Check.Source (source);
211 return source.Provider.Execute<float?> (
213 (MethodInfo) MethodBase.GetCurrentMethod (),
217 public static double Average (this IQueryable<double> source)
219 Check.Source (source);
221 return source.Provider.Execute<double> (
223 (MethodInfo) MethodBase.GetCurrentMethod (),
227 public static double? Average (this IQueryable<double?> source)
229 Check.Source (source);
231 return source.Provider.Execute<double?> (
233 (MethodInfo) MethodBase.GetCurrentMethod (),
237 public static decimal Average (this IQueryable<decimal> source)
239 Check.Source (source);
241 return source.Provider.Execute<decimal> (
243 (MethodInfo) MethodBase.GetCurrentMethod (),
247 public static decimal? Average (this IQueryable<decimal?> source)
249 Check.Source (source);
251 return source.Provider.Execute<decimal?> (
253 (MethodInfo) MethodBase.GetCurrentMethod (),
257 public static int Average<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, int>> selector)
259 Check.SourceAndSelector (source, selector);
261 return source.Provider.Execute<int> (
263 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
265 Expression.Quote (selector)));
268 public static int? Average<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, int?>> selector)
270 Check.SourceAndSelector (source, selector);
272 return source.Provider.Execute<int?> (
274 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
276 Expression.Quote (selector)));
279 public static long Average<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, long>> selector)
281 Check.SourceAndSelector (source, selector);
283 return source.Provider.Execute<long> (
285 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
287 Expression.Quote (selector)));
290 public static long? Average<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, long?>> selector)
292 Check.SourceAndSelector (source, selector);
294 return source.Provider.Execute<long?> (
296 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
298 Expression.Quote (selector)));
301 public static float Average<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, float>> selector)
303 Check.SourceAndSelector (source, selector);
305 return source.Provider.Execute<float> (
307 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
309 Expression.Quote (selector)));
312 public static float? Average<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, float?>> selector)
314 Check.SourceAndSelector (source, selector);
316 return source.Provider.Execute<float?> (
318 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
320 Expression.Quote (selector)));
323 public static double Average<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, double>> selector)
325 Check.SourceAndSelector (source, selector);
327 return source.Provider.Execute<double> (
329 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
331 Expression.Quote (selector)));
334 public static double? Average<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, double?>> selector)
336 Check.SourceAndSelector (source, selector);
338 return source.Provider.Execute<double?> (
340 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
342 Expression.Quote (selector)));
345 public static decimal Average<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, decimal>> selector)
347 Check.SourceAndSelector (source, selector);
349 return source.Provider.Execute<decimal> (
351 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
353 Expression.Quote (selector)));
356 public static decimal? Average<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, decimal?>> selector)
358 Check.SourceAndSelector (source, selector);
360 return source.Provider.Execute<decimal?> (
362 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
364 Expression.Quote (selector)));
371 public static IQueryable<TResult> Cast<TResult> (this IQueryable source)
373 Check.Source (source);
375 return (IQueryable<TResult>) source.Provider.CreateQuery (
376 StaticCall (MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TResult)),
384 public static IQueryable<TSource> Concat<TSource> (this IQueryable<TSource> source1, IEnumerable<TSource> source2)
386 Check.Source1AndSource2 (source1, source2);
388 return source1.Provider.CreateQuery<TSource> (
390 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
392 Expression.Constant (source2)));
399 public static bool Contains<TSource> (this IQueryable<TSource> source, TSource item)
401 Check.Source (source);
403 return source.Provider.Execute<bool> (
405 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
407 Expression.Constant (item)));
410 public static bool Contains<TSource> (this IQueryable<TSource> source, TSource item, IEqualityComparer<TSource> comparer)
412 Check.Source (source);
414 return source.Provider.Execute<bool> (
416 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
418 Expression.Constant (item),
419 Expression.Constant (comparer)));
426 public static int Count<TSource> (this IQueryable<TSource> source)
428 Check.Source (source);
430 return source.Execute<int, TSource> (MethodBase.GetCurrentMethod ());
433 public static int Count<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, bool>> predicate)
435 Check.SourceAndPredicate (source, predicate);
437 return source.Provider.Execute<int> (
439 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
441 Expression.Quote (predicate)));
446 #region DefaultIfEmpty
448 public static IQueryable<TSource> DefaultIfEmpty<TSource> (this IQueryable<TSource> source)
450 Check.Source (source);
452 return source.Provider.CreateQuery<TSource> (
454 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
458 public static IQueryable<TSource> DefaultIfEmpty<TSource> (this IQueryable<TSource> source, TSource defaultValue)
460 Check.Source (source);
462 return source.Provider.CreateQuery<TSource> (
464 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
466 Expression.Constant (defaultValue)));
473 public static IQueryable<TSource> Distinct<TSource> (this IQueryable<TSource> source)
475 Check.Source (source);
477 return source.Provider.CreateQuery<TSource> (
479 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
483 public static IQueryable<TSource> Distinct<TSource> (this IQueryable<TSource> source, IEqualityComparer<TSource> comparer)
485 Check.Source (source);
487 return source.Provider.CreateQuery<TSource> (
489 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
491 Expression.Constant (comparer)));
498 public static TSource ElementAt<TSource> (this IQueryable<TSource> source, int index)
500 Check.Source (source);
502 return source.Provider.Execute<TSource> (
504 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
506 Expression.Constant (index)));
511 #region ElementAtOrDefault
513 public static TSource ElementAtOrDefault<TSource> (this IQueryable<TSource> source, int index)
515 Check.Source (source);
517 return source.Provider.Execute<TSource> (
519 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
521 Expression.Constant (index)));
528 public static IQueryable<TSource> Except<TSource> (this IQueryable<TSource> source1, IEnumerable<TSource> source2)
530 Check.Source1AndSource2 (source1, source2);
532 return source1.Provider.CreateQuery<TSource> (
534 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
536 Expression.Constant (source2)));
539 public static IQueryable<TSource> Except<TSource> (this IQueryable<TSource> source1, IEnumerable<TSource> source2, IEqualityComparer<TSource> comparer)
541 Check.Source1AndSource2 (source1, source2);
543 return source1.Provider.CreateQuery<TSource> (
545 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
547 Expression.Constant (source2),
548 Expression.Constant (comparer)));
555 public static TSource First<TSource> (this IQueryable<TSource> source)
557 Check.Source (source);
559 return source.Provider.Execute<TSource> (
561 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
565 public static TSource First<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, bool>> predicate)
567 Check.SourceAndPredicate (source, predicate);
569 return source.Provider.Execute<TSource> (
571 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
573 Expression.Quote (predicate)));
578 #region FirstOrDefault
580 public static TSource FirstOrDefault<TSource> (this IQueryable<TSource> source)
582 Check.Source (source);
584 return source.Provider.Execute<TSource> (
586 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
590 public static TSource FirstOrDefault<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, bool>> predicate)
592 Check.SourceAndPredicate (source, predicate);
594 return source.Provider.Execute<TSource> (
596 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
598 Expression.Quote (predicate)));
605 public static IQueryable<IGrouping<TKey, TSource>> GroupBy<TSource, TKey> (this IQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector)
607 Check.SourceAndKeySelector (source, keySelector);
609 return source.Provider.CreateQuery<IGrouping<TKey, TSource>> (
611 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource), typeof (TKey)),
613 Expression.Quote (keySelector)));
615 public static IQueryable<IGrouping<TKey, TSource>> GroupBy<TSource, TKey> (this IQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector, IEqualityComparer<TKey> comparer)
617 Check.SourceAndKeySelector (source, keySelector);
619 return source.Provider.CreateQuery<IGrouping<TKey, TSource>> (
621 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource), typeof (TKey)),
623 Expression.Quote (keySelector),
624 Expression.Constant (comparer)));
626 public static IQueryable<IGrouping<TKey, TSource>> GroupBy<TSource, TKey, TElement> (this IQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector, Expression<Func<TSource, TElement>> elementSelector)
628 Check.SourceAndKeyElementSelectors (source, keySelector, elementSelector);
630 return source.Provider.CreateQuery<IGrouping<TKey, TSource>> (
632 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource), typeof (TKey), typeof (TElement)),
634 Expression.Quote (keySelector),
635 Expression.Quote (elementSelector)));
637 public static IQueryable<TResult> GroupBy<TSource, TKey, TResult> (this IQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector, Expression<Func<TKey, IEnumerable<TSource>, TResult>> resultSelector)
639 Check.SourceAndKeyResultSelectors (source, keySelector, resultSelector);
641 return source.Provider.CreateQuery<TResult> (
643 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource), typeof (TKey), typeof (TResult)),
645 Expression.Quote (keySelector),
646 Expression.Quote (resultSelector)));
648 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)
650 Check.SourceAndKeyElementSelectors (source, keySelector, elementSelector);
652 return source.Provider.CreateQuery<IGrouping<TKey, TElement>> (
654 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource), typeof (TKey), typeof (TElement)),
656 Expression.Quote (keySelector),
657 Expression.Quote (elementSelector),
658 Expression.Constant (comparer)));
660 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)
662 Check.GroupBySelectors (source, keySelector, elementSelector, resultSelector);
664 return source.Provider.CreateQuery<TResult> (
666 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource), typeof (TKey), typeof (TElement), typeof (TResult)),
668 Expression.Quote (keySelector),
669 Expression.Quote (elementSelector),
670 Expression.Quote (resultSelector)));
673 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)
675 Check.SourceAndKeyResultSelectors (source, keySelector, resultSelector);
677 return source.Provider.CreateQuery<TResult> (
679 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource), typeof (TKey), typeof (TResult)),
681 Expression.Quote (keySelector),
682 Expression.Quote (resultSelector),
683 Expression.Constant (comparer)));
685 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)
687 Check.GroupBySelectors (source, keySelector, elementSelector, resultSelector);
689 return source.Provider.CreateQuery<TResult> (
691 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource), typeof (TKey), typeof (TElement), typeof (TResult)),
693 Expression.Quote (keySelector),
694 Expression.Quote (elementSelector),
695 Expression.Quote (resultSelector),
696 Expression.Constant (comparer)));
702 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)
705 throw new ArgumentNullException ("outer");
707 throw new ArgumentNullException ("inner");
708 if (outerKeySelector == null)
709 throw new ArgumentNullException ("outerKeySelector");
710 if (innerKeySelector == null)
711 throw new ArgumentNullException ("innerKeySelector");
712 if (resultSelector == null)
713 throw new ArgumentNullException ("resultSelector");
715 return outer.Provider.CreateQuery<TResult> (
717 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TOuter), typeof (TInner), typeof (TKey), typeof (TResult)),
719 Expression.Constant (inner),
720 Expression.Quote (outerKeySelector),
721 Expression.Quote (innerKeySelector),
722 Expression.Quote (resultSelector)));
725 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)
728 throw new ArgumentNullException ("outer");
730 throw new ArgumentNullException ("inner");
731 if (outerKeySelector == null)
732 throw new ArgumentNullException ("outerKeySelector");
733 if (innerKeySelector == null)
734 throw new ArgumentNullException ("innerKeySelector");
735 if (resultSelector == null)
736 throw new ArgumentNullException ("resultSelector");
738 return outer.Provider.CreateQuery<TResult> (
740 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TOuter), typeof (TInner), typeof (TKey), typeof (TResult)),
742 Expression.Constant (inner),
743 Expression.Quote (outerKeySelector),
744 Expression.Quote (innerKeySelector),
745 Expression.Quote (resultSelector),
746 Expression.Constant (comparer)));
753 public static IQueryable<TSource> Intersect<TSource> (this IQueryable<TSource> source1, IEnumerable<TSource> source2)
755 Check.Source1AndSource2 (source1, source2);
757 return source1.Provider.CreateQuery<TSource> (
759 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
761 Expression.Constant (source2)));
764 public static IQueryable<TSource> Intersect<TSource> (this IQueryable<TSource> source1, IEnumerable<TSource> source2, IEqualityComparer<TSource> comparer)
766 Check.Source1AndSource2 (source1, source2);
768 return source1.Provider.CreateQuery<TSource> (
770 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
772 Expression.Constant (source2),
773 Expression.Constant (comparer)));
780 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)
782 Check.JoinSelectors (outer, inner, outerKeySelector, innerKeySelector, resultSelector);
784 return outer.Provider.CreateQuery<TResult> (
786 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TOuter), typeof (TInner), typeof (TKey), typeof (TResult)),
788 Expression.Constant (inner),
789 Expression.Quote (outerKeySelector),
790 Expression.Quote (innerKeySelector),
791 Expression.Quote (resultSelector)));
794 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)
796 Check.JoinSelectors (outer, inner, outerKeySelector, innerKeySelector, resultSelector);
798 return outer.Provider.CreateQuery<TResult> (
800 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TOuter), typeof (TInner), typeof (TKey), typeof (TResult)),
802 Expression.Constant (inner),
803 Expression.Quote (outerKeySelector),
804 Expression.Quote (innerKeySelector),
805 Expression.Quote (resultSelector),
806 Expression.Constant (comparer)));
814 public static TSource Last<TSource> (this IQueryable<TSource> source)
816 Check.Source (source);
818 return source.Provider.Execute<TSource> (
820 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
824 public static TSource Last<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, bool>> predicate)
826 Check.SourceAndPredicate (source, predicate);
828 return source.Provider.Execute<TSource> (
830 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
832 Expression.Quote (predicate)));
837 #region LastOrDefault
839 public static TSource LastOrDefault<TSource> (this IQueryable<TSource> source)
841 Check.Source (source);
843 return source.Provider.Execute<TSource> (
845 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
849 public static TSource LastOrDefault<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, bool>> predicate)
851 Check.SourceAndPredicate (source, predicate);
853 return source.Provider.Execute<TSource> (
855 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
857 Expression.Quote (predicate)));
864 public static long LongCount<TSource> (this IQueryable<TSource> source)
866 Check.Source (source);
868 return source.Execute<long, TSource> (MethodBase.GetCurrentMethod ());
871 public static long LongCount<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, bool>> predicate)
873 Check.SourceAndPredicate (source, predicate);
875 return source.Provider.Execute<long> (
877 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
879 Expression.Quote (predicate)));
886 public static TSource Max<TSource> (this IQueryable<TSource> source)
888 Check.Source (source);
890 return source.Provider.Execute<TSource> (
892 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
896 public static TResult Max<TSource, TResult> (this IQueryable<TSource> source, Expression<Func<TSource, TResult>> selector)
898 Check.Source (source);
900 return source.Provider.Execute<TResult> (
902 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource), typeof (TResult)),
904 Expression.Quote (selector)));
912 public static TSource Min<TSource> (this IQueryable<TSource> source)
914 Check.Source (source);
916 return source.Provider.Execute<TSource> (
918 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
922 public static TResult Min<TSource, TResult> (this IQueryable<TSource> source, Expression<Func<TSource, TResult>> selector)
924 Check.SourceAndSelector (source, selector);
926 return source.Provider.Execute<TResult> (
928 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource), typeof (TResult)),
930 Expression.Quote (selector)));
938 public static IQueryable<TResult> OfType<TResult> (this IQueryable source)
940 Check.Source (source);
942 return (IQueryable<TResult>) source.Provider.CreateQuery (
944 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TResult)),
952 public static IQueryable<TSource> OrderBy<TSource, TKey> (this IQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector)
954 Check.SourceAndKeySelector (source, keySelector);
956 return source.Provider.CreateQuery<TSource> (
958 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource), typeof (TKey)),
960 Expression.Quote (keySelector)));
963 public static IQueryable<TSource> OrderBy<TSource, TKey> (this IQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector, IComparer<TKey> comparer)
965 Check.SourceAndKeySelector (source, keySelector);
967 return source.Provider.CreateQuery<TSource> (
969 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource), typeof (TKey)),
971 Expression.Quote (keySelector),
972 Expression.Constant (comparer)));
977 #region OrderByDescending
979 public static IQueryable<TSource> OrderByDescending<TSource, TKey> (this IQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector)
981 Check.SourceAndKeySelector (source, keySelector);
983 return source.Provider.CreateQuery<TSource> (
985 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource), typeof (TKey)),
987 Expression.Quote (keySelector)));
990 public static IQueryable<TSource> OrderByDescending<TSource, TKey> (this IQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector, IComparer<TKey> comparer)
992 Check.SourceAndKeySelector (source, keySelector);
994 return source.Provider.CreateQuery<TSource> (
996 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource), typeof (TKey)),
998 Expression.Quote (keySelector),
999 Expression.Constant (comparer)));
1006 public static IQueryable<TSource> Reverse<TSource> (this IQueryable<TSource> source)
1008 Check.Source (source);
1010 return source.Provider.CreateQuery<TSource> (
1012 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
1013 source.Expression));
1020 public static IQueryable<TResult> Select<TSource, TResult> (this IQueryable<TSource> source, Expression<Func<TSource, TResult>> selector)
1022 Check.SourceAndSelector (source, selector);
1024 return source.Provider.CreateQuery<TResult> (
1026 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource), typeof (TResult)),
1028 Expression.Quote (selector)));
1031 public static IQueryable<TResult> Select<TSource, TResult> (this IQueryable<TSource> source, Expression<Func<TSource, int, TResult>> selector)
1033 Check.SourceAndSelector (source, selector);
1035 return source.Provider.CreateQuery<TResult> (
1037 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource), typeof (TResult)),
1039 Expression.Quote (selector)));
1046 public static IQueryable<TResult> SelectMany<TSource, TResult> (this IQueryable<TSource> source, Expression<Func<TSource, IEnumerable<TResult>>> selector)
1048 Check.SourceAndSelector (source, selector);
1050 return source.Provider.CreateQuery<TResult> (
1052 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource), typeof (TResult)),
1054 Expression.Quote (selector)));
1057 public static IQueryable<TResult> SelectMany<TSource, TResult> (this IQueryable<TSource> source, Expression<Func<TSource, int, IEnumerable<TResult>>> selector)
1059 Check.SourceAndSelector (source, selector);
1061 return source.Provider.CreateQuery<TResult> (
1063 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource), typeof (TResult)),
1065 Expression.Quote (selector)));
1068 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)
1070 Check.SourceAndCollectionSelectorAndResultSelector (source, collectionSelector, resultSelector);
1072 return source.Provider.CreateQuery<TResult> (
1074 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource), typeof (TCollection), typeof (TResult)),
1076 Expression.Quote (collectionSelector),
1077 Expression.Quote (resultSelector)));
1080 public static IQueryable<TResult> SelectMany<TSource, TCollection, TResult> (this IQueryable<TSource> source, Expression<Func<TSource, IEnumerable<TCollection>>> collectionSelector, Expression<Func<TSource, TCollection, TResult>> resultSelector)
1082 Check.SourceAndCollectionSelectorAndResultSelector (source, collectionSelector, resultSelector);
1084 return source.Provider.CreateQuery<TResult> (
1086 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource), typeof (TCollection), typeof (TResult)),
1088 Expression.Quote (collectionSelector),
1089 Expression.Quote (resultSelector)));
1094 #region SequenceEqual
1096 public static bool SequenceEqual<TSource> (this IQueryable<TSource> source1, IEnumerable<TSource> source2)
1098 Check.Source1AndSource2 (source1, source2);
1100 return source1.Provider.Execute<bool> (
1102 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
1104 Expression.Constant (source2)));
1107 public static bool SequenceEqual<TSource> (this IQueryable<TSource> source1, IEnumerable<TSource> source2, IEqualityComparer<TSource> comparer)
1109 Check.Source1AndSource2 (source1, source2);
1111 return source1.Provider.Execute<bool> (
1113 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
1115 Expression.Constant (source2),
1116 Expression.Constant (comparer)));
1123 public static TSource Single<TSource> (this IQueryable<TSource> source)
1125 Check.Source (source);
1127 return source.Provider.Execute<TSource> (
1129 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
1130 source.Expression));
1133 public static TSource Single<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, bool>> predicate)
1135 Check.SourceAndPredicate (source, predicate);
1137 return source.Provider.Execute<TSource> (
1139 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
1141 Expression.Quote (predicate)));
1146 #region SingleOrDefault
1148 public static TSource SingleOrDefault<TSource> (this IQueryable<TSource> source)
1150 Check.Source (source);
1152 return source.Provider.Execute<TSource> (
1154 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
1155 source.Expression));
1158 public static TSource SingleOrDefault<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, bool>> predicate)
1160 Check.SourceAndPredicate (source, predicate);
1162 return source.Provider.Execute<TSource> (
1164 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
1166 Expression.Quote (predicate)));
1173 public static IQueryable<TSource> Skip<TSource> (this IQueryable<TSource> source, int count)
1175 Check.Source (source);
1177 return source.Provider.CreateQuery<TSource> (
1179 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
1181 Expression.Constant (count)));
1188 public static IQueryable<TSource> SkipWhile<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, bool>> predicate)
1190 Check.SourceAndPredicate (source, predicate);
1192 return source.Provider.CreateQuery<TSource> (
1194 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
1196 Expression.Quote (predicate)));
1199 public static IQueryable<TSource> SkipWhile<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, int, bool>> predicate)
1201 Check.SourceAndPredicate (source, predicate);
1203 return source.Provider.CreateQuery<TSource> (
1205 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
1207 Expression.Quote (predicate)));
1216 public static int Sum<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, int>> selector)
1218 Check.SourceAndSelector (source, selector);
1220 return source.Provider.Execute<int> (
1222 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
1224 Expression.Quote (selector)));
1227 public static int? Sum<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, int?>> selector)
1229 Check.SourceAndSelector (source, selector);
1231 return source.Provider.Execute<int?> (
1233 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
1235 Expression.Quote (selector)));
1238 public static long Sum<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, long>> selector)
1240 Check.SourceAndSelector (source, selector);
1242 return source.Provider.Execute<long> (
1244 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
1246 Expression.Quote (selector)));
1249 public static long? Sum<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, long?>> selector)
1251 Check.SourceAndSelector (source, selector);
1253 return source.Provider.Execute<long?> (
1255 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
1257 Expression.Quote (selector)));
1260 public static float Sum<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, float>> selector)
1262 Check.SourceAndSelector (source, selector);
1264 return source.Provider.Execute<float> (
1266 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
1268 Expression.Quote (selector)));
1271 public static float? Sum<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, float?>> selector)
1273 Check.SourceAndSelector (source, selector);
1275 return source.Provider.Execute<float?> (
1277 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
1279 Expression.Quote (selector)));
1282 public static double Sum<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, double>> selector)
1284 Check.SourceAndSelector (source, selector);
1286 return source.Provider.Execute<double> (
1288 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
1290 Expression.Quote (selector)));
1293 public static double? Sum<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, double?>> selector)
1295 Check.SourceAndSelector (source, selector);
1297 return source.Provider.Execute<double?> (
1299 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
1301 Expression.Quote (selector)));
1304 public static decimal Sum<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, decimal>> selector)
1306 Check.SourceAndSelector (source, selector);
1308 return source.Provider.Execute<decimal> (
1310 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
1312 Expression.Quote (selector)));
1315 public static decimal? Sum<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, decimal?>> selector)
1317 Check.SourceAndSelector (source, selector);
1319 return source.Provider.Execute<decimal?> (
1321 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
1323 Expression.Quote (selector)));
1326 public static int Sum (this IQueryable<int> source)
1328 Check.Source (source);
1330 return source.Provider.Execute<int> (
1332 (MethodInfo) MethodBase.GetCurrentMethod (),
1333 source.Expression));
1336 public static int? Sum (this IQueryable<int?> source)
1338 Check.Source (source);
1340 return source.Provider.Execute<int?> (
1342 (MethodInfo) MethodBase.GetCurrentMethod (),
1343 source.Expression));
1348 public static long Sum (this IQueryable<long> source)
1350 Check.Source (source);
1352 return source.Provider.Execute<long> (
1354 (MethodInfo) MethodBase.GetCurrentMethod (),
1355 source.Expression));
1360 public static long? Sum (this IQueryable<long?> source)
1362 Check.Source (source);
1364 return source.Provider.Execute<long?> (
1366 (MethodInfo) MethodBase.GetCurrentMethod (),
1367 source.Expression));
1370 public static float Sum (this IQueryable<float> source)
1372 Check.Source (source);
1374 return source.Provider.Execute<float> (
1376 (MethodInfo) MethodBase.GetCurrentMethod (),
1377 source.Expression));
1380 public static float? Sum (this IQueryable<float?> source)
1382 Check.Source (source);
1384 return source.Provider.Execute<float?> (
1386 (MethodInfo) MethodBase.GetCurrentMethod (),
1387 source.Expression));
1391 public static double Sum (this IQueryable<double> source)
1393 Check.Source (source);
1395 return source.Provider.Execute<double> (
1397 (MethodInfo) MethodBase.GetCurrentMethod (),
1398 source.Expression));
1403 public static double? Sum (this IQueryable<double?> source)
1405 Check.Source (source);
1407 return source.Provider.Execute<double?> (
1409 (MethodInfo) MethodBase.GetCurrentMethod (),
1410 source.Expression));
1414 public static decimal Sum (this IQueryable<decimal> source)
1416 Check.Source (source);
1418 return source.Provider.Execute<decimal> (
1420 (MethodInfo) MethodBase.GetCurrentMethod (),
1421 source.Expression));
1426 public static decimal? Sum (this IQueryable<decimal?> source)
1428 Check.Source (source);
1430 return source.Provider.Execute<decimal?> (
1432 (MethodInfo) MethodBase.GetCurrentMethod (),
1433 source.Expression));
1442 public static IQueryable<TSource> Take<TSource> (this IQueryable<TSource> source, int count)
1444 Check.Source (source);
1446 return source.Provider.CreateQuery<TSource> (
1448 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
1450 Expression.Constant (count)));
1457 public static IQueryable<TSource> TakeWhile<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, bool>> predicate)
1459 Check.SourceAndPredicate (source, predicate);
1461 return source.Provider.CreateQuery<TSource> (
1463 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
1465 Expression.Quote (predicate)));
1468 public static IQueryable<TSource> TakeWhile<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, int, bool>> predicate)
1470 Check.SourceAndPredicate (source, predicate);
1472 return source.Provider.CreateQuery<TSource> (
1474 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
1476 Expression.Quote (predicate)));
1483 public static IOrderedQueryable<TSource> ThenBy<TSource, TKey> (this IOrderedQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector)
1485 Check.SourceAndKeySelector (source, keySelector);
1487 return (IOrderedQueryable<TSource>) source.Provider.CreateQuery (
1489 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource), typeof (TKey)),
1491 Expression.Quote (keySelector)));
1494 public static IOrderedQueryable<TSource> ThenBy<TSource, TKey> (this IOrderedQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector, IComparer<TKey> comparer)
1496 Check.SourceAndKeySelector (source, keySelector);
1498 return (IOrderedQueryable<TSource>) source.Provider.CreateQuery (
1500 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource), typeof (TKey)),
1502 Expression.Quote (keySelector),
1503 Expression.Constant (comparer)));
1508 #region ThenByDescending
1510 public static IOrderedQueryable<TSource> ThenByDescending<TSource, TKey> (this IOrderedQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector)
1512 Check.SourceAndKeySelector (source, keySelector);
1514 return (IOrderedQueryable<TSource>) source.Provider.CreateQuery (
1516 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource), typeof (TKey)),
1518 Expression.Quote (keySelector)));
1521 public static IOrderedQueryable<TSource> ThenByDescending<TSource, TKey> (this IOrderedQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector, IComparer<TKey> comparer)
1523 Check.SourceAndKeySelector (source, keySelector);
1525 return (IOrderedQueryable<TSource>) source.Provider.CreateQuery (
1527 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource), typeof (TKey)),
1529 Expression.Quote (keySelector),
1530 Expression.Constant (comparer)));
1537 public static IEnumerable<TSource> Union<TSource> (this IQueryable<TSource> source1, IEnumerable<TSource> source2)
1539 Check.Source1AndSource2 (source1, source2);
1541 return source1.Provider.CreateQuery<TSource> (
1543 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
1545 Expression.Constant (source2)));
1548 public static IEnumerable<TSource> Union<TSource> (this IQueryable<TSource> source1, IEnumerable<TSource> source2, IEqualityComparer<TSource> comparer)
1550 Check.Source1AndSource2 (source1, source2);
1552 return source1.Provider.CreateQuery<TSource> (
1554 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
1556 Expression.Constant (source2),
1557 Expression.Constant (comparer)));
1565 public static IQueryable<TSource> Where<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, bool>> predicate)
1567 Check.SourceAndPredicate (source, predicate);
1569 return source.Provider.CreateQuery<TSource> (
1571 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
1573 Expression.Quote (predicate)));
1576 public static IQueryable<TSource> Where<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, int, bool>> predicate)
1578 Check.SourceAndPredicate (source, predicate);
1580 return source.Provider.CreateQuery<TSource> (
1582 MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
1584 Expression.Quote (predicate)));