Queryable.cs 49 KB

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