Queryable.cs 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597
  1. //
  2. // Queryable.cs
  3. //
  4. // Authors:
  5. // Marek Safar ([email protected])
  6. // Alejandro Serrano "Serras" ([email protected])
  7. // Jb Evain ([email protected])
  8. // Andreas Noever ([email protected])
  9. //
  10. // Copyright (C) 2008 Novell, Inc (http://www.novell.com)
  11. //
  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:
  19. //
  20. // The above copyright notice and this permission notice shall be
  21. // included in all copies or substantial portions of the Software.
  22. //
  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.
  30. //
  31. using System;
  32. using System.Collections;
  33. using System.Collections.Generic;
  34. using System.Linq.Expressions;
  35. using System.Reflection;
  36. using System.Linq;
  37. namespace System.Linq {
  38. public static class Queryable {
  39. static MethodInfo MakeGeneric (MethodBase method, params Type [] parameters)
  40. {
  41. return ((MethodInfo) method).MakeGenericMethod (parameters);
  42. }
  43. static Expression StaticCall (MethodInfo method, params Expression [] expressions)
  44. {
  45. return Expression.Call (null, method, expressions);
  46. }
  47. static TRet Execute<TRet, TSource> (this IQueryable<TSource> source, MethodBase current)
  48. {
  49. return source.Provider.Execute<TRet> (
  50. StaticCall (
  51. MakeGeneric (current, typeof (TSource)),
  52. source.Expression));
  53. }
  54. #region Aggregate
  55. public static TSource Aggregate<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, TSource, TSource>> func)
  56. {
  57. Check.SourceAndFunc (source, func);
  58. return source.Provider.Execute<TSource> (
  59. StaticCall (
  60. MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
  61. source.Expression,
  62. Expression.Quote (func)));
  63. }
  64. public static TAccumulate Aggregate<TSource, TAccumulate> (this IQueryable<TSource> source, TAccumulate seed, Expression<Func<TAccumulate, TSource, TAccumulate>> func)
  65. {
  66. Check.SourceAndFunc (source, func);
  67. return source.Provider.Execute<TAccumulate> (
  68. StaticCall (
  69. MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource), typeof (TAccumulate)),
  70. source.Expression,
  71. Expression.Constant (seed),
  72. Expression.Quote (func)));
  73. }
  74. public static TResult Aggregate<TSource, TAccumulate, TResult> (this IQueryable<TSource> source, TAccumulate seed, Expression<Func<TAccumulate, TSource, TAccumulate>> func, Expression<Func<TAccumulate, TResult>> selector)
  75. {
  76. Check.SourceAndFuncAndSelector (source, func, selector);
  77. return source.Provider.Execute<TResult> (
  78. StaticCall (
  79. MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource), typeof (TAccumulate), typeof (TResult)),
  80. source.Expression,
  81. Expression.Constant (seed),
  82. Expression.Quote (func),
  83. Expression.Quote (selector)));
  84. }
  85. #endregion
  86. #region All
  87. public static bool All<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, bool>> predicate)
  88. {
  89. Check.SourceAndPredicate (source, predicate);
  90. return source.Provider.Execute<bool> (
  91. StaticCall (
  92. MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
  93. source.Expression,
  94. Expression.Quote (predicate)));
  95. }
  96. #endregion
  97. #region Any
  98. public static bool Any<TSource> (this IQueryable<TSource> source)
  99. {
  100. Check.Source (source);
  101. return source.Provider.Execute<bool> (
  102. StaticCall (
  103. MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
  104. source.Expression));
  105. }
  106. public static bool Any<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, bool>> predicate)
  107. {
  108. Check.SourceAndPredicate (source, predicate);
  109. return source.Provider.Execute<bool> (
  110. StaticCall (
  111. MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
  112. source.Expression,
  113. Expression.Quote (predicate)));
  114. }
  115. #endregion
  116. #region AsQueryable
  117. public static IQueryable<TElement> AsQueryable<TElement> (this IEnumerable<TElement> source)
  118. {
  119. var queryable = source as IQueryable<TElement>;
  120. if (queryable != null)
  121. return queryable;
  122. return new QueryableEnumerable<TElement> (source);
  123. }
  124. [MonoTODO]
  125. public static IQueryable AsQueryable (this IEnumerable source)
  126. {
  127. var queryable = source as IQueryable;
  128. if (queryable != null)
  129. return queryable;
  130. throw new NotImplementedException ();
  131. }
  132. #endregion
  133. #region Average
  134. public static double Average (this IQueryable<int> source)
  135. {
  136. Check.Source (source);
  137. return source.Provider.Execute<double>(
  138. StaticCall (
  139. (MethodInfo) MethodBase.GetCurrentMethod (),
  140. source.Expression));
  141. }
  142. public static double? Average(this IQueryable<int?> source)
  143. {
  144. Check.Source (source);
  145. return source.Provider.Execute<double?>(
  146. StaticCall (
  147. (MethodInfo) MethodBase.GetCurrentMethod (),
  148. source.Expression));
  149. }
  150. public static double Average(this IQueryable<long> source)
  151. {
  152. Check.Source (source);
  153. return source.Provider.Execute<double>(
  154. StaticCall (
  155. (MethodInfo) MethodBase.GetCurrentMethod (),
  156. source.Expression));
  157. }
  158. public static double? Average(this IQueryable<long?> source)
  159. {
  160. Check.Source (source);
  161. return source.Provider.Execute<double?>(
  162. StaticCall (
  163. (MethodInfo) MethodBase.GetCurrentMethod (),
  164. source.Expression));
  165. }
  166. public static float Average(this IQueryable<float> source)
  167. {
  168. Check.Source (source);
  169. return source.Provider.Execute<float>(
  170. StaticCall (
  171. (MethodInfo) MethodBase.GetCurrentMethod (),
  172. source.Expression));
  173. }
  174. public static float? Average(this IQueryable<float?> source)
  175. {
  176. Check.Source (source);
  177. return source.Provider.Execute<float?>(
  178. StaticCall (
  179. (MethodInfo) MethodBase.GetCurrentMethod (),
  180. source.Expression));
  181. }
  182. public static double Average (this IQueryable<double> source)
  183. {
  184. Check.Source (source);
  185. return source.Provider.Execute<double> (
  186. StaticCall (
  187. (MethodInfo) MethodBase.GetCurrentMethod (),
  188. source.Expression));
  189. }
  190. public static double? Average (this IQueryable<double?> source)
  191. {
  192. Check.Source (source);
  193. return source.Provider.Execute<double?> (
  194. StaticCall (
  195. (MethodInfo) MethodBase.GetCurrentMethod (),
  196. source.Expression));
  197. }
  198. public static decimal Average(this IQueryable<decimal> source)
  199. {
  200. Check.Source (source);
  201. return source.Provider.Execute<decimal>(
  202. StaticCall (
  203. (MethodInfo) MethodBase.GetCurrentMethod (),
  204. source.Expression));
  205. }
  206. public static decimal? Average(this IQueryable<decimal?> source)
  207. {
  208. Check.Source (source);
  209. return source.Provider.Execute<decimal?>(
  210. StaticCall (
  211. (MethodInfo) MethodBase.GetCurrentMethod (),
  212. source.Expression));
  213. }
  214. public static double Average<TSource>(this IQueryable<TSource> source, Expression<Func<TSource, int>> selector)
  215. {
  216. Check.SourceAndSelector (source, selector);
  217. return source.Provider.Execute<double>(
  218. StaticCall (
  219. MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
  220. source.Expression,
  221. Expression.Quote (selector)));
  222. }
  223. public static double? Average<TSource>(this IQueryable<TSource> source, Expression<Func<TSource, int?>> selector)
  224. {
  225. Check.SourceAndSelector (source, selector);
  226. return source.Provider.Execute<double?>(
  227. StaticCall (
  228. MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
  229. source.Expression,
  230. Expression.Quote (selector)));
  231. }
  232. public static double Average<TSource>(this IQueryable<TSource> source, Expression<Func<TSource, long>> selector)
  233. {
  234. Check.SourceAndSelector (source, selector);
  235. return source.Provider.Execute<double>(
  236. StaticCall (
  237. MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
  238. source.Expression,
  239. Expression.Quote (selector)));
  240. }
  241. public static double? Average<TSource>(this IQueryable<TSource> source, Expression<Func<TSource, long?>> selector)
  242. {
  243. Check.SourceAndSelector (source, selector);
  244. return source.Provider.Execute<double?>(
  245. StaticCall (
  246. MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
  247. source.Expression,
  248. Expression.Quote (selector)));
  249. }
  250. public static float Average<TSource>(this IQueryable<TSource> source, Expression<Func<TSource, float>> selector)
  251. {
  252. Check.SourceAndSelector (source, selector);
  253. return source.Provider.Execute<float>(
  254. StaticCall (
  255. MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
  256. source.Expression,
  257. Expression.Quote (selector)));
  258. }
  259. public static float? Average<TSource>(this IQueryable<TSource> source, Expression<Func<TSource, float?>> selector)
  260. {
  261. Check.SourceAndSelector (source, selector);
  262. return source.Provider.Execute<float?>(
  263. StaticCall (
  264. MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
  265. source.Expression,
  266. Expression.Quote (selector)));
  267. }
  268. public static double Average<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, double>> selector)
  269. {
  270. Check.SourceAndSelector (source, selector);
  271. return source.Provider.Execute<double> (
  272. StaticCall (
  273. MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
  274. source.Expression,
  275. Expression.Quote (selector)));
  276. }
  277. public static double? Average<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, double?>> selector)
  278. {
  279. Check.SourceAndSelector (source, selector);
  280. return source.Provider.Execute<double?> (
  281. StaticCall (
  282. MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
  283. source.Expression,
  284. Expression.Quote (selector)));
  285. }
  286. public static decimal Average<TSource>(this IQueryable<TSource> source, Expression<Func<TSource, decimal>> selector)
  287. {
  288. Check.SourceAndSelector (source, selector);
  289. return source.Provider.Execute<decimal>(
  290. StaticCall (
  291. MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
  292. source.Expression,
  293. Expression.Quote (selector)));
  294. }
  295. public static decimal? Average<TSource>(this IQueryable<TSource> source, Expression<Func<TSource, decimal?>> selector)
  296. {
  297. Check.SourceAndSelector (source, selector);
  298. return source.Provider.Execute<decimal?>(
  299. StaticCall (
  300. MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
  301. source.Expression,
  302. Expression.Quote (selector)));
  303. }
  304. #endregion
  305. #region Cast
  306. public static IQueryable<TResult> Cast<TResult> (this IQueryable source)
  307. {
  308. Check.Source (source);
  309. return (IQueryable<TResult>) source.Provider.CreateQuery (
  310. StaticCall (MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TResult)),
  311. source.Expression));
  312. }
  313. #endregion
  314. #region Concat
  315. public static IQueryable<TSource> Concat<TSource> (this IQueryable<TSource> source1, IEnumerable<TSource> source2)
  316. {
  317. Check.Source1AndSource2 (source1, source2);
  318. return source1.Provider.CreateQuery<TSource> (
  319. StaticCall (
  320. MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
  321. source1.Expression,
  322. Expression.Constant (source2)));
  323. }
  324. #endregion
  325. #region Contains
  326. public static bool Contains<TSource> (this IQueryable<TSource> source, TSource item)
  327. {
  328. Check.Source (source);
  329. return source.Provider.Execute<bool> (
  330. StaticCall (
  331. MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
  332. source.Expression,
  333. Expression.Constant (item)));
  334. }
  335. public static bool Contains<TSource> (this IQueryable<TSource> source, TSource item, IEqualityComparer<TSource> comparer)
  336. {
  337. Check.Source (source);
  338. return source.Provider.Execute<bool> (
  339. StaticCall (
  340. MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
  341. source.Expression,
  342. Expression.Constant (item),
  343. Expression.Constant (comparer)));
  344. }
  345. #endregion
  346. #region Count
  347. public static int Count<TSource> (this IQueryable<TSource> source)
  348. {
  349. Check.Source (source);
  350. return source.Execute<int, TSource> (MethodBase.GetCurrentMethod ());
  351. }
  352. public static int Count<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, bool>> predicate)
  353. {
  354. Check.SourceAndPredicate (source, predicate);
  355. return source.Provider.Execute<int> (
  356. StaticCall (
  357. MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
  358. source.Expression,
  359. Expression.Quote (predicate)));
  360. }
  361. #endregion
  362. #region DefaultIfEmpty
  363. public static IQueryable<TSource> DefaultIfEmpty<TSource> (this IQueryable<TSource> source)
  364. {
  365. Check.Source (source);
  366. return source.Provider.CreateQuery<TSource> (
  367. StaticCall (
  368. MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
  369. source.Expression));
  370. }
  371. public static IQueryable<TSource> DefaultIfEmpty<TSource> (this IQueryable<TSource> source, TSource defaultValue)
  372. {
  373. Check.Source (source);
  374. return source.Provider.CreateQuery<TSource> (
  375. StaticCall (
  376. MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
  377. source.Expression,
  378. Expression.Constant (defaultValue)));
  379. }
  380. #endregion
  381. #region Distinct
  382. public static IQueryable<TSource> Distinct<TSource> (this IQueryable<TSource> source)
  383. {
  384. Check.Source (source);
  385. return source.Provider.CreateQuery<TSource> (
  386. StaticCall (
  387. MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
  388. source.Expression));
  389. }
  390. public static IQueryable<TSource> Distinct<TSource> (this IQueryable<TSource> source, IEqualityComparer<TSource> comparer)
  391. {
  392. Check.Source (source);
  393. return source.Provider.CreateQuery<TSource> (
  394. StaticCall (
  395. MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
  396. source.Expression,
  397. Expression.Constant (comparer)));
  398. }
  399. #endregion
  400. #region ElementAt
  401. public static TSource ElementAt<TSource> (this IQueryable<TSource> source, int index)
  402. {
  403. Check.Source (source);
  404. return source.Provider.Execute<TSource> (
  405. StaticCall (
  406. MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
  407. source.Expression,
  408. Expression.Constant (index)));
  409. }
  410. #endregion
  411. #region ElementAtOrDefault
  412. public static TSource ElementAtOrDefault<TSource> (this IQueryable<TSource> source, int index)
  413. {
  414. Check.Source (source);
  415. return source.Provider.Execute<TSource> (
  416. StaticCall (
  417. MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
  418. source.Expression,
  419. Expression.Constant (index)));
  420. }
  421. #endregion
  422. #region Except
  423. public static IQueryable<TSource> Except<TSource> (this IQueryable<TSource> source1, IEnumerable<TSource> source2)
  424. {
  425. Check.Source1AndSource2 (source1, source2);
  426. return source1.Provider.CreateQuery<TSource> (
  427. StaticCall (
  428. MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
  429. source1.Expression,
  430. Expression.Constant (source2)));
  431. }
  432. public static IQueryable<TSource> Except<TSource> (this IQueryable<TSource> source1, IEnumerable<TSource> source2, IEqualityComparer<TSource> comparer)
  433. {
  434. Check.Source1AndSource2 (source1, source2);
  435. return source1.Provider.CreateQuery<TSource> (
  436. StaticCall (
  437. MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
  438. source1.Expression,
  439. Expression.Constant (source2),
  440. Expression.Constant (comparer)));
  441. }
  442. #endregion
  443. #region First
  444. public static TSource First<TSource> (this IQueryable<TSource> source)
  445. {
  446. Check.Source (source);
  447. return source.Provider.Execute<TSource> (
  448. StaticCall (
  449. MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
  450. source.Expression));
  451. }
  452. public static TSource First<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, bool>> predicate)
  453. {
  454. Check.SourceAndPredicate (source, predicate);
  455. return source.Provider.Execute<TSource> (
  456. StaticCall (
  457. MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
  458. source.Expression,
  459. Expression.Quote (predicate)));
  460. }
  461. #endregion
  462. #region FirstOrDefault
  463. public static TSource FirstOrDefault<TSource> (this IQueryable<TSource> source)
  464. {
  465. Check.Source (source);
  466. return source.Provider.Execute<TSource> (
  467. StaticCall (
  468. MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
  469. source.Expression));
  470. }
  471. public static TSource FirstOrDefault<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, bool>> predicate)
  472. {
  473. Check.SourceAndPredicate (source, predicate);
  474. return source.Provider.Execute<TSource> (
  475. StaticCall (
  476. MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
  477. source.Expression,
  478. Expression.Quote (predicate)));
  479. }
  480. #endregion
  481. #region GroupBy
  482. public static IQueryable<IGrouping<TKey, TSource>> GroupBy<TSource, TKey> (this IQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector)
  483. {
  484. Check.SourceAndKeySelector (source, keySelector);
  485. return source.Provider.CreateQuery<IGrouping<TKey, TSource>> (
  486. StaticCall (
  487. MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource), typeof (TKey)),
  488. source.Expression,
  489. Expression.Quote (keySelector)));
  490. }
  491. public static IQueryable<IGrouping<TKey, TSource>> GroupBy<TSource, TKey> (this IQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector, IEqualityComparer<TKey> comparer)
  492. {
  493. Check.SourceAndKeySelector (source, keySelector);
  494. return source.Provider.CreateQuery<IGrouping<TKey, TSource>> (
  495. StaticCall (
  496. MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource), typeof (TKey)),
  497. source.Expression,
  498. Expression.Quote (keySelector),
  499. Expression.Constant (comparer)));
  500. }
  501. public static IQueryable<IGrouping<TKey, TElement>> GroupBy<TSource, TKey, TElement> (this IQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector, Expression<Func<TSource, TElement>> elementSelector)
  502. {
  503. Check.SourceAndKeyElementSelectors (source, keySelector, elementSelector);
  504. return source.Provider.CreateQuery<IGrouping<TKey, TElement>>(
  505. StaticCall (
  506. MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource), typeof (TKey), typeof (TElement)),
  507. source.Expression,
  508. Expression.Quote (keySelector),
  509. Expression.Quote (elementSelector)));
  510. }
  511. public static IQueryable<TResult> GroupBy<TSource, TKey, TResult> (this IQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector, Expression<Func<TKey, IEnumerable<TSource>, TResult>> resultSelector)
  512. {
  513. Check.SourceAndKeyResultSelectors (source, keySelector, resultSelector);
  514. return source.Provider.CreateQuery<TResult> (
  515. StaticCall (
  516. MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource), typeof (TKey), typeof (TResult)),
  517. source.Expression,
  518. Expression.Quote (keySelector),
  519. Expression.Quote (resultSelector)));
  520. }
  521. 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)
  522. {
  523. Check.SourceAndKeyElementSelectors (source, keySelector, elementSelector);
  524. return source.Provider.CreateQuery<IGrouping<TKey, TElement>> (
  525. StaticCall (
  526. MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource), typeof (TKey), typeof (TElement)),
  527. source.Expression,
  528. Expression.Quote (keySelector),
  529. Expression.Quote (elementSelector),
  530. Expression.Constant (comparer)));
  531. }
  532. 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)
  533. {
  534. Check.GroupBySelectors (source, keySelector, elementSelector, resultSelector);
  535. return source.Provider.CreateQuery<TResult> (
  536. StaticCall (
  537. MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource), typeof (TKey), typeof (TElement), typeof (TResult)),
  538. source.Expression,
  539. Expression.Quote (keySelector),
  540. Expression.Quote (elementSelector),
  541. Expression.Quote (resultSelector)));
  542. }
  543. 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)
  544. {
  545. Check.SourceAndKeyResultSelectors (source, keySelector, resultSelector);
  546. return source.Provider.CreateQuery<TResult> (
  547. StaticCall (
  548. MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource), typeof (TKey), typeof (TResult)),
  549. source.Expression,
  550. Expression.Quote (keySelector),
  551. Expression.Quote (resultSelector),
  552. Expression.Constant (comparer)));
  553. }
  554. 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)
  555. {
  556. Check.GroupBySelectors (source, keySelector, elementSelector, resultSelector);
  557. return source.Provider.CreateQuery<TResult> (
  558. StaticCall (
  559. MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource), typeof (TKey), typeof (TElement), typeof (TResult)),
  560. source.Expression,
  561. Expression.Quote (keySelector),
  562. Expression.Quote (elementSelector),
  563. Expression.Quote (resultSelector),
  564. Expression.Constant (comparer)));
  565. }
  566. #endregion
  567. #region GroupJoin
  568. 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)
  569. {
  570. if (outer == null)
  571. throw new ArgumentNullException ("outer");
  572. if (inner == null)
  573. throw new ArgumentNullException ("inner");
  574. if (outerKeySelector == null)
  575. throw new ArgumentNullException ("outerKeySelector");
  576. if (innerKeySelector == null)
  577. throw new ArgumentNullException ("innerKeySelector");
  578. if (resultSelector == null)
  579. throw new ArgumentNullException ("resultSelector");
  580. return outer.Provider.CreateQuery<TResult> (
  581. StaticCall (
  582. MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TOuter), typeof (TInner), typeof (TKey), typeof (TResult)),
  583. outer.Expression,
  584. Expression.Constant (inner),
  585. Expression.Quote (outerKeySelector),
  586. Expression.Quote (innerKeySelector),
  587. Expression.Quote (resultSelector)));
  588. }
  589. 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)
  590. {
  591. if (outer == null)
  592. throw new ArgumentNullException ("outer");
  593. if (inner == null)
  594. throw new ArgumentNullException ("inner");
  595. if (outerKeySelector == null)
  596. throw new ArgumentNullException ("outerKeySelector");
  597. if (innerKeySelector == null)
  598. throw new ArgumentNullException ("innerKeySelector");
  599. if (resultSelector == null)
  600. throw new ArgumentNullException ("resultSelector");
  601. return outer.Provider.CreateQuery<TResult> (
  602. StaticCall (
  603. MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TOuter), typeof (TInner), typeof (TKey), typeof (TResult)),
  604. outer.Expression,
  605. Expression.Constant (inner),
  606. Expression.Quote (outerKeySelector),
  607. Expression.Quote (innerKeySelector),
  608. Expression.Quote (resultSelector),
  609. Expression.Constant (comparer)));
  610. }
  611. #endregion
  612. #region Intersect
  613. public static IQueryable<TSource> Intersect<TSource> (this IQueryable<TSource> source1, IEnumerable<TSource> source2)
  614. {
  615. Check.Source1AndSource2 (source1, source2);
  616. return source1.Provider.CreateQuery<TSource> (
  617. StaticCall (
  618. MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
  619. source1.Expression,
  620. Expression.Constant (source2)));
  621. }
  622. public static IQueryable<TSource> Intersect<TSource> (this IQueryable<TSource> source1, IEnumerable<TSource> source2, IEqualityComparer<TSource> comparer)
  623. {
  624. Check.Source1AndSource2 (source1, source2);
  625. return source1.Provider.CreateQuery<TSource> (
  626. StaticCall (
  627. MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
  628. source1.Expression,
  629. Expression.Constant (source2),
  630. Expression.Constant (comparer)));
  631. }
  632. #endregion
  633. #region Join
  634. 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)
  635. {
  636. Check.JoinSelectors (outer, inner, outerKeySelector, innerKeySelector, resultSelector);
  637. return outer.Provider.CreateQuery<TResult> (
  638. StaticCall (
  639. MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TOuter), typeof (TInner), typeof (TKey), typeof (TResult)),
  640. outer.Expression,
  641. Expression.Constant (inner),
  642. Expression.Quote (outerKeySelector),
  643. Expression.Quote (innerKeySelector),
  644. Expression.Quote (resultSelector)));
  645. }
  646. 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)
  647. {
  648. Check.JoinSelectors (outer, inner, outerKeySelector, innerKeySelector, resultSelector);
  649. return outer.Provider.CreateQuery<TResult> (
  650. StaticCall (
  651. MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TOuter), typeof (TInner), typeof (TKey), typeof (TResult)),
  652. outer.Expression,
  653. Expression.Constant (inner),
  654. Expression.Quote (outerKeySelector),
  655. Expression.Quote (innerKeySelector),
  656. Expression.Quote (resultSelector),
  657. Expression.Constant (comparer)));
  658. }
  659. #endregion
  660. #region Last
  661. public static TSource Last<TSource> (this IQueryable<TSource> source)
  662. {
  663. Check.Source (source);
  664. return source.Provider.Execute<TSource> (
  665. StaticCall (
  666. MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
  667. source.Expression));
  668. }
  669. public static TSource Last<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, bool>> predicate)
  670. {
  671. Check.SourceAndPredicate (source, predicate);
  672. return source.Provider.Execute<TSource> (
  673. StaticCall (
  674. MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
  675. source.Expression,
  676. Expression.Quote (predicate)));
  677. }
  678. #endregion
  679. #region LastOrDefault
  680. public static TSource LastOrDefault<TSource> (this IQueryable<TSource> source)
  681. {
  682. Check.Source (source);
  683. return source.Provider.Execute<TSource> (
  684. StaticCall (
  685. MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
  686. source.Expression));
  687. }
  688. public static TSource LastOrDefault<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, bool>> predicate)
  689. {
  690. Check.SourceAndPredicate (source, predicate);
  691. return source.Provider.Execute<TSource> (
  692. StaticCall (
  693. MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
  694. source.Expression,
  695. Expression.Quote (predicate)));
  696. }
  697. #endregion
  698. #region LongCount
  699. public static long LongCount<TSource> (this IQueryable<TSource> source)
  700. {
  701. Check.Source (source);
  702. return source.Execute<long, TSource> (MethodBase.GetCurrentMethod ());
  703. }
  704. public static long LongCount<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, bool>> predicate)
  705. {
  706. Check.SourceAndPredicate (source, predicate);
  707. return source.Provider.Execute<long> (
  708. StaticCall (
  709. MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
  710. source.Expression,
  711. Expression.Quote (predicate)));
  712. }
  713. #endregion
  714. #region Max
  715. public static TSource Max<TSource> (this IQueryable<TSource> source)
  716. {
  717. Check.Source (source);
  718. return source.Provider.Execute<TSource> (
  719. StaticCall (
  720. MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
  721. source.Expression));
  722. }
  723. public static TResult Max<TSource, TResult> (this IQueryable<TSource> source, Expression<Func<TSource, TResult>> selector)
  724. {
  725. Check.Source (source);
  726. return source.Provider.Execute<TResult> (
  727. StaticCall (
  728. MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource), typeof (TResult)),
  729. source.Expression,
  730. Expression.Quote (selector)));
  731. }
  732. #endregion
  733. #region Min
  734. public static TSource Min<TSource> (this IQueryable<TSource> source)
  735. {
  736. Check.Source (source);
  737. return source.Provider.Execute<TSource> (
  738. StaticCall (
  739. MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
  740. source.Expression));
  741. }
  742. public static TResult Min<TSource, TResult> (this IQueryable<TSource> source, Expression<Func<TSource, TResult>> selector)
  743. {
  744. Check.SourceAndSelector (source, selector);
  745. return source.Provider.Execute<TResult> (
  746. StaticCall (
  747. MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource), typeof (TResult)),
  748. source.Expression,
  749. Expression.Quote (selector)));
  750. }
  751. #endregion
  752. #region OfType
  753. public static IQueryable<TResult> OfType<TResult> (this IQueryable source)
  754. {
  755. Check.Source (source);
  756. return (IQueryable<TResult>) source.Provider.CreateQuery (
  757. StaticCall (
  758. MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TResult)),
  759. source.Expression));
  760. }
  761. #endregion
  762. #region OrderBy
  763. public static IOrderedQueryable<TSource> OrderBy<TSource, TKey> (this IQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector)
  764. {
  765. Check.SourceAndKeySelector (source, keySelector);
  766. return (IOrderedQueryable<TSource>) source.Provider.CreateQuery<TSource> (
  767. StaticCall (
  768. MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource), typeof (TKey)),
  769. source.Expression,
  770. Expression.Quote (keySelector)));
  771. }
  772. public static IOrderedQueryable<TSource> OrderBy<TSource, TKey> (this IQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector, IComparer<TKey> comparer)
  773. {
  774. Check.SourceAndKeySelector (source, keySelector);
  775. return (IOrderedQueryable<TSource>) source.Provider.CreateQuery<TSource> (
  776. StaticCall (
  777. MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource), typeof (TKey)),
  778. source.Expression,
  779. Expression.Quote (keySelector),
  780. Expression.Constant (comparer)));
  781. }
  782. #endregion
  783. #region OrderByDescending
  784. public static IOrderedQueryable<TSource> OrderByDescending<TSource, TKey> (this IQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector)
  785. {
  786. Check.SourceAndKeySelector (source, keySelector);
  787. return (IOrderedQueryable<TSource>) source.Provider.CreateQuery<TSource> (
  788. StaticCall (
  789. MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource), typeof (TKey)),
  790. source.Expression,
  791. Expression.Quote (keySelector)));
  792. }
  793. public static IOrderedQueryable<TSource> OrderByDescending<TSource, TKey> (this IQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector, IComparer<TKey> comparer)
  794. {
  795. Check.SourceAndKeySelector (source, keySelector);
  796. return (IOrderedQueryable<TSource>) source.Provider.CreateQuery<TSource> (
  797. StaticCall (
  798. MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource), typeof (TKey)),
  799. source.Expression,
  800. Expression.Quote (keySelector),
  801. Expression.Constant (comparer)));
  802. }
  803. #endregion
  804. #region Reverse
  805. public static IQueryable<TSource> Reverse<TSource> (this IQueryable<TSource> source)
  806. {
  807. Check.Source (source);
  808. return source.Provider.CreateQuery<TSource> (
  809. StaticCall (
  810. MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
  811. source.Expression));
  812. }
  813. #endregion
  814. #region Select
  815. public static IQueryable<TResult> Select<TSource, TResult> (this IQueryable<TSource> source, Expression<Func<TSource, TResult>> selector)
  816. {
  817. Check.SourceAndSelector (source, selector);
  818. return source.Provider.CreateQuery<TResult> (
  819. StaticCall (
  820. MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource), typeof (TResult)),
  821. source.Expression,
  822. Expression.Quote (selector)));
  823. }
  824. public static IQueryable<TResult> Select<TSource, TResult> (this IQueryable<TSource> source, Expression<Func<TSource, int, TResult>> selector)
  825. {
  826. Check.SourceAndSelector (source, selector);
  827. return source.Provider.CreateQuery<TResult> (
  828. StaticCall (
  829. MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource), typeof (TResult)),
  830. source.Expression,
  831. Expression.Quote (selector)));
  832. }
  833. #endregion
  834. #region SelectMany
  835. public static IQueryable<TResult> SelectMany<TSource, TResult> (this IQueryable<TSource> source, Expression<Func<TSource, IEnumerable<TResult>>> selector)
  836. {
  837. Check.SourceAndSelector (source, selector);
  838. return source.Provider.CreateQuery<TResult> (
  839. StaticCall (
  840. MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource), typeof (TResult)),
  841. source.Expression,
  842. Expression.Quote (selector)));
  843. }
  844. public static IQueryable<TResult> SelectMany<TSource, TResult> (this IQueryable<TSource> source, Expression<Func<TSource, int, IEnumerable<TResult>>> selector)
  845. {
  846. Check.SourceAndSelector (source, selector);
  847. return source.Provider.CreateQuery<TResult> (
  848. StaticCall (
  849. MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource), typeof (TResult)),
  850. source.Expression,
  851. Expression.Quote (selector)));
  852. }
  853. 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)
  854. {
  855. Check.SourceAndCollectionSelectorAndResultSelector (source, collectionSelector, resultSelector);
  856. return source.Provider.CreateQuery<TResult> (
  857. StaticCall (
  858. MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource), typeof (TCollection), typeof (TResult)),
  859. source.Expression,
  860. Expression.Quote (collectionSelector),
  861. Expression.Quote (resultSelector)));
  862. }
  863. public static IQueryable<TResult> SelectMany<TSource, TCollection, TResult> (this IQueryable<TSource> source, Expression<Func<TSource, IEnumerable<TCollection>>> collectionSelector, Expression<Func<TSource, TCollection, TResult>> resultSelector)
  864. {
  865. Check.SourceAndCollectionSelectorAndResultSelector (source, collectionSelector, resultSelector);
  866. return source.Provider.CreateQuery<TResult> (
  867. StaticCall (
  868. MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource), typeof (TCollection), typeof (TResult)),
  869. source.Expression,
  870. Expression.Quote (collectionSelector),
  871. Expression.Quote (resultSelector)));
  872. }
  873. #endregion
  874. #region SequenceEqual
  875. public static bool SequenceEqual<TSource> (this IQueryable<TSource> source1, IEnumerable<TSource> source2)
  876. {
  877. Check.Source1AndSource2 (source1, source2);
  878. return source1.Provider.Execute<bool> (
  879. StaticCall (
  880. MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
  881. source1.Expression,
  882. Expression.Constant (source2)));
  883. }
  884. public static bool SequenceEqual<TSource> (this IQueryable<TSource> source1, IEnumerable<TSource> source2, IEqualityComparer<TSource> comparer)
  885. {
  886. Check.Source1AndSource2 (source1, source2);
  887. return source1.Provider.Execute<bool> (
  888. StaticCall (
  889. MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
  890. source1.Expression,
  891. Expression.Constant (source2),
  892. Expression.Constant (comparer)));
  893. }
  894. #endregion
  895. #region Single
  896. public static TSource Single<TSource> (this IQueryable<TSource> source)
  897. {
  898. Check.Source (source);
  899. return source.Provider.Execute<TSource> (
  900. StaticCall (
  901. MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
  902. source.Expression));
  903. }
  904. public static TSource Single<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, bool>> predicate)
  905. {
  906. Check.SourceAndPredicate (source, predicate);
  907. return source.Provider.Execute<TSource> (
  908. StaticCall (
  909. MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
  910. source.Expression,
  911. Expression.Quote (predicate)));
  912. }
  913. #endregion
  914. #region SingleOrDefault
  915. public static TSource SingleOrDefault<TSource> (this IQueryable<TSource> source)
  916. {
  917. Check.Source (source);
  918. return source.Provider.Execute<TSource> (
  919. StaticCall (
  920. MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
  921. source.Expression));
  922. }
  923. public static TSource SingleOrDefault<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, bool>> predicate)
  924. {
  925. Check.SourceAndPredicate (source, predicate);
  926. return source.Provider.Execute<TSource> (
  927. StaticCall (
  928. MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
  929. source.Expression,
  930. Expression.Quote (predicate)));
  931. }
  932. #endregion
  933. #region Skip
  934. public static IQueryable<TSource> Skip<TSource> (this IQueryable<TSource> source, int count)
  935. {
  936. Check.Source (source);
  937. return source.Provider.CreateQuery<TSource> (
  938. StaticCall (
  939. MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
  940. source.Expression,
  941. Expression.Constant (count)));
  942. }
  943. #endregion
  944. #region SkipWhile
  945. public static IQueryable<TSource> SkipWhile<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, bool>> predicate)
  946. {
  947. Check.SourceAndPredicate (source, predicate);
  948. return source.Provider.CreateQuery<TSource> (
  949. StaticCall (
  950. MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
  951. source.Expression,
  952. Expression.Quote (predicate)));
  953. }
  954. public static IQueryable<TSource> SkipWhile<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, int, bool>> predicate)
  955. {
  956. Check.SourceAndPredicate (source, predicate);
  957. return source.Provider.CreateQuery<TSource> (
  958. StaticCall (
  959. MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
  960. source.Expression,
  961. Expression.Quote (predicate)));
  962. }
  963. #endregion
  964. #region Sum
  965. public static int Sum<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, int>> selector)
  966. {
  967. Check.SourceAndSelector (source, selector);
  968. return source.Provider.Execute<int> (
  969. StaticCall (
  970. MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
  971. source.Expression,
  972. Expression.Quote (selector)));
  973. }
  974. public static int? Sum<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, int?>> selector)
  975. {
  976. Check.SourceAndSelector (source, selector);
  977. return source.Provider.Execute<int?> (
  978. StaticCall (
  979. MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
  980. source.Expression,
  981. Expression.Quote (selector)));
  982. }
  983. public static long Sum<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, long>> selector)
  984. {
  985. Check.SourceAndSelector (source, selector);
  986. return source.Provider.Execute<long> (
  987. StaticCall (
  988. MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
  989. source.Expression,
  990. Expression.Quote (selector)));
  991. }
  992. public static long? Sum<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, long?>> selector)
  993. {
  994. Check.SourceAndSelector (source, selector);
  995. return source.Provider.Execute<long?> (
  996. StaticCall (
  997. MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
  998. source.Expression,
  999. Expression.Quote (selector)));
  1000. }
  1001. public static float Sum<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, float>> selector)
  1002. {
  1003. Check.SourceAndSelector (source, selector);
  1004. return source.Provider.Execute<float> (
  1005. StaticCall (
  1006. MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
  1007. source.Expression,
  1008. Expression.Quote (selector)));
  1009. }
  1010. public static float? Sum<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, float?>> selector)
  1011. {
  1012. Check.SourceAndSelector (source, selector);
  1013. return source.Provider.Execute<float?> (
  1014. StaticCall (
  1015. MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
  1016. source.Expression,
  1017. Expression.Quote (selector)));
  1018. }
  1019. public static double Sum<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, double>> selector)
  1020. {
  1021. Check.SourceAndSelector (source, selector);
  1022. return source.Provider.Execute<double> (
  1023. StaticCall (
  1024. MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
  1025. source.Expression,
  1026. Expression.Quote (selector)));
  1027. }
  1028. public static double? Sum<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, double?>> selector)
  1029. {
  1030. Check.SourceAndSelector (source, selector);
  1031. return source.Provider.Execute<double?> (
  1032. StaticCall (
  1033. MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
  1034. source.Expression,
  1035. Expression.Quote (selector)));
  1036. }
  1037. public static decimal Sum<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, decimal>> selector)
  1038. {
  1039. Check.SourceAndSelector (source, selector);
  1040. return source.Provider.Execute<decimal> (
  1041. StaticCall (
  1042. MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
  1043. source.Expression,
  1044. Expression.Quote (selector)));
  1045. }
  1046. public static decimal? Sum<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, decimal?>> selector)
  1047. {
  1048. Check.SourceAndSelector (source, selector);
  1049. return source.Provider.Execute<decimal?> (
  1050. StaticCall (
  1051. MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
  1052. source.Expression,
  1053. Expression.Quote (selector)));
  1054. }
  1055. public static int Sum (this IQueryable<int> source)
  1056. {
  1057. Check.Source (source);
  1058. return source.Provider.Execute<int> (
  1059. StaticCall (
  1060. (MethodInfo) MethodBase.GetCurrentMethod (),
  1061. source.Expression));
  1062. }
  1063. public static int? Sum (this IQueryable<int?> source)
  1064. {
  1065. Check.Source (source);
  1066. return source.Provider.Execute<int?> (
  1067. StaticCall (
  1068. (MethodInfo) MethodBase.GetCurrentMethod (),
  1069. source.Expression));
  1070. }
  1071. public static long Sum (this IQueryable<long> source)
  1072. {
  1073. Check.Source (source);
  1074. return source.Provider.Execute<long> (
  1075. StaticCall (
  1076. (MethodInfo) MethodBase.GetCurrentMethod (),
  1077. source.Expression));
  1078. }
  1079. public static long? Sum (this IQueryable<long?> source)
  1080. {
  1081. Check.Source (source);
  1082. return source.Provider.Execute<long?> (
  1083. StaticCall (
  1084. (MethodInfo) MethodBase.GetCurrentMethod (),
  1085. source.Expression));
  1086. }
  1087. public static float Sum (this IQueryable<float> source)
  1088. {
  1089. Check.Source (source);
  1090. return source.Provider.Execute<float> (
  1091. StaticCall (
  1092. (MethodInfo) MethodBase.GetCurrentMethod (),
  1093. source.Expression));
  1094. }
  1095. public static float? Sum (this IQueryable<float?> source)
  1096. {
  1097. Check.Source (source);
  1098. return source.Provider.Execute<float?> (
  1099. StaticCall (
  1100. (MethodInfo) MethodBase.GetCurrentMethod (),
  1101. source.Expression));
  1102. }
  1103. public static double Sum (this IQueryable<double> source)
  1104. {
  1105. Check.Source (source);
  1106. return source.Provider.Execute<double> (
  1107. StaticCall (
  1108. (MethodInfo) MethodBase.GetCurrentMethod (),
  1109. source.Expression));
  1110. }
  1111. public static double? Sum (this IQueryable<double?> source)
  1112. {
  1113. Check.Source (source);
  1114. return source.Provider.Execute<double?> (
  1115. StaticCall (
  1116. (MethodInfo) MethodBase.GetCurrentMethod (),
  1117. source.Expression));
  1118. }
  1119. public static decimal Sum (this IQueryable<decimal> source)
  1120. {
  1121. Check.Source (source);
  1122. return source.Provider.Execute<decimal> (
  1123. StaticCall (
  1124. (MethodInfo) MethodBase.GetCurrentMethod (),
  1125. source.Expression));
  1126. }
  1127. public static decimal? Sum (this IQueryable<decimal?> source)
  1128. {
  1129. Check.Source (source);
  1130. return source.Provider.Execute<decimal?> (
  1131. StaticCall (
  1132. (MethodInfo) MethodBase.GetCurrentMethod (),
  1133. source.Expression));
  1134. }
  1135. #endregion
  1136. #region Take
  1137. public static IQueryable<TSource> Take<TSource> (this IQueryable<TSource> source, int count)
  1138. {
  1139. Check.Source (source);
  1140. return source.Provider.CreateQuery<TSource> (
  1141. StaticCall (
  1142. MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
  1143. source.Expression,
  1144. Expression.Constant (count)));
  1145. }
  1146. #endregion
  1147. #region TakeWhile
  1148. public static IQueryable<TSource> TakeWhile<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, bool>> predicate)
  1149. {
  1150. Check.SourceAndPredicate (source, predicate);
  1151. return source.Provider.CreateQuery<TSource> (
  1152. StaticCall (
  1153. MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
  1154. source.Expression,
  1155. Expression.Quote (predicate)));
  1156. }
  1157. public static IQueryable<TSource> TakeWhile<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, int, bool>> predicate)
  1158. {
  1159. Check.SourceAndPredicate (source, predicate);
  1160. return source.Provider.CreateQuery<TSource> (
  1161. StaticCall (
  1162. MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
  1163. source.Expression,
  1164. Expression.Quote (predicate)));
  1165. }
  1166. #endregion
  1167. #region ThenBy
  1168. public static IOrderedQueryable<TSource> ThenBy<TSource, TKey> (this IOrderedQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector)
  1169. {
  1170. Check.SourceAndKeySelector (source, keySelector);
  1171. return (IOrderedQueryable<TSource>) source.Provider.CreateQuery (
  1172. StaticCall (
  1173. MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource), typeof (TKey)),
  1174. source.Expression,
  1175. Expression.Quote (keySelector)));
  1176. }
  1177. public static IOrderedQueryable<TSource> ThenBy<TSource, TKey> (this IOrderedQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector, IComparer<TKey> comparer)
  1178. {
  1179. Check.SourceAndKeySelector (source, keySelector);
  1180. return (IOrderedQueryable<TSource>) source.Provider.CreateQuery (
  1181. StaticCall (
  1182. MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource), typeof (TKey)),
  1183. source.Expression,
  1184. Expression.Quote (keySelector),
  1185. Expression.Constant (comparer)));
  1186. }
  1187. #endregion
  1188. #region ThenByDescending
  1189. public static IOrderedQueryable<TSource> ThenByDescending<TSource, TKey> (this IOrderedQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector)
  1190. {
  1191. Check.SourceAndKeySelector (source, keySelector);
  1192. return (IOrderedQueryable<TSource>) source.Provider.CreateQuery (
  1193. StaticCall (
  1194. MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource), typeof (TKey)),
  1195. source.Expression,
  1196. Expression.Quote (keySelector)));
  1197. }
  1198. public static IOrderedQueryable<TSource> ThenByDescending<TSource, TKey> (this IOrderedQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector, IComparer<TKey> comparer)
  1199. {
  1200. Check.SourceAndKeySelector (source, keySelector);
  1201. return (IOrderedQueryable<TSource>) source.Provider.CreateQuery (
  1202. StaticCall (
  1203. MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource), typeof (TKey)),
  1204. source.Expression,
  1205. Expression.Quote (keySelector),
  1206. Expression.Constant (comparer)));
  1207. }
  1208. #endregion
  1209. #region Union
  1210. public static IQueryable<TSource> Union<TSource> (this IQueryable<TSource> source1, IEnumerable<TSource> source2)
  1211. {
  1212. Check.Source1AndSource2 (source1, source2);
  1213. return source1.Provider.CreateQuery<TSource> (
  1214. StaticCall (
  1215. MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
  1216. source1.Expression,
  1217. Expression.Constant (source2)));
  1218. }
  1219. public static IQueryable<TSource> Union<TSource>(this IQueryable<TSource> source1, IEnumerable<TSource> source2, IEqualityComparer<TSource> comparer)
  1220. {
  1221. Check.Source1AndSource2 (source1, source2);
  1222. return source1.Provider.CreateQuery<TSource> (
  1223. StaticCall (
  1224. MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
  1225. source1.Expression,
  1226. Expression.Constant (source2),
  1227. Expression.Constant (comparer)));
  1228. }
  1229. #endregion
  1230. #region Where
  1231. public static IQueryable<TSource> Where<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, bool>> predicate)
  1232. {
  1233. Check.SourceAndPredicate (source, predicate);
  1234. return source.Provider.CreateQuery<TSource> (
  1235. StaticCall (
  1236. MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
  1237. source.Expression,
  1238. Expression.Quote (predicate)));
  1239. }
  1240. public static IQueryable<TSource> Where<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, int, bool>> predicate)
  1241. {
  1242. Check.SourceAndPredicate (source, predicate);
  1243. return source.Provider.CreateQuery<TSource> (
  1244. StaticCall (
  1245. MakeGeneric (MethodBase.GetCurrentMethod (), typeof (TSource)),
  1246. source.Expression,
  1247. Expression.Quote (predicate)));
  1248. }
  1249. #endregion
  1250. }
  1251. }