2
0

IQueryable.cs 77 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq.Expressions;
  5. using System.Reflection;
  6. using System.Text;
  7. // Include Silverlight's managed resources
  8. #if SILVERLIGHT
  9. using System.Core;
  10. #endif //SILVERLIGHT
  11. namespace System.Linq {
  12. public interface IQueryable : IEnumerable {
  13. Expression Expression { get; }
  14. Type ElementType { get; }
  15. // the provider that created this query
  16. IQueryProvider Provider { get; }
  17. }
  18. public interface IQueryable<out T> : IEnumerable<T>, IQueryable {
  19. }
  20. public interface IQueryProvider{
  21. IQueryable CreateQuery(Expression expression);
  22. IQueryable<TElement> CreateQuery<TElement>(Expression expression);
  23. object Execute(Expression expression);
  24. TResult Execute<TResult>(Expression expression);
  25. }
  26. public interface IOrderedQueryable : IQueryable {
  27. }
  28. public interface IOrderedQueryable<out T> : IQueryable<T>, IOrderedQueryable {
  29. }
  30. public static class Queryable {
  31. #region Helper methods to obtain MethodInfo in a safe way
  32. private static MethodInfo GetMethodInfo<T1, T2>(Func<T1, T2> f, T1 unused1) {
  33. return f.Method;
  34. }
  35. private static MethodInfo GetMethodInfo<T1, T2, T3>(Func<T1, T2, T3> f, T1 unused1, T2 unused2) {
  36. return f.Method;
  37. }
  38. private static MethodInfo GetMethodInfo<T1, T2, T3, T4>(Func<T1, T2, T3, T4> f, T1 unused1, T2 unused2, T3 unused3) {
  39. return f.Method;
  40. }
  41. private static MethodInfo GetMethodInfo<T1, T2, T3, T4, T5>(Func<T1, T2, T3, T4, T5> f, T1 unused1, T2 unused2, T3 unused3, T4 unused4) {
  42. return f.Method;
  43. }
  44. private static MethodInfo GetMethodInfo<T1, T2, T3, T4, T5, T6>(Func<T1, T2, T3, T4, T5, T6> f, T1 unused1, T2 unused2, T3 unused3, T4 unused4, T5 unused5) {
  45. return f.Method;
  46. }
  47. private static MethodInfo GetMethodInfo<T1, T2, T3, T4, T5, T6, T7>(Func<T1, T2, T3, T4, T5, T6, T7> f, T1 unused1, T2 unused2, T3 unused3, T4 unused4, T5 unused5, T6 unused6) {
  48. return f.Method;
  49. }
  50. #endregion
  51. public static IQueryable<TElement> AsQueryable<TElement>(this IEnumerable<TElement> source) {
  52. if (source == null)
  53. throw Error.ArgumentNull("source");
  54. if (source is IQueryable<TElement>)
  55. return (IQueryable<TElement>)source;
  56. return new EnumerableQuery<TElement>(source);
  57. }
  58. public static IQueryable AsQueryable(this IEnumerable source) {
  59. if (source == null)
  60. throw Error.ArgumentNull("source");
  61. if (source is IQueryable)
  62. return (IQueryable)source;
  63. Type enumType = TypeHelper.FindGenericType(typeof(IEnumerable<>), source.GetType());
  64. if (enumType == null)
  65. throw Error.ArgumentNotIEnumerableGeneric("source");
  66. return EnumerableQuery.Create(enumType.GetGenericArguments()[0], source);
  67. }
  68. public static IQueryable<TSource> Where<TSource>(this IQueryable<TSource> source, Expression<Func<TSource, bool>> predicate) {
  69. if (source == null)
  70. throw Error.ArgumentNull("source");
  71. if (predicate == null)
  72. throw Error.ArgumentNull("predicate");
  73. return source.Provider.CreateQuery<TSource>(
  74. Expression.Call(
  75. null,
  76. GetMethodInfo(Queryable.Where, source, predicate),
  77. new Expression[] { source.Expression, Expression.Quote(predicate) }
  78. ));
  79. }
  80. public static IQueryable<TSource> Where<TSource>(this IQueryable<TSource> source, Expression<Func<TSource, int, bool>> predicate) {
  81. if (source == null)
  82. throw Error.ArgumentNull("source");
  83. if (predicate == null)
  84. throw Error.ArgumentNull("predicate");
  85. return source.Provider.CreateQuery<TSource>(
  86. Expression.Call(
  87. null,
  88. GetMethodInfo(Queryable.Where, source, predicate),
  89. new Expression[] { source.Expression, Expression.Quote(predicate) }
  90. ));
  91. }
  92. public static IQueryable<TResult> OfType<TResult>(this IQueryable source) {
  93. if (source == null)
  94. throw Error.ArgumentNull("source");
  95. return source.Provider.CreateQuery<TResult>(
  96. Expression.Call(
  97. null,
  98. GetMethodInfo(Queryable.OfType<TResult>, source),
  99. new Expression[] { source.Expression }
  100. ));
  101. }
  102. public static IQueryable<TResult> Cast<TResult>(this IQueryable source) {
  103. if (source == null)
  104. throw Error.ArgumentNull("source");
  105. return source.Provider.CreateQuery<TResult>(
  106. Expression.Call(
  107. null,
  108. GetMethodInfo(Queryable.Cast<TResult>, source),
  109. new Expression[] { source.Expression }
  110. ));
  111. }
  112. public static IQueryable<TResult> Select<TSource,TResult>(this IQueryable<TSource> source, Expression<Func<TSource, TResult>> selector) {
  113. if (source == null)
  114. throw Error.ArgumentNull("source");
  115. if (selector == null)
  116. throw Error.ArgumentNull("selector");
  117. return source.Provider.CreateQuery<TResult>(
  118. Expression.Call(
  119. null,
  120. GetMethodInfo(Queryable.Select, source, selector),
  121. new Expression[] { source.Expression, Expression.Quote(selector) }
  122. ));
  123. }
  124. public static IQueryable<TResult> Select<TSource,TResult>(this IQueryable<TSource> source, Expression<Func<TSource, int, TResult>> selector) {
  125. if (source == null)
  126. throw Error.ArgumentNull("source");
  127. if (selector == null)
  128. throw Error.ArgumentNull("selector");
  129. return source.Provider.CreateQuery<TResult>(
  130. Expression.Call(
  131. null,
  132. GetMethodInfo(Queryable.Select, source, selector),
  133. new Expression[] { source.Expression, Expression.Quote(selector) }
  134. ));
  135. }
  136. public static IQueryable<TResult> SelectMany<TSource,TResult>(this IQueryable<TSource> source, Expression<Func<TSource, IEnumerable<TResult>>> selector) {
  137. if (source == null)
  138. throw Error.ArgumentNull("source");
  139. if (selector == null)
  140. throw Error.ArgumentNull("selector");
  141. return source.Provider.CreateQuery<TResult>(
  142. Expression.Call(
  143. null,
  144. GetMethodInfo(Queryable.SelectMany, source, selector),
  145. new Expression[] { source.Expression, Expression.Quote(selector) }
  146. ));
  147. }
  148. public static IQueryable<TResult> SelectMany<TSource,TResult>(this IQueryable<TSource> source, Expression<Func<TSource, int, IEnumerable<TResult>>> selector) {
  149. if (source == null)
  150. throw Error.ArgumentNull("source");
  151. if (selector == null)
  152. throw Error.ArgumentNull("selector");
  153. return source.Provider.CreateQuery<TResult>(
  154. Expression.Call(
  155. null,
  156. GetMethodInfo(Queryable.SelectMany, source, selector),
  157. new Expression[] { source.Expression, Expression.Quote(selector) }
  158. ));
  159. }
  160. 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){
  161. if (source == null)
  162. throw Error.ArgumentNull("source");
  163. if (collectionSelector == null)
  164. throw Error.ArgumentNull("collectionSelector");
  165. if (resultSelector == null)
  166. throw Error.ArgumentNull("resultSelector");
  167. return source.Provider.CreateQuery<TResult>(
  168. Expression.Call(
  169. null,
  170. GetMethodInfo(Queryable.SelectMany, source, collectionSelector, resultSelector),
  171. new Expression[] { source.Expression, Expression.Quote(collectionSelector), Expression.Quote(resultSelector) }
  172. ));
  173. }
  174. public static IQueryable<TResult> SelectMany<TSource,TCollection,TResult>(this IQueryable<TSource> source, Expression<Func<TSource, IEnumerable<TCollection>>> collectionSelector, Expression<Func<TSource, TCollection, TResult>> resultSelector) {
  175. if (source == null)
  176. throw Error.ArgumentNull("source");
  177. if (collectionSelector == null)
  178. throw Error.ArgumentNull("collectionSelector");
  179. if (resultSelector == null)
  180. throw Error.ArgumentNull("resultSelector");
  181. return source.Provider.CreateQuery<TResult>(
  182. Expression.Call(
  183. null,
  184. GetMethodInfo(Queryable.SelectMany, source, collectionSelector, resultSelector),
  185. new Expression[] { source.Expression, Expression.Quote(collectionSelector), Expression.Quote(resultSelector) }
  186. ));
  187. }
  188. private static Expression GetSourceExpression<TSource>(IEnumerable<TSource> source) {
  189. IQueryable<TSource> q = source as IQueryable<TSource>;
  190. if (q != null) return q.Expression;
  191. return Expression.Constant(source, typeof(IEnumerable<TSource>));
  192. }
  193. 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) {
  194. if (outer == null)
  195. throw Error.ArgumentNull("outer");
  196. if (inner == null)
  197. throw Error.ArgumentNull("inner");
  198. if (outerKeySelector == null)
  199. throw Error.ArgumentNull("outerKeySelector");
  200. if (innerKeySelector == null)
  201. throw Error.ArgumentNull("innerKeySelector");
  202. if (resultSelector == null)
  203. throw Error.ArgumentNull("resultSelector");
  204. return outer.Provider.CreateQuery<TResult>(
  205. Expression.Call(
  206. null,
  207. GetMethodInfo(Queryable.Join, outer, inner, outerKeySelector, innerKeySelector, resultSelector),
  208. new Expression[] {
  209. outer.Expression,
  210. GetSourceExpression(inner),
  211. Expression.Quote(outerKeySelector),
  212. Expression.Quote(innerKeySelector),
  213. Expression.Quote(resultSelector)
  214. }
  215. ));
  216. }
  217. 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) {
  218. if (outer == null)
  219. throw Error.ArgumentNull("outer");
  220. if (inner == null)
  221. throw Error.ArgumentNull("inner");
  222. if (outerKeySelector == null)
  223. throw Error.ArgumentNull("outerKeySelector");
  224. if (innerKeySelector == null)
  225. throw Error.ArgumentNull("innerKeySelector");
  226. if (resultSelector == null)
  227. throw Error.ArgumentNull("resultSelector");
  228. return outer.Provider.CreateQuery<TResult>(
  229. Expression.Call(
  230. null,
  231. GetMethodInfo(Queryable.Join, outer, inner, outerKeySelector, innerKeySelector, resultSelector, comparer),
  232. new Expression[] {
  233. outer.Expression,
  234. GetSourceExpression(inner),
  235. Expression.Quote(outerKeySelector),
  236. Expression.Quote(innerKeySelector),
  237. Expression.Quote(resultSelector),
  238. Expression.Constant(comparer, typeof(IEqualityComparer<TKey>))
  239. }
  240. ));
  241. }
  242. 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) {
  243. if (outer == null)
  244. throw Error.ArgumentNull("outer");
  245. if (inner == null)
  246. throw Error.ArgumentNull("inner");
  247. if (outerKeySelector == null)
  248. throw Error.ArgumentNull("outerKeySelector");
  249. if (innerKeySelector == null)
  250. throw Error.ArgumentNull("innerKeySelector");
  251. if (resultSelector == null)
  252. throw Error.ArgumentNull("resultSelector");
  253. return outer.Provider.CreateQuery<TResult>(
  254. Expression.Call(
  255. null,
  256. GetMethodInfo(Queryable.GroupJoin, outer, inner, outerKeySelector, innerKeySelector, resultSelector),
  257. new Expression[] {
  258. outer.Expression,
  259. GetSourceExpression(inner),
  260. Expression.Quote(outerKeySelector),
  261. Expression.Quote(innerKeySelector),
  262. Expression.Quote(resultSelector) }
  263. ));
  264. }
  265. 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) {
  266. if (outer == null)
  267. throw Error.ArgumentNull("outer");
  268. if (inner == null)
  269. throw Error.ArgumentNull("inner");
  270. if (outerKeySelector == null)
  271. throw Error.ArgumentNull("outerKeySelector");
  272. if (innerKeySelector == null)
  273. throw Error.ArgumentNull("innerKeySelector");
  274. if (resultSelector == null)
  275. throw Error.ArgumentNull("resultSelector");
  276. return outer.Provider.CreateQuery<TResult>(
  277. Expression.Call(
  278. null,
  279. GetMethodInfo(Queryable.GroupJoin, outer, inner, outerKeySelector, innerKeySelector, resultSelector, comparer),
  280. new Expression[] {
  281. outer.Expression,
  282. GetSourceExpression(inner),
  283. Expression.Quote(outerKeySelector),
  284. Expression.Quote(innerKeySelector),
  285. Expression.Quote(resultSelector),
  286. Expression.Constant(comparer, typeof(IEqualityComparer<TKey>)) }
  287. ));
  288. }
  289. public static IOrderedQueryable<TSource> OrderBy<TSource, TKey>(this IQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector)
  290. {
  291. if (source == null)
  292. throw Error.ArgumentNull("source");
  293. if (keySelector == null)
  294. throw Error.ArgumentNull("keySelector");
  295. return (IOrderedQueryable<TSource>) source.Provider.CreateQuery<TSource>(
  296. Expression.Call(
  297. null,
  298. GetMethodInfo(Queryable.OrderBy, source, keySelector),
  299. new Expression[] { source.Expression, Expression.Quote(keySelector) }
  300. ));
  301. }
  302. public static IOrderedQueryable<TSource> OrderBy<TSource, TKey>(this IQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector, IComparer<TKey> comparer) {
  303. if (source == null)
  304. throw Error.ArgumentNull("source");
  305. if (keySelector == null)
  306. throw Error.ArgumentNull("keySelector");
  307. return (IOrderedQueryable<TSource>) source.Provider.CreateQuery<TSource>(
  308. Expression.Call(
  309. null,
  310. GetMethodInfo(Queryable.OrderBy, source, keySelector, comparer),
  311. new Expression[] { source.Expression, Expression.Quote(keySelector), Expression.Constant(comparer, typeof(IComparer<TKey>)) }
  312. ));
  313. }
  314. public static IOrderedQueryable<TSource> OrderByDescending<TSource, TKey>(this IQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector) {
  315. if (source == null)
  316. throw Error.ArgumentNull("source");
  317. if (keySelector == null)
  318. throw Error.ArgumentNull("keySelector");
  319. return (IOrderedQueryable<TSource>)source.Provider.CreateQuery<TSource>(
  320. Expression.Call(
  321. null,
  322. GetMethodInfo(Queryable.OrderByDescending, source, keySelector),
  323. new Expression[] { source.Expression, Expression.Quote(keySelector) }
  324. ));
  325. }
  326. public static IOrderedQueryable<TSource> OrderByDescending<TSource, TKey>(this IQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector, IComparer<TKey> comparer) {
  327. if (source == null)
  328. throw Error.ArgumentNull("source");
  329. if (keySelector == null)
  330. throw Error.ArgumentNull("keySelector");
  331. return (IOrderedQueryable<TSource>) source.Provider.CreateQuery<TSource>(
  332. Expression.Call(
  333. null,
  334. GetMethodInfo(Queryable.OrderByDescending, source, keySelector, comparer),
  335. new Expression[] { source.Expression, Expression.Quote(keySelector), Expression.Constant(comparer, typeof(IComparer<TKey>)) }
  336. ));
  337. }
  338. public static IOrderedQueryable<TSource> ThenBy<TSource, TKey>(this IOrderedQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector) {
  339. if (source == null)
  340. throw Error.ArgumentNull("source");
  341. if (keySelector == null)
  342. throw Error.ArgumentNull("keySelector");
  343. return (IOrderedQueryable<TSource>) source.Provider.CreateQuery<TSource>(
  344. Expression.Call(
  345. null,
  346. GetMethodInfo(Queryable.ThenBy, source, keySelector),
  347. new Expression[] { source.Expression, Expression.Quote(keySelector) }
  348. ));
  349. }
  350. public static IOrderedQueryable<TSource> ThenBy<TSource, TKey>(this IOrderedQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector, IComparer<TKey> comparer) {
  351. if (source == null)
  352. throw Error.ArgumentNull("source");
  353. if (keySelector == null)
  354. throw Error.ArgumentNull("keySelector");
  355. return (IOrderedQueryable<TSource>) source.Provider.CreateQuery<TSource>(
  356. Expression.Call(
  357. null,
  358. GetMethodInfo(Queryable.ThenBy, source, keySelector, comparer),
  359. new Expression[] { source.Expression, Expression.Quote(keySelector), Expression.Constant(comparer, typeof(IComparer<TKey>)) }
  360. ));
  361. }
  362. public static IOrderedQueryable<TSource> ThenByDescending<TSource, TKey>(this IOrderedQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector) {
  363. if (source == null)
  364. throw Error.ArgumentNull("source");
  365. if (keySelector == null)
  366. throw Error.ArgumentNull("keySelector");
  367. return (IOrderedQueryable<TSource>) source.Provider.CreateQuery<TSource>(
  368. Expression.Call(
  369. null,
  370. GetMethodInfo(Queryable.ThenByDescending, source, keySelector),
  371. new Expression[] { source.Expression, Expression.Quote(keySelector) }
  372. ));
  373. }
  374. public static IOrderedQueryable<TSource> ThenByDescending<TSource, TKey>(this IOrderedQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector, IComparer<TKey> comparer) {
  375. if (source == null)
  376. throw Error.ArgumentNull("source");
  377. if (keySelector == null)
  378. throw Error.ArgumentNull("keySelector");
  379. return (IOrderedQueryable<TSource>) source.Provider.CreateQuery<TSource>(
  380. Expression.Call(
  381. null,
  382. GetMethodInfo(Queryable.ThenByDescending, source, keySelector, comparer),
  383. new Expression[] { source.Expression, Expression.Quote(keySelector), Expression.Constant(comparer, typeof(IComparer<TKey>)) }
  384. ));
  385. }
  386. public static IQueryable<TSource> Take<TSource>(this IQueryable<TSource> source, int count) {
  387. if (source == null)
  388. throw Error.ArgumentNull("source");
  389. return source.Provider.CreateQuery<TSource>(
  390. Expression.Call(
  391. null,
  392. GetMethodInfo(Queryable.Take, source, count),
  393. new Expression[] { source.Expression, Expression.Constant(count) }
  394. ));
  395. }
  396. public static IQueryable<TSource> TakeWhile<TSource>(this IQueryable<TSource> source, Expression<Func<TSource, bool>> predicate) {
  397. if (source == null)
  398. throw Error.ArgumentNull("source");
  399. if (predicate == null)
  400. throw Error.ArgumentNull("predicate");
  401. return source.Provider.CreateQuery<TSource>(
  402. Expression.Call(
  403. null,
  404. GetMethodInfo(Queryable.TakeWhile, source, predicate),
  405. new Expression[] { source.Expression, Expression.Quote(predicate) }
  406. ));
  407. }
  408. public static IQueryable<TSource> TakeWhile<TSource>(this IQueryable<TSource> source, Expression<Func<TSource, int, bool>> predicate) {
  409. if (source == null)
  410. throw Error.ArgumentNull("source");
  411. if (predicate == null)
  412. throw Error.ArgumentNull("predicate");
  413. return source.Provider.CreateQuery<TSource>(
  414. Expression.Call(
  415. null,
  416. GetMethodInfo(Queryable.TakeWhile, source, predicate),
  417. new Expression[] { source.Expression, Expression.Quote(predicate) }
  418. ));
  419. }
  420. public static IQueryable<TSource> Skip<TSource>(this IQueryable<TSource> source, int count) {
  421. if (source == null)
  422. throw Error.ArgumentNull("source");
  423. return source.Provider.CreateQuery<TSource>(
  424. Expression.Call(
  425. null,
  426. GetMethodInfo(Queryable.Skip, source, count),
  427. new Expression[] { source.Expression, Expression.Constant(count) }
  428. ));
  429. }
  430. public static IQueryable<TSource> SkipWhile<TSource>(this IQueryable<TSource> source, Expression<Func<TSource, bool>> predicate) {
  431. if (source == null)
  432. throw Error.ArgumentNull("source");
  433. if (predicate == null)
  434. throw Error.ArgumentNull("predicate");
  435. return source.Provider.CreateQuery<TSource>(
  436. Expression.Call(
  437. null,
  438. GetMethodInfo(Queryable.SkipWhile, source, predicate),
  439. new Expression[] { source.Expression, Expression.Quote(predicate) }
  440. ));
  441. }
  442. public static IQueryable<TSource> SkipWhile<TSource>(this IQueryable<TSource> source, Expression<Func<TSource, int, bool>> predicate) {
  443. if (source == null)
  444. throw Error.ArgumentNull("source");
  445. if (predicate == null)
  446. throw Error.ArgumentNull("predicate");
  447. return source.Provider.CreateQuery<TSource>(
  448. Expression.Call(
  449. null,
  450. GetMethodInfo(Queryable.SkipWhile, source, predicate),
  451. new Expression[] { source.Expression, Expression.Quote(predicate) }
  452. ));
  453. }
  454. public static IQueryable<IGrouping<TKey,TSource>> GroupBy<TSource, TKey>(this IQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector) {
  455. if (source == null)
  456. throw Error.ArgumentNull("source");
  457. if (keySelector == null)
  458. throw Error.ArgumentNull("keySelector");
  459. return source.Provider.CreateQuery<IGrouping<TKey,TSource>>(
  460. Expression.Call(
  461. null,
  462. GetMethodInfo(Queryable.GroupBy, source, keySelector),
  463. new Expression[] { source.Expression, Expression.Quote(keySelector) }
  464. ));
  465. }
  466. public static IQueryable<IGrouping<TKey, TElement>> GroupBy<TSource, TKey, TElement>(this IQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector, Expression<Func<TSource, TElement>> elementSelector) {
  467. if (source == null)
  468. throw Error.ArgumentNull("source");
  469. if (keySelector == null)
  470. throw Error.ArgumentNull("keySelector");
  471. if (elementSelector == null)
  472. throw Error.ArgumentNull("elementSelector");
  473. return source.Provider.CreateQuery<IGrouping<TKey,TElement>>(
  474. Expression.Call(
  475. null,
  476. GetMethodInfo(Queryable.GroupBy, source, keySelector, elementSelector),
  477. new Expression[] { source.Expression, Expression.Quote(keySelector), Expression.Quote(elementSelector) }
  478. ));
  479. }
  480. public static IQueryable<IGrouping<TKey, TSource>> GroupBy<TSource, TKey>(this IQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector, IEqualityComparer<TKey> comparer) {
  481. if (source == null)
  482. throw Error.ArgumentNull("source");
  483. if (keySelector == null)
  484. throw Error.ArgumentNull("keySelector");
  485. return source.Provider.CreateQuery<IGrouping<TKey,TSource>>(
  486. Expression.Call(
  487. null,
  488. GetMethodInfo(Queryable.GroupBy, source, keySelector, comparer),
  489. new Expression[] { source.Expression, Expression.Quote(keySelector), Expression.Constant(comparer, typeof(IEqualityComparer<TKey>)) }
  490. ));
  491. }
  492. 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) {
  493. if (source == null)
  494. throw Error.ArgumentNull("source");
  495. if (keySelector == null)
  496. throw Error.ArgumentNull("keySelector");
  497. if (elementSelector == null)
  498. throw Error.ArgumentNull("elementSelector");
  499. return source.Provider.CreateQuery<IGrouping<TKey,TElement>>(
  500. Expression.Call(
  501. null,
  502. GetMethodInfo(Queryable.GroupBy, source, keySelector, elementSelector, comparer),
  503. new Expression[] { source.Expression, Expression.Quote(keySelector), Expression.Quote(elementSelector), Expression.Constant(comparer, typeof(IEqualityComparer<TKey>)) }
  504. ));
  505. }
  506. 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)
  507. {
  508. if (source == null)
  509. throw Error.ArgumentNull("source");
  510. if (keySelector == null)
  511. throw Error.ArgumentNull("keySelector");
  512. if (elementSelector == null)
  513. throw Error.ArgumentNull("elementSelector");
  514. if (resultSelector == null)
  515. throw Error.ArgumentNull("resultSelector");
  516. return source.Provider.CreateQuery<TResult>(
  517. Expression.Call(
  518. null,
  519. GetMethodInfo(Queryable.GroupBy, source, keySelector, elementSelector, resultSelector),
  520. new Expression[] { source.Expression, Expression.Quote(keySelector), Expression.Quote(elementSelector), Expression.Quote(resultSelector) }
  521. ));
  522. }
  523. public static IQueryable<TResult> GroupBy<TSource, TKey, TResult>(this IQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector,Expression<Func<TKey, IEnumerable<TSource>, TResult>> resultSelector)
  524. {
  525. if (source == null)
  526. throw Error.ArgumentNull("source");
  527. if (keySelector == null)
  528. throw Error.ArgumentNull("keySelector");
  529. if (resultSelector == null)
  530. throw Error.ArgumentNull("resultSelector");
  531. return source.Provider.CreateQuery<TResult>(
  532. Expression.Call(
  533. null,
  534. GetMethodInfo(Queryable.GroupBy, source, keySelector, resultSelector),
  535. new Expression[] { source.Expression, Expression.Quote(keySelector), Expression.Quote(resultSelector) }
  536. ));
  537. }
  538. 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)
  539. {
  540. if (source == null)
  541. throw Error.ArgumentNull("source");
  542. if (keySelector == null)
  543. throw Error.ArgumentNull("keySelector");
  544. if (resultSelector == null)
  545. throw Error.ArgumentNull("resultSelector");
  546. return source.Provider.CreateQuery<TResult>(
  547. Expression.Call(
  548. null,
  549. GetMethodInfo(Queryable.GroupBy, source, keySelector, resultSelector, comparer),
  550. new Expression[] { source.Expression, Expression.Quote(keySelector), Expression.Quote(resultSelector), Expression.Constant(comparer, typeof(IEqualityComparer<TKey>)) }
  551. ));
  552. }
  553. 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)
  554. {
  555. if (source == null)
  556. throw Error.ArgumentNull("source");
  557. if (keySelector == null)
  558. throw Error.ArgumentNull("keySelector");
  559. if (elementSelector == null)
  560. throw Error.ArgumentNull("elementSelector");
  561. if (resultSelector == null)
  562. throw Error.ArgumentNull("resultSelector");
  563. return source.Provider.CreateQuery<TResult>(
  564. Expression.Call(
  565. null,
  566. GetMethodInfo(Queryable.GroupBy, source, keySelector, elementSelector, resultSelector, comparer),
  567. new Expression[] { source.Expression, Expression.Quote(keySelector), Expression.Quote(elementSelector), Expression.Quote(resultSelector), Expression.Constant(comparer, typeof(IEqualityComparer<TKey>)) }
  568. ));
  569. }
  570. public static IQueryable<TSource> Distinct<TSource>(this IQueryable<TSource> source) {
  571. if (source == null)
  572. throw Error.ArgumentNull("source");
  573. return source.Provider.CreateQuery<TSource>(
  574. Expression.Call(
  575. null,
  576. GetMethodInfo(Queryable.Distinct, source),
  577. new Expression[] { source.Expression }
  578. ));
  579. }
  580. public static IQueryable<TSource> Distinct<TSource>(this IQueryable<TSource> source, IEqualityComparer<TSource> comparer) {
  581. if (source == null)
  582. throw Error.ArgumentNull("source");
  583. return source.Provider.CreateQuery<TSource>(
  584. Expression.Call(
  585. null,
  586. GetMethodInfo(Queryable.Distinct, source, comparer),
  587. new Expression[] { source.Expression, Expression.Constant(comparer, typeof(IEqualityComparer<TSource>)) }
  588. ));
  589. }
  590. public static IQueryable<TSource> Concat<TSource>(this IQueryable<TSource> source1, IEnumerable<TSource> source2) {
  591. if (source1 == null)
  592. throw Error.ArgumentNull("source1");
  593. if (source2 == null)
  594. throw Error.ArgumentNull("source2");
  595. return source1.Provider.CreateQuery<TSource>(
  596. Expression.Call(
  597. null,
  598. GetMethodInfo(Queryable.Concat, source1, source2),
  599. new Expression[] { source1.Expression, GetSourceExpression(source2) }
  600. ));
  601. }
  602. public static IQueryable<TResult> Zip<TFirst, TSecond, TResult>(this IQueryable<TFirst> source1, IEnumerable<TSecond> source2, Expression<Func<TFirst, TSecond, TResult>> resultSelector) {
  603. if (source1 == null)
  604. throw Error.ArgumentNull("source1");
  605. if (source2 == null)
  606. throw Error.ArgumentNull("source2");
  607. if (resultSelector == null)
  608. throw Error.ArgumentNull("resultSelector");
  609. return source1.Provider.CreateQuery<TResult>(
  610. Expression.Call(
  611. null,
  612. GetMethodInfo(Queryable.Zip, source1, source2, resultSelector),
  613. new Expression[] { source1.Expression, GetSourceExpression(source2), Expression.Quote(resultSelector) }
  614. ));
  615. }
  616. public static IQueryable<TSource> Union<TSource>(this IQueryable<TSource> source1, IEnumerable<TSource> source2) {
  617. if (source1 == null)
  618. throw Error.ArgumentNull("source1");
  619. if (source2 == null)
  620. throw Error.ArgumentNull("source2");
  621. return source1.Provider.CreateQuery<TSource>(
  622. Expression.Call(
  623. null,
  624. GetMethodInfo(Queryable.Union, source1, source2),
  625. new Expression[] { source1.Expression, GetSourceExpression(source2) }
  626. ));
  627. }
  628. public static IQueryable<TSource> Union<TSource>(this IQueryable<TSource> source1, IEnumerable<TSource> source2, IEqualityComparer<TSource> comparer) {
  629. if (source1 == null)
  630. throw Error.ArgumentNull("source1");
  631. if (source2 == null)
  632. throw Error.ArgumentNull("source2");
  633. return source1.Provider.CreateQuery<TSource>(
  634. Expression.Call(
  635. null,
  636. GetMethodInfo(Queryable.Union, source1, source2, comparer),
  637. new Expression[] {
  638. source1.Expression,
  639. GetSourceExpression(source2),
  640. Expression.Constant(comparer, typeof(IEqualityComparer<TSource>))
  641. }
  642. ));
  643. }
  644. public static IQueryable<TSource> Intersect<TSource>(this IQueryable<TSource> source1, IEnumerable<TSource> source2) {
  645. if (source1 == null)
  646. throw Error.ArgumentNull("source1");
  647. if (source2 == null)
  648. throw Error.ArgumentNull("source2");
  649. return source1.Provider.CreateQuery<TSource>(
  650. Expression.Call(
  651. null,
  652. GetMethodInfo(Queryable.Intersect, source1, source2),
  653. new Expression[] { source1.Expression, GetSourceExpression(source2) }
  654. ));
  655. }
  656. public static IQueryable<TSource> Intersect<TSource>(this IQueryable<TSource> source1, IEnumerable<TSource> source2, IEqualityComparer<TSource> comparer) {
  657. if (source1 == null)
  658. throw Error.ArgumentNull("source1");
  659. if (source2 == null)
  660. throw Error.ArgumentNull("source2");
  661. return source1.Provider.CreateQuery<TSource>(
  662. Expression.Call(
  663. null,
  664. GetMethodInfo(Queryable.Intersect, source1, source2, comparer),
  665. new Expression[] {
  666. source1.Expression,
  667. GetSourceExpression(source2),
  668. Expression.Constant(comparer, typeof(IEqualityComparer<TSource>))
  669. }
  670. ));
  671. }
  672. public static IQueryable<TSource> Except<TSource>(this IQueryable<TSource> source1, IEnumerable<TSource> source2) {
  673. if (source1 == null)
  674. throw Error.ArgumentNull("source1");
  675. if (source2 == null)
  676. throw Error.ArgumentNull("source2");
  677. return source1.Provider.CreateQuery<TSource>(
  678. Expression.Call(
  679. null,
  680. GetMethodInfo(Queryable.Except, source1, source2),
  681. new Expression[] { source1.Expression, GetSourceExpression(source2) }
  682. ));
  683. }
  684. public static IQueryable<TSource> Except<TSource>(this IQueryable<TSource> source1, IEnumerable<TSource> source2, IEqualityComparer<TSource> comparer) {
  685. if (source1 == null)
  686. throw Error.ArgumentNull("source1");
  687. if (source2 == null)
  688. throw Error.ArgumentNull("source2");
  689. return source1.Provider.CreateQuery<TSource>(
  690. Expression.Call(
  691. null,
  692. GetMethodInfo(Queryable.Except, source1, source2, comparer),
  693. new Expression[] {
  694. source1.Expression,
  695. GetSourceExpression(source2),
  696. Expression.Constant(comparer, typeof(IEqualityComparer<TSource>))
  697. }
  698. ));
  699. }
  700. public static TSource First<TSource>(this IQueryable<TSource> source) {
  701. if (source == null)
  702. throw Error.ArgumentNull("source");
  703. return source.Provider.Execute<TSource>(
  704. Expression.Call(
  705. null,
  706. GetMethodInfo(Queryable.First, source),
  707. new Expression[] { source.Expression }
  708. ));
  709. }
  710. public static TSource First<TSource>(this IQueryable<TSource> source, Expression<Func<TSource, bool>> predicate) {
  711. if (source == null)
  712. throw Error.ArgumentNull("source");
  713. if (predicate == null)
  714. throw Error.ArgumentNull("predicate");
  715. return source.Provider.Execute<TSource>(
  716. Expression.Call(
  717. null,
  718. GetMethodInfo(Queryable.First, source, predicate),
  719. new Expression[] { source.Expression, Expression.Quote(predicate) }
  720. ));
  721. }
  722. public static TSource FirstOrDefault<TSource>(this IQueryable<TSource> source) {
  723. if (source == null)
  724. throw Error.ArgumentNull("source");
  725. return source.Provider.Execute<TSource>(
  726. Expression.Call(
  727. null,
  728. GetMethodInfo(Queryable.FirstOrDefault, source),
  729. new Expression[] { source.Expression }
  730. ));
  731. }
  732. public static TSource FirstOrDefault<TSource>(this IQueryable<TSource> source, Expression<Func<TSource, bool>> predicate) {
  733. if (source == null)
  734. throw Error.ArgumentNull("source");
  735. if (predicate == null)
  736. throw Error.ArgumentNull("predicate");
  737. return source.Provider.Execute<TSource>(
  738. Expression.Call(
  739. null,
  740. GetMethodInfo(Queryable.FirstOrDefault, source, predicate),
  741. new Expression[] { source.Expression, Expression.Quote(predicate) }
  742. ));
  743. }
  744. public static TSource Last<TSource>(this IQueryable<TSource> source) {
  745. if (source == null)
  746. throw Error.ArgumentNull("source");
  747. return source.Provider.Execute<TSource>(
  748. Expression.Call(
  749. null,
  750. GetMethodInfo(Queryable.Last, source),
  751. new Expression[] { source.Expression }
  752. ));
  753. }
  754. public static TSource Last<TSource>(this IQueryable<TSource> source, Expression<Func<TSource, bool>> predicate) {
  755. if (source == null)
  756. throw Error.ArgumentNull("source");
  757. if (predicate == null)
  758. throw Error.ArgumentNull("predicate");
  759. return source.Provider.Execute<TSource>(
  760. Expression.Call(
  761. null,
  762. GetMethodInfo(Queryable.Last, source, predicate),
  763. new Expression[] { source.Expression, Expression.Quote(predicate) }
  764. ));
  765. }
  766. public static TSource LastOrDefault<TSource>(this IQueryable<TSource> source) {
  767. if (source == null)
  768. throw Error.ArgumentNull("source");
  769. return source.Provider.Execute<TSource>(
  770. Expression.Call(
  771. null,
  772. GetMethodInfo(Queryable.LastOrDefault, source),
  773. new Expression[] { source.Expression }
  774. ));
  775. }
  776. public static TSource LastOrDefault<TSource>(this IQueryable<TSource> source, Expression<Func<TSource, bool>> predicate) {
  777. if (source == null)
  778. throw Error.ArgumentNull("source");
  779. if (predicate == null)
  780. throw Error.ArgumentNull("predicate");
  781. return source.Provider.Execute<TSource>(
  782. Expression.Call(
  783. null,
  784. GetMethodInfo(Queryable.LastOrDefault, source, predicate),
  785. new Expression[] { source.Expression, Expression.Quote(predicate) }
  786. ));
  787. }
  788. public static TSource Single<TSource>(this IQueryable<TSource> source) {
  789. if (source == null)
  790. throw Error.ArgumentNull("source");
  791. return source.Provider.Execute<TSource>(
  792. Expression.Call(
  793. null,
  794. GetMethodInfo(Queryable.Single, source),
  795. new Expression[] { source.Expression }
  796. ));
  797. }
  798. public static TSource Single<TSource>(this IQueryable<TSource> source, Expression<Func<TSource,bool>> predicate) {
  799. if (source == null)
  800. throw Error.ArgumentNull("source");
  801. if (predicate == null)
  802. throw Error.ArgumentNull("predicate");
  803. return source.Provider.Execute<TSource>(
  804. Expression.Call(
  805. null,
  806. GetMethodInfo(Queryable.Single, source, predicate),
  807. new Expression[] { source.Expression, Expression.Quote(predicate) }
  808. ));
  809. }
  810. public static TSource SingleOrDefault<TSource>(this IQueryable<TSource> source) {
  811. if (source == null)
  812. throw Error.ArgumentNull("source");
  813. return source.Provider.Execute<TSource>(
  814. Expression.Call(
  815. null,
  816. GetMethodInfo(Queryable.SingleOrDefault, source),
  817. new Expression[] { source.Expression }
  818. ));
  819. }
  820. public static TSource SingleOrDefault<TSource>(this IQueryable<TSource> source, Expression<Func<TSource,bool>> predicate) {
  821. if (source == null)
  822. throw Error.ArgumentNull("source");
  823. if (predicate == null)
  824. throw Error.ArgumentNull("predicate");
  825. return source.Provider.Execute<TSource>(
  826. Expression.Call(
  827. null,
  828. GetMethodInfo(Queryable.SingleOrDefault, source, predicate),
  829. new Expression[] { source.Expression, Expression.Quote(predicate) }
  830. ));
  831. }
  832. public static TSource ElementAt<TSource>(this IQueryable<TSource> source, int index) {
  833. if (source == null)
  834. throw Error.ArgumentNull("source");
  835. if (index < 0)
  836. throw Error.ArgumentOutOfRange("index");
  837. return source.Provider.Execute<TSource>(
  838. Expression.Call(
  839. null,
  840. GetMethodInfo(Queryable.ElementAt, source, index),
  841. new Expression[] { source.Expression, Expression.Constant(index) }
  842. ));
  843. }
  844. public static TSource ElementAtOrDefault<TSource>(this IQueryable<TSource> source, int index) {
  845. if (source == null)
  846. throw Error.ArgumentNull("source");
  847. return source.Provider.Execute<TSource>(
  848. Expression.Call(
  849. null,
  850. GetMethodInfo(Queryable.ElementAtOrDefault, source, index),
  851. new Expression[] { source.Expression, Expression.Constant(index) }
  852. ));
  853. }
  854. public static IQueryable<TSource> DefaultIfEmpty<TSource>(this IQueryable<TSource> source) {
  855. if (source == null)
  856. throw Error.ArgumentNull("source");
  857. return source.Provider.CreateQuery<TSource>(
  858. Expression.Call(
  859. null,
  860. GetMethodInfo(Queryable.DefaultIfEmpty, source),
  861. new Expression[] { source.Expression }
  862. ));
  863. }
  864. public static IQueryable<TSource> DefaultIfEmpty<TSource>(this IQueryable<TSource> source, TSource defaultValue) {
  865. if (source == null)
  866. throw Error.ArgumentNull("source");
  867. return source.Provider.CreateQuery<TSource>(
  868. Expression.Call(
  869. null,
  870. GetMethodInfo(Queryable.DefaultIfEmpty, source, defaultValue),
  871. new Expression[] { source.Expression, Expression.Constant(defaultValue, typeof(TSource)) }
  872. ));
  873. }
  874. public static bool Contains<TSource>(this IQueryable<TSource> source, TSource item) {
  875. if (source == null)
  876. throw Error.ArgumentNull("source");
  877. return source.Provider.Execute<bool>(
  878. Expression.Call(
  879. null,
  880. GetMethodInfo(Queryable.Contains, source, item),
  881. new Expression[] { source.Expression, Expression.Constant(item, typeof(TSource)) }
  882. ));
  883. }
  884. public static bool Contains<TSource>(this IQueryable<TSource> source, TSource item, IEqualityComparer<TSource> comparer) {
  885. if (source == null)
  886. throw Error.ArgumentNull("source");
  887. return source.Provider.Execute<bool>(
  888. Expression.Call(
  889. null,
  890. GetMethodInfo(Queryable.Contains, source, item, comparer),
  891. new Expression[] { source.Expression, Expression.Constant(item, typeof(TSource)), Expression.Constant(comparer, typeof(IEqualityComparer<TSource>)) }
  892. ));
  893. }
  894. public static IQueryable<TSource> Reverse<TSource>(this IQueryable<TSource> source) {
  895. if (source == null)
  896. throw Error.ArgumentNull("source");
  897. return source.Provider.CreateQuery<TSource>(
  898. Expression.Call(
  899. null,
  900. GetMethodInfo(Queryable.Reverse, source),
  901. new Expression[] { source.Expression }
  902. ));
  903. }
  904. public static bool SequenceEqual<TSource>(this IQueryable<TSource> source1, IEnumerable<TSource> source2) {
  905. if (source1 == null)
  906. throw Error.ArgumentNull("source1");
  907. if (source2 == null)
  908. throw Error.ArgumentNull("source2");
  909. return source1.Provider.Execute<bool>(
  910. Expression.Call(
  911. null,
  912. GetMethodInfo(Queryable.SequenceEqual, source1, source2),
  913. new Expression[] { source1.Expression, GetSourceExpression(source2) }
  914. ));
  915. }
  916. public static bool SequenceEqual<TSource>(this IQueryable<TSource> source1, IEnumerable<TSource> source2, IEqualityComparer<TSource> comparer) {
  917. if (source1 == null)
  918. throw Error.ArgumentNull("source1");
  919. if (source2 == null)
  920. throw Error.ArgumentNull("source2");
  921. return source1.Provider.Execute<bool>(
  922. Expression.Call(
  923. null,
  924. GetMethodInfo(Queryable.SequenceEqual, source1, source2, comparer),
  925. new Expression[] {
  926. source1.Expression,
  927. GetSourceExpression(source2),
  928. Expression.Constant(comparer, typeof(IEqualityComparer<TSource>))
  929. }
  930. ));
  931. }
  932. public static bool Any<TSource>(this IQueryable<TSource> source) {
  933. if (source == null)
  934. throw Error.ArgumentNull("source");
  935. return source.Provider.Execute<bool>(
  936. Expression.Call(
  937. null,
  938. GetMethodInfo(Queryable.Any, source),
  939. new Expression[] { source.Expression }
  940. ));
  941. }
  942. public static bool Any<TSource>(this IQueryable<TSource> source, Expression<Func<TSource, bool>> predicate) {
  943. if (source == null)
  944. throw Error.ArgumentNull("source");
  945. if (predicate == null)
  946. throw Error.ArgumentNull("predicate");
  947. return source.Provider.Execute<bool>(
  948. Expression.Call(
  949. null,
  950. GetMethodInfo(Queryable.Any, source, predicate),
  951. new Expression[] { source.Expression, Expression.Quote(predicate) }
  952. ));
  953. }
  954. public static bool All<TSource>(this IQueryable<TSource> source, Expression<Func<TSource, bool>> predicate) {
  955. if (source == null)
  956. throw Error.ArgumentNull("source");
  957. if (predicate == null)
  958. throw Error.ArgumentNull("predicate");
  959. return source.Provider.Execute<bool>(
  960. Expression.Call(
  961. null,
  962. GetMethodInfo(Queryable.All, source, predicate),
  963. new Expression[] { source.Expression, Expression.Quote(predicate) }
  964. ));
  965. }
  966. public static int Count<TSource>(this IQueryable<TSource> source) {
  967. if (source == null)
  968. throw Error.ArgumentNull("source");
  969. return source.Provider.Execute<int>(
  970. Expression.Call(
  971. null,
  972. GetMethodInfo(Queryable.Count, source),
  973. new Expression[] { source.Expression }
  974. ));
  975. }
  976. public static int Count<TSource>(this IQueryable<TSource> source, Expression<Func<TSource, bool>> predicate) {
  977. if (source == null)
  978. throw Error.ArgumentNull("source");
  979. if (predicate == null)
  980. throw Error.ArgumentNull("predicate");
  981. return source.Provider.Execute<int>(
  982. Expression.Call(
  983. null,
  984. GetMethodInfo(Queryable.Count, source, predicate),
  985. new Expression[] { source.Expression, Expression.Quote(predicate) }
  986. ));
  987. }
  988. public static long LongCount<TSource>(this IQueryable<TSource> source) {
  989. if (source == null)
  990. throw Error.ArgumentNull("source");
  991. return source.Provider.Execute<long>(
  992. Expression.Call(
  993. null,
  994. GetMethodInfo(Queryable.LongCount, source),
  995. new Expression[] { source.Expression }
  996. ));
  997. }
  998. public static long LongCount<TSource>(this IQueryable<TSource> source, Expression<Func<TSource, bool>> predicate) {
  999. if (source == null)
  1000. throw Error.ArgumentNull("source");
  1001. if (predicate == null)
  1002. throw Error.ArgumentNull("predicate");
  1003. return source.Provider.Execute<long>(
  1004. Expression.Call(
  1005. null,
  1006. GetMethodInfo(Queryable.LongCount, source, predicate),
  1007. new Expression[] { source.Expression, Expression.Quote(predicate) }
  1008. ));
  1009. }
  1010. public static TSource Min<TSource>(this IQueryable<TSource> source) {
  1011. if (source == null)
  1012. throw Error.ArgumentNull("source");
  1013. return source.Provider.Execute<TSource>(
  1014. Expression.Call(
  1015. null,
  1016. GetMethodInfo(Queryable.Min, source),
  1017. new Expression[] { source.Expression }
  1018. ));
  1019. }
  1020. public static TResult Min<TSource,TResult>(this IQueryable<TSource> source, Expression<Func<TSource,TResult>> selector) {
  1021. if (source == null)
  1022. throw Error.ArgumentNull("source");
  1023. if (selector == null)
  1024. throw Error.ArgumentNull("selector");
  1025. return source.Provider.Execute<TResult>(
  1026. Expression.Call(
  1027. null,
  1028. GetMethodInfo(Queryable.Min, source, selector),
  1029. new Expression[] { source.Expression, Expression.Quote(selector) }
  1030. ));
  1031. }
  1032. public static TSource Max<TSource>(this IQueryable<TSource> source) {
  1033. if (source == null)
  1034. throw Error.ArgumentNull("source");
  1035. return source.Provider.Execute<TSource>(
  1036. Expression.Call(
  1037. null,
  1038. GetMethodInfo(Queryable.Max, source),
  1039. new Expression[] { source.Expression }
  1040. ));
  1041. }
  1042. public static TResult Max<TSource,TResult>(this IQueryable<TSource> source, Expression<Func<TSource,TResult>> selector) {
  1043. if (source == null)
  1044. throw Error.ArgumentNull("source");
  1045. if (selector == null)
  1046. throw Error.ArgumentNull("selector");
  1047. return source.Provider.Execute<TResult>(
  1048. Expression.Call(
  1049. null,
  1050. GetMethodInfo(Queryable.Max, source, selector),
  1051. new Expression[] { source.Expression, Expression.Quote(selector) }
  1052. ));
  1053. }
  1054. public static int Sum(this IQueryable<int> source) {
  1055. if (source == null)
  1056. throw Error.ArgumentNull("source");
  1057. return source.Provider.Execute<int>(
  1058. Expression.Call(
  1059. null,
  1060. GetMethodInfo(Queryable.Sum, source),
  1061. new Expression[] { source.Expression }
  1062. ));
  1063. }
  1064. public static int? Sum(this IQueryable<int?> source) {
  1065. if (source == null)
  1066. throw Error.ArgumentNull("source");
  1067. return source.Provider.Execute<int?>(
  1068. Expression.Call(
  1069. null,
  1070. GetMethodInfo(Queryable.Sum, source),
  1071. new Expression[] { source.Expression }
  1072. ));
  1073. }
  1074. public static long Sum(this IQueryable<long> source) {
  1075. if (source == null)
  1076. throw Error.ArgumentNull("source");
  1077. return source.Provider.Execute<long>(
  1078. Expression.Call(
  1079. null,
  1080. GetMethodInfo(Queryable.Sum, source),
  1081. new Expression[] { source.Expression }
  1082. ));
  1083. }
  1084. public static long? Sum(this IQueryable<long?> source) {
  1085. if (source == null)
  1086. throw Error.ArgumentNull("source");
  1087. return source.Provider.Execute<long?>(
  1088. Expression.Call(
  1089. null,
  1090. GetMethodInfo(Queryable.Sum, source),
  1091. new Expression[] { source.Expression }
  1092. ));
  1093. }
  1094. public static float Sum(this IQueryable<float> source) {
  1095. if (source == null)
  1096. throw Error.ArgumentNull("source");
  1097. return source.Provider.Execute<float>(
  1098. Expression.Call(
  1099. null,
  1100. GetMethodInfo(Queryable.Sum, source),
  1101. new Expression[] { source.Expression }
  1102. ));
  1103. }
  1104. public static float? Sum(this IQueryable<float?> source) {
  1105. if (source == null)
  1106. throw Error.ArgumentNull("source");
  1107. return source.Provider.Execute<float?>(
  1108. Expression.Call(
  1109. null,
  1110. GetMethodInfo(Queryable.Sum, source),
  1111. new Expression[] { source.Expression }
  1112. ));
  1113. }
  1114. public static double Sum(this IQueryable<double> source) {
  1115. if (source == null)
  1116. throw Error.ArgumentNull("source");
  1117. return source.Provider.Execute<double>(
  1118. Expression.Call(
  1119. null,
  1120. GetMethodInfo(Queryable.Sum, source),
  1121. new Expression[] { source.Expression }
  1122. ));
  1123. }
  1124. public static double? Sum(this IQueryable<double?> source) {
  1125. if (source == null)
  1126. throw Error.ArgumentNull("source");
  1127. return source.Provider.Execute<double?>(
  1128. Expression.Call(
  1129. null,
  1130. GetMethodInfo(Queryable.Sum, source),
  1131. new Expression[] { source.Expression }
  1132. ));
  1133. }
  1134. public static decimal Sum(this IQueryable<decimal> source) {
  1135. if (source == null)
  1136. throw Error.ArgumentNull("source");
  1137. return source.Provider.Execute<decimal>(
  1138. Expression.Call(
  1139. null,
  1140. GetMethodInfo(Queryable.Sum, source),
  1141. new Expression[] { source.Expression }
  1142. ));
  1143. }
  1144. public static decimal? Sum(this IQueryable<decimal?> source) {
  1145. if (source == null)
  1146. throw Error.ArgumentNull("source");
  1147. return source.Provider.Execute<decimal?>(
  1148. Expression.Call(
  1149. null,
  1150. GetMethodInfo(Queryable.Sum, source),
  1151. new Expression[] { source.Expression }
  1152. ));
  1153. }
  1154. public static int Sum<TSource>(this IQueryable<TSource> source, Expression<Func<TSource,int>> selector) {
  1155. if (source == null)
  1156. throw Error.ArgumentNull("source");
  1157. if (selector == null)
  1158. throw Error.ArgumentNull("selector");
  1159. return source.Provider.Execute<int>(
  1160. Expression.Call(
  1161. null,
  1162. GetMethodInfo(Queryable.Sum, source, selector),
  1163. new Expression[] { source.Expression, Expression.Quote(selector) }
  1164. ));
  1165. }
  1166. public static int? Sum<TSource>(this IQueryable<TSource> source, Expression<Func<TSource,int?>> selector) {
  1167. if (source == null)
  1168. throw Error.ArgumentNull("source");
  1169. if (selector == null)
  1170. throw Error.ArgumentNull("selector");
  1171. return source.Provider.Execute<int?>(
  1172. Expression.Call(
  1173. null,
  1174. GetMethodInfo(Queryable.Sum, source, selector),
  1175. new Expression[] { source.Expression, Expression.Quote(selector) }
  1176. ));
  1177. }
  1178. public static long Sum<TSource>(this IQueryable<TSource> source, Expression<Func<TSource,long>> selector) {
  1179. if (source == null)
  1180. throw Error.ArgumentNull("source");
  1181. if (selector == null)
  1182. throw Error.ArgumentNull("selector");
  1183. return source.Provider.Execute<long>(
  1184. Expression.Call(
  1185. null,
  1186. GetMethodInfo(Queryable.Sum, source, selector),
  1187. new Expression[] { source.Expression, Expression.Quote(selector) }
  1188. ));
  1189. }
  1190. public static long? Sum<TSource>(this IQueryable<TSource> source, Expression<Func<TSource,long?>> selector) {
  1191. if (source == null)
  1192. throw Error.ArgumentNull("source");
  1193. if (selector == null)
  1194. throw Error.ArgumentNull("selector");
  1195. return source.Provider.Execute<long?>(
  1196. Expression.Call(
  1197. null,
  1198. GetMethodInfo(Queryable.Sum, source, selector),
  1199. new Expression[] { source.Expression, Expression.Quote(selector) }
  1200. ));
  1201. }
  1202. public static float Sum<TSource>(this IQueryable<TSource> source, Expression<Func<TSource,float>> selector) {
  1203. if (source == null)
  1204. throw Error.ArgumentNull("source");
  1205. if (selector == null)
  1206. throw Error.ArgumentNull("selector");
  1207. return source.Provider.Execute<float>(
  1208. Expression.Call(
  1209. null,
  1210. GetMethodInfo(Queryable.Sum, source, selector),
  1211. new Expression[] { source.Expression, Expression.Quote(selector) }
  1212. ));
  1213. }
  1214. public static float? Sum<TSource>(this IQueryable<TSource> source, Expression<Func<TSource,float?>> selector) {
  1215. if (source == null)
  1216. throw Error.ArgumentNull("source");
  1217. if (selector == null)
  1218. throw Error.ArgumentNull("selector");
  1219. return source.Provider.Execute<float?>(
  1220. Expression.Call(
  1221. null,
  1222. GetMethodInfo(Queryable.Sum, source, selector),
  1223. new Expression[] { source.Expression, Expression.Quote(selector) }
  1224. ));
  1225. }
  1226. public static double Sum<TSource>(this IQueryable<TSource> source, Expression<Func<TSource,double>> selector) {
  1227. if (source == null)
  1228. throw Error.ArgumentNull("source");
  1229. if (selector == null)
  1230. throw Error.ArgumentNull("selector");
  1231. return source.Provider.Execute<double>(
  1232. Expression.Call(
  1233. null,
  1234. GetMethodInfo(Queryable.Sum, source, selector),
  1235. new Expression[] { source.Expression, Expression.Quote(selector) }
  1236. ));
  1237. }
  1238. public static double? Sum<TSource>(this IQueryable<TSource> source, Expression<Func<TSource,double?>> selector) {
  1239. if (source == null)
  1240. throw Error.ArgumentNull("source");
  1241. if (selector == null)
  1242. throw Error.ArgumentNull("selector");
  1243. return source.Provider.Execute<double?>(
  1244. Expression.Call(
  1245. null,
  1246. GetMethodInfo(Queryable.Sum, source, selector),
  1247. new Expression[] { source.Expression, Expression.Quote(selector) }
  1248. ));
  1249. }
  1250. public static decimal Sum<TSource>(this IQueryable<TSource> source, Expression<Func<TSource,decimal>> selector) {
  1251. if (source == null)
  1252. throw Error.ArgumentNull("source");
  1253. if (selector == null)
  1254. throw Error.ArgumentNull("selector");
  1255. return source.Provider.Execute<decimal>(
  1256. Expression.Call(
  1257. null,
  1258. GetMethodInfo(Queryable.Sum, source, selector),
  1259. new Expression[] { source.Expression, Expression.Quote(selector) }
  1260. ));
  1261. }
  1262. public static decimal? Sum<TSource>(this IQueryable<TSource> source, Expression<Func<TSource,decimal?>> selector) {
  1263. if (source == null)
  1264. throw Error.ArgumentNull("source");
  1265. if (selector == null)
  1266. throw Error.ArgumentNull("selector");
  1267. return source.Provider.Execute<decimal?>(
  1268. Expression.Call(
  1269. null,
  1270. GetMethodInfo(Queryable.Sum, source, selector),
  1271. new Expression[] { source.Expression, Expression.Quote(selector) }
  1272. ));
  1273. }
  1274. public static double Average(this IQueryable<int> source) {
  1275. if (source == null)
  1276. throw Error.ArgumentNull("source");
  1277. return source.Provider.Execute<double>(
  1278. Expression.Call(
  1279. null,
  1280. GetMethodInfo(Queryable.Average, source),
  1281. new Expression[] { source.Expression }
  1282. ));
  1283. }
  1284. public static double? Average(this IQueryable<int?> source) {
  1285. if (source == null)
  1286. throw Error.ArgumentNull("source");
  1287. return source.Provider.Execute<double?>(
  1288. Expression.Call(
  1289. null,
  1290. GetMethodInfo(Queryable.Average, source),
  1291. new Expression[] { source.Expression }
  1292. ));
  1293. }
  1294. public static double Average(this IQueryable<long> source) {
  1295. if (source == null)
  1296. throw Error.ArgumentNull("source");
  1297. return source.Provider.Execute<double>(
  1298. Expression.Call(
  1299. null,
  1300. GetMethodInfo(Queryable.Average, source),
  1301. new Expression[] { source.Expression }
  1302. ));
  1303. }
  1304. public static double? Average(this IQueryable<long?> source) {
  1305. if (source == null)
  1306. throw Error.ArgumentNull("source");
  1307. return source.Provider.Execute<double?>(
  1308. Expression.Call(
  1309. null,
  1310. GetMethodInfo(Queryable.Average, source),
  1311. new Expression[] { source.Expression }
  1312. ));
  1313. }
  1314. public static float Average(this IQueryable<float> source)
  1315. {
  1316. if (source == null)
  1317. throw Error.ArgumentNull("source");
  1318. return source.Provider.Execute<float>(
  1319. Expression.Call(
  1320. null,
  1321. GetMethodInfo(Queryable.Average, source),
  1322. new Expression[] { source.Expression }
  1323. ));
  1324. }
  1325. public static float? Average(this IQueryable<float?> source)
  1326. {
  1327. if (source == null)
  1328. throw Error.ArgumentNull("source");
  1329. return source.Provider.Execute<float?>(
  1330. Expression.Call(
  1331. null,
  1332. GetMethodInfo(Queryable.Average, source),
  1333. new Expression[] { source.Expression }
  1334. ));
  1335. }
  1336. public static double Average(this IQueryable<double> source) {
  1337. if (source == null)
  1338. throw Error.ArgumentNull("source");
  1339. return source.Provider.Execute<double>(
  1340. Expression.Call(
  1341. null,
  1342. GetMethodInfo(Queryable.Average, source),
  1343. new Expression[] { source.Expression }
  1344. ));
  1345. }
  1346. public static double? Average(this IQueryable<double?> source) {
  1347. if (source == null)
  1348. throw Error.ArgumentNull("source");
  1349. return source.Provider.Execute<double?>(
  1350. Expression.Call(
  1351. null,
  1352. GetMethodInfo(Queryable.Average, source),
  1353. new Expression[] { source.Expression }
  1354. ));
  1355. }
  1356. public static decimal Average(this IQueryable<decimal> source) {
  1357. if (source == null)
  1358. throw Error.ArgumentNull("source");
  1359. return source.Provider.Execute<decimal>(
  1360. Expression.Call(
  1361. null,
  1362. GetMethodInfo(Queryable.Average, source),
  1363. new Expression[] { source.Expression }
  1364. ));
  1365. }
  1366. public static decimal? Average(this IQueryable<decimal?> source) {
  1367. if (source == null)
  1368. throw Error.ArgumentNull("source");
  1369. return source.Provider.Execute<decimal?>(
  1370. Expression.Call(
  1371. null,
  1372. GetMethodInfo(Queryable.Average, source),
  1373. new Expression[] { source.Expression }
  1374. ));
  1375. }
  1376. public static double Average<TSource>(this IQueryable<TSource> source, Expression<Func<TSource,int>> selector) {
  1377. if (source == null)
  1378. throw Error.ArgumentNull("source");
  1379. if (selector == null)
  1380. throw Error.ArgumentNull("selector");
  1381. return source.Provider.Execute<double>(
  1382. Expression.Call(
  1383. null,
  1384. GetMethodInfo(Queryable.Average, source, selector),
  1385. new Expression[] { source.Expression, Expression.Quote(selector) }
  1386. ));
  1387. }
  1388. public static double? Average<TSource>(this IQueryable<TSource> source, Expression<Func<TSource,int?>> selector) {
  1389. if (source == null)
  1390. throw Error.ArgumentNull("source");
  1391. if (selector == null)
  1392. throw Error.ArgumentNull("selector");
  1393. return source.Provider.Execute<double?>(
  1394. Expression.Call(
  1395. null,
  1396. GetMethodInfo(Queryable.Average, source, selector),
  1397. new Expression[] { source.Expression, Expression.Quote(selector) }
  1398. ));
  1399. }
  1400. public static float Average<TSource>(this IQueryable<TSource> source, Expression<Func<TSource, float>> selector)
  1401. {
  1402. if (source == null)
  1403. throw Error.ArgumentNull("source");
  1404. if (selector == null)
  1405. throw Error.ArgumentNull("selector");
  1406. return source.Provider.Execute<float>(
  1407. Expression.Call(
  1408. null,
  1409. GetMethodInfo(Queryable.Average, source, selector),
  1410. new Expression[] { source.Expression, Expression.Quote(selector) }
  1411. ));
  1412. }
  1413. public static float? Average<TSource>(this IQueryable<TSource> source, Expression<Func<TSource, float?>> selector)
  1414. {
  1415. if (source == null)
  1416. throw Error.ArgumentNull("source");
  1417. if (selector == null)
  1418. throw Error.ArgumentNull("selector");
  1419. return source.Provider.Execute<float?>(
  1420. Expression.Call(
  1421. null,
  1422. GetMethodInfo(Queryable.Average, source, selector),
  1423. new Expression[] { source.Expression, Expression.Quote(selector) }
  1424. ));
  1425. }
  1426. public static double Average<TSource>(this IQueryable<TSource> source, Expression<Func<TSource,long>> selector) {
  1427. if (source == null)
  1428. throw Error.ArgumentNull("source");
  1429. if (selector == null)
  1430. throw Error.ArgumentNull("selector");
  1431. return source.Provider.Execute<double>(
  1432. Expression.Call(
  1433. null,
  1434. GetMethodInfo(Queryable.Average, source, selector),
  1435. new Expression[] { source.Expression, Expression.Quote(selector) }
  1436. ));
  1437. }
  1438. public static double? Average<TSource>(this IQueryable<TSource> source, Expression<Func<TSource,long?>> selector) {
  1439. if (source == null)
  1440. throw Error.ArgumentNull("source");
  1441. if (selector == null)
  1442. throw Error.ArgumentNull("selector");
  1443. return source.Provider.Execute<double?>(
  1444. Expression.Call(
  1445. null,
  1446. GetMethodInfo(Queryable.Average, source, selector),
  1447. new Expression[] { source.Expression, Expression.Quote(selector) }
  1448. ));
  1449. }
  1450. public static double Average<TSource>(this IQueryable<TSource> source, Expression<Func<TSource,double>> selector) {
  1451. if (source == null)
  1452. throw Error.ArgumentNull("source");
  1453. if (selector == null)
  1454. throw Error.ArgumentNull("selector");
  1455. return source.Provider.Execute<double>(
  1456. Expression.Call(
  1457. null,
  1458. GetMethodInfo(Queryable.Average, source, selector),
  1459. new Expression[] { source.Expression, Expression.Quote(selector) }
  1460. ));
  1461. }
  1462. public static double? Average<TSource>(this IQueryable<TSource> source, Expression<Func<TSource,double?>> selector) {
  1463. if (source == null)
  1464. throw Error.ArgumentNull("source");
  1465. if (selector == null)
  1466. throw Error.ArgumentNull("selector");
  1467. return source.Provider.Execute<double?>(
  1468. Expression.Call(
  1469. null,
  1470. GetMethodInfo(Queryable.Average, source, selector),
  1471. new Expression[] { source.Expression, Expression.Quote(selector) }
  1472. ));
  1473. }
  1474. public static decimal Average<TSource>(this IQueryable<TSource> source, Expression<Func<TSource,decimal>> selector) {
  1475. if (source == null)
  1476. throw Error.ArgumentNull("source");
  1477. if (selector == null)
  1478. throw Error.ArgumentNull("selector");
  1479. return source.Provider.Execute<decimal>(
  1480. Expression.Call(
  1481. null,
  1482. GetMethodInfo(Queryable.Average, source, selector),
  1483. new Expression[] { source.Expression, Expression.Quote(selector) }
  1484. ));
  1485. }
  1486. public static decimal? Average<TSource>(this IQueryable<TSource> source, Expression<Func<TSource,decimal?>> selector) {
  1487. if (source == null)
  1488. throw Error.ArgumentNull("source");
  1489. if (selector == null)
  1490. throw Error.ArgumentNull("selector");
  1491. return source.Provider.Execute<decimal?>(
  1492. Expression.Call(
  1493. null,
  1494. GetMethodInfo(Queryable.Average, source, selector),
  1495. new Expression[] { source.Expression, Expression.Quote(selector) }
  1496. ));
  1497. }
  1498. public static TSource Aggregate<TSource>(this IQueryable<TSource> source, Expression<Func<TSource,TSource,TSource>> func) {
  1499. if (source == null)
  1500. throw Error.ArgumentNull("source");
  1501. if (func == null)
  1502. throw Error.ArgumentNull("func");
  1503. return source.Provider.Execute<TSource>(
  1504. Expression.Call(
  1505. null,
  1506. GetMethodInfo(Queryable.Aggregate, source, func),
  1507. new Expression[] { source.Expression, Expression.Quote(func) }
  1508. ));
  1509. }
  1510. public static TAccumulate Aggregate<TSource,TAccumulate>(this IQueryable<TSource> source, TAccumulate seed, Expression<Func<TAccumulate,TSource,TAccumulate>> func) {
  1511. if (source == null)
  1512. throw Error.ArgumentNull("source");
  1513. if (func == null)
  1514. throw Error.ArgumentNull("func");
  1515. return source.Provider.Execute<TAccumulate>(
  1516. Expression.Call(
  1517. null,
  1518. GetMethodInfo(Queryable.Aggregate, source, seed, func),
  1519. new Expression[] { source.Expression, Expression.Constant(seed), Expression.Quote(func) }
  1520. ));
  1521. }
  1522. public static TResult Aggregate<TSource,TAccumulate,TResult>(this IQueryable<TSource> source, TAccumulate seed, Expression<Func<TAccumulate,TSource,TAccumulate>> func, Expression<Func<TAccumulate,TResult>> selector) {
  1523. if (source == null)
  1524. throw Error.ArgumentNull("source");
  1525. if (func == null)
  1526. throw Error.ArgumentNull("func");
  1527. if (selector == null)
  1528. throw Error.ArgumentNull("selector");
  1529. return source.Provider.Execute<TResult>(
  1530. Expression.Call(
  1531. null,
  1532. GetMethodInfo(Queryable.Aggregate, source, seed, func, selector),
  1533. new Expression[] { source.Expression, Expression.Constant(seed), Expression.Quote(func), Expression.Quote(selector) }
  1534. ));
  1535. }
  1536. }
  1537. }