QueryableEx.Generated.cs 103 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT License.
  3. // See the LICENSE file in the project root for more information.
  4. using System.Collections.Generic;
  5. using System.ComponentModel;
  6. using System.Linq.Expressions;
  7. using System.Reflection;
  8. namespace System.Linq
  9. {
  10. public static partial class QueryableEx
  11. {
  12. /// <summary>
  13. /// Determines whether an enumerable sequence is empty.
  14. /// </summary>
  15. /// <typeparam name="TSource">Source sequence element type.</typeparam>
  16. /// <param name="source">Source sequence.</param>
  17. /// <returns>true if the sequence is empty; false otherwise.</returns>
  18. public static bool IsEmpty<TSource>(this IQueryable<TSource> source)
  19. {
  20. if (source == null)
  21. throw new ArgumentNullException(nameof(source));
  22. return source.Provider.Execute<bool>(
  23. Expression.Call(
  24. null,
  25. ((MethodInfo)MethodInfo.GetCurrentMethod()).MakeGenericMethod(typeof(TSource)),
  26. source.Expression
  27. )
  28. );
  29. }
  30. #pragma warning disable 1591
  31. [EditorBrowsable(EditorBrowsableState.Never)]
  32. public static bool IsEmpty<TSource>(IEnumerable<TSource> source)
  33. {
  34. return EnumerableEx.IsEmpty(source);
  35. }
  36. #pragma warning restore 1591
  37. /// <summary>
  38. /// Returns the minimum value in the enumerable sequence by using the specified comparer to compare values.
  39. /// </summary>
  40. /// <typeparam name="TSource">Source sequence element type.</typeparam>
  41. /// <param name="source">Source sequence.</param>
  42. /// <param name="comparer">Comparer used to determine the minimum value.</param>
  43. /// <returns>Minimum value in the sequence.</returns>
  44. public static TSource Min<TSource>(this IQueryable<TSource> source, IComparer<TSource> comparer)
  45. {
  46. if (source == null)
  47. throw new ArgumentNullException(nameof(source));
  48. if (comparer == null)
  49. throw new ArgumentNullException(nameof(comparer));
  50. return source.Provider.Execute<TSource>(
  51. Expression.Call(
  52. null,
  53. ((MethodInfo)MethodInfo.GetCurrentMethod()).MakeGenericMethod(typeof(TSource)),
  54. source.Expression,
  55. Expression.Constant(comparer, typeof(IComparer<TSource>))
  56. )
  57. );
  58. }
  59. #pragma warning disable 1591
  60. [EditorBrowsable(EditorBrowsableState.Never)]
  61. public static TSource Min<TSource>(IEnumerable<TSource> source, IComparer<TSource> comparer)
  62. {
  63. #if REFERENCE_ASSEMBLY
  64. return default;
  65. #else
  66. return EnumerableEx.Min(source, comparer);
  67. #endif
  68. }
  69. #pragma warning restore 1591
  70. /// <summary>
  71. /// Returns the elements with the minimum key value by using the default comparer to compare key values.
  72. /// </summary>
  73. /// <typeparam name="TSource">Source sequence element type.</typeparam>
  74. /// <typeparam name="TKey">Key type.</typeparam>
  75. /// <param name="source">Source sequence.</param>
  76. /// <param name="keySelector">Key selector used to extract the key for each element in the sequence.</param>
  77. /// <returns>List with the elements that share the same minimum key value.</returns>
  78. public static IList<TSource> MinBy<TSource, TKey>(this IQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector)
  79. {
  80. if (source == null)
  81. throw new ArgumentNullException(nameof(source));
  82. if (keySelector == null)
  83. throw new ArgumentNullException(nameof(keySelector));
  84. return source.Provider.Execute<IList<TSource>>(
  85. Expression.Call(
  86. null,
  87. ((MethodInfo)MethodInfo.GetCurrentMethod()).MakeGenericMethod(typeof(TSource), typeof(TKey)),
  88. source.Expression,
  89. keySelector
  90. )
  91. );
  92. }
  93. #pragma warning disable 1591
  94. [EditorBrowsable(EditorBrowsableState.Never)]
  95. public static IList<TSource> MinBy<TSource, TKey>(IEnumerable<TSource> source, Func<TSource, TKey> keySelector)
  96. {
  97. #if REFERENCE_ASSEMBLY
  98. return default;
  99. #else
  100. return EnumerableEx.MinByWithTies(source, keySelector);
  101. #endif
  102. }
  103. #pragma warning restore 1591
  104. /// <summary>
  105. /// Returns the elements with the minimum key value by using the specified comparer to compare key values.
  106. /// </summary>
  107. /// <typeparam name="TSource">Source sequence element type.</typeparam>
  108. /// <typeparam name="TKey">Key type.</typeparam>
  109. /// <param name="source">Source sequence.</param>
  110. /// <param name="keySelector">Key selector used to extract the key for each element in the sequence.</param>
  111. /// <param name="comparer">Comparer used to determine the minimum key value.</param>
  112. /// <returns>List with the elements that share the same minimum key value.</returns>
  113. public static IList<TSource> MinBy<TSource, TKey>(this IQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector, IComparer<TKey> comparer)
  114. {
  115. if (source == null)
  116. throw new ArgumentNullException(nameof(source));
  117. if (keySelector == null)
  118. throw new ArgumentNullException(nameof(keySelector));
  119. if (comparer == null)
  120. throw new ArgumentNullException(nameof(comparer));
  121. return source.Provider.Execute<IList<TSource>>(
  122. Expression.Call(
  123. null,
  124. ((MethodInfo)MethodInfo.GetCurrentMethod()).MakeGenericMethod(typeof(TSource), typeof(TKey)),
  125. source.Expression,
  126. keySelector,
  127. Expression.Constant(comparer, typeof(IComparer<TKey>))
  128. )
  129. );
  130. }
  131. /// <summary>
  132. /// Returns the elements with the minimum key value by using the specified comparer to compare key values.
  133. /// </summary>
  134. /// <typeparam name="TSource">Source sequence element type.</typeparam>
  135. /// <typeparam name="TKey">Key type.</typeparam>
  136. /// <param name="source">Source sequence.</param>
  137. /// <param name="keySelector">Key selector used to extract the key for each element in the sequence.</param>
  138. /// <param name="comparer">Comparer used to determine the minimum key value.</param>
  139. /// <returns>List with the elements that share the same minimum key value.</returns>
  140. public static IList<TSource> MinByWithTies<TSource, TKey>(this IQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector, IComparer<TKey> comparer)
  141. {
  142. if (source == null)
  143. throw new ArgumentNullException(nameof(source));
  144. if (keySelector == null)
  145. throw new ArgumentNullException(nameof(keySelector));
  146. if (comparer == null)
  147. throw new ArgumentNullException(nameof(comparer));
  148. return source.Provider.Execute<IList<TSource>>(
  149. Expression.Call(
  150. null,
  151. ((MethodInfo)MethodInfo.GetCurrentMethod()).MakeGenericMethod(typeof(TSource), typeof(TKey)),
  152. source.Expression,
  153. keySelector,
  154. Expression.Constant(comparer, typeof(IComparer<TKey>))
  155. )
  156. );
  157. }
  158. #pragma warning disable 1591
  159. [EditorBrowsable(EditorBrowsableState.Never)]
  160. public static IList<TSource> MinBy<TSource, TKey>(IEnumerable<TSource> source, Func<TSource, TKey> keySelector, IComparer<TKey> comparer)
  161. {
  162. #if REFERENCE_ASSEMBLY
  163. return default;
  164. #else
  165. return EnumerableEx.MinByWithTies(source, keySelector, comparer);
  166. #endif
  167. }
  168. [EditorBrowsable(EditorBrowsableState.Never)]
  169. public static IList<TSource> MinByWithTies<TSource, TKey>(IEnumerable<TSource> source, Func<TSource, TKey> keySelector, IComparer<TKey> comparer)
  170. {
  171. #if REFERENCE_ASSEMBLY
  172. return default;
  173. #else
  174. return EnumerableEx.MinByWithTies(source, keySelector, comparer);
  175. #endif
  176. }
  177. #pragma warning restore 1591
  178. /// <summary>
  179. /// Returns the maximum value in the enumerable sequence by using the specified comparer to compare values.
  180. /// </summary>
  181. /// <typeparam name="TSource">Source sequence element type.</typeparam>
  182. /// <param name="source">Source sequence.</param>
  183. /// <param name="comparer">Comparer used to determine the maximum value.</param>
  184. /// <returns>Maximum value in the sequence.</returns>
  185. public static TSource Max<TSource>(this IQueryable<TSource> source, IComparer<TSource> comparer)
  186. {
  187. if (source == null)
  188. throw new ArgumentNullException(nameof(source));
  189. if (comparer == null)
  190. throw new ArgumentNullException(nameof(comparer));
  191. return source.Provider.Execute<TSource>(
  192. Expression.Call(
  193. null,
  194. ((MethodInfo)MethodInfo.GetCurrentMethod()).MakeGenericMethod(typeof(TSource)),
  195. source.Expression,
  196. Expression.Constant(comparer, typeof(IComparer<TSource>))
  197. )
  198. );
  199. }
  200. #pragma warning disable 1591
  201. [EditorBrowsable(EditorBrowsableState.Never)]
  202. public static TSource Max<TSource>(IEnumerable<TSource> source, IComparer<TSource> comparer)
  203. {
  204. #if REFERENCE_ASSEMBLY
  205. return default;
  206. #else
  207. return EnumerableEx.Max(source, comparer);
  208. #endif
  209. }
  210. #pragma warning restore 1591
  211. /// <summary>
  212. /// Returns the elements with the maximum key value by using the default comparer to compare key values.
  213. /// </summary>
  214. /// <typeparam name="TSource">Source sequence element type.</typeparam>
  215. /// <typeparam name="TKey">Key type.</typeparam>
  216. /// <param name="source">Source sequence.</param>
  217. /// <param name="keySelector">Key selector used to extract the key for each element in the sequence.</param>
  218. /// <returns>List with the elements that share the same maximum key value.</returns>
  219. public static IList<TSource> MaxBy<TSource, TKey>(this IQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector)
  220. {
  221. if (source == null)
  222. throw new ArgumentNullException(nameof(source));
  223. if (keySelector == null)
  224. throw new ArgumentNullException(nameof(keySelector));
  225. return source.Provider.Execute<IList<TSource>>(
  226. Expression.Call(
  227. null,
  228. ((MethodInfo)MethodInfo.GetCurrentMethod()).MakeGenericMethod(typeof(TSource), typeof(TKey)),
  229. source.Expression,
  230. keySelector
  231. )
  232. );
  233. }
  234. /// <summary>
  235. /// Returns the elements with the maximum key value by using the default comparer to compare key values.
  236. /// </summary>
  237. /// <typeparam name="TSource">Source sequence element type.</typeparam>
  238. /// <typeparam name="TKey">Key type.</typeparam>
  239. /// <param name="source">Source sequence.</param>
  240. /// <param name="keySelector">Key selector used to extract the key for each element in the sequence.</param>
  241. /// <returns>List with the elements that share the same maximum key value.</returns>
  242. public static IList<TSource> MaxByWithTies<TSource, TKey>(this IQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector)
  243. {
  244. if (source == null)
  245. throw new ArgumentNullException(nameof(source));
  246. if (keySelector == null)
  247. throw new ArgumentNullException(nameof(keySelector));
  248. return source.Provider.Execute<IList<TSource>>(
  249. Expression.Call(
  250. null,
  251. ((MethodInfo)MethodInfo.GetCurrentMethod()).MakeGenericMethod(typeof(TSource), typeof(TKey)),
  252. source.Expression,
  253. keySelector
  254. )
  255. );
  256. }
  257. #pragma warning disable 1591
  258. [EditorBrowsable(EditorBrowsableState.Never)]
  259. public static IList<TSource> MaxBy<TSource, TKey>(IEnumerable<TSource> source, Func<TSource, TKey> keySelector)
  260. {
  261. #if REFERENCE_ASSEMBLY
  262. return default;
  263. #else
  264. return EnumerableEx.MaxByWithTies(source, keySelector);
  265. #endif
  266. }
  267. [EditorBrowsable(EditorBrowsableState.Never)]
  268. public static IList<TSource> MaxByWithTies<TSource, TKey>(IEnumerable<TSource> source, Func<TSource, TKey> keySelector)
  269. {
  270. #if REFERENCE_ASSEMBLY
  271. return default;
  272. #else
  273. return EnumerableEx.MaxByWithTies(source, keySelector);
  274. #endif
  275. }
  276. #pragma warning restore 1591
  277. /// <summary>
  278. /// Returns the elements with the minimum key value by using the specified comparer to compare key values.
  279. /// </summary>
  280. /// <typeparam name="TSource">Source sequence element type.</typeparam>
  281. /// <typeparam name="TKey">Key type.</typeparam>
  282. /// <param name="source">Source sequence.</param>
  283. /// <param name="keySelector">Key selector used to extract the key for each element in the sequence.</param>
  284. /// <param name="comparer">Comparer used to determine the maximum key value.</param>
  285. /// <returns>List with the elements that share the same maximum key value.</returns>
  286. public static IList<TSource> MaxBy<TSource, TKey>(this IQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector, IComparer<TKey> comparer)
  287. {
  288. if (source == null)
  289. throw new ArgumentNullException(nameof(source));
  290. if (keySelector == null)
  291. throw new ArgumentNullException(nameof(keySelector));
  292. if (comparer == null)
  293. throw new ArgumentNullException(nameof(comparer));
  294. return source.Provider.Execute<IList<TSource>>(
  295. Expression.Call(
  296. null,
  297. ((MethodInfo)MethodInfo.GetCurrentMethod()).MakeGenericMethod(typeof(TSource), typeof(TKey)),
  298. source.Expression,
  299. keySelector,
  300. Expression.Constant(comparer, typeof(IComparer<TKey>))
  301. )
  302. );
  303. }
  304. #pragma warning disable 1591
  305. [EditorBrowsable(EditorBrowsableState.Never)]
  306. public static IList<TSource> MaxBy<TSource, TKey>(IEnumerable<TSource> source, Func<TSource, TKey> keySelector, IComparer<TKey> comparer)
  307. {
  308. #if REFERENCE_ASSEMBLY
  309. return default;
  310. #else
  311. return EnumerableEx.MaxByWithTies(source, keySelector, comparer);
  312. #endif
  313. }
  314. #pragma warning restore 1591
  315. /// <summary>
  316. /// Shares the source sequence within a selector function where each enumerator can fetch the next element from the source sequence.
  317. /// </summary>
  318. /// <typeparam name="TSource">Source sequence element type.</typeparam>
  319. /// <typeparam name="TResult">Result sequence element type.</typeparam>
  320. /// <param name="source">Source sequence.</param>
  321. /// <param name="selector">Selector function with shared access to the source sequence for each enumerator.</param>
  322. /// <returns>Sequence resulting from applying the selector function to the shared view over the source sequence.</returns>
  323. public static IQueryable<TResult> Share<TSource, TResult>(this IQueryable<TSource> source, Expression<Func<IEnumerable<TSource>, IEnumerable<TResult>>> selector)
  324. {
  325. if (source == null)
  326. throw new ArgumentNullException(nameof(source));
  327. if (selector == null)
  328. throw new ArgumentNullException(nameof(selector));
  329. return source.Provider.CreateQuery<TResult>(
  330. Expression.Call(
  331. null,
  332. ((MethodInfo)MethodInfo.GetCurrentMethod()).MakeGenericMethod(typeof(TSource), typeof(TResult)),
  333. source.Expression,
  334. selector
  335. )
  336. );
  337. }
  338. #pragma warning disable 1591
  339. [EditorBrowsable(EditorBrowsableState.Never)]
  340. public static IEnumerable<TResult> Share<TSource, TResult>(IEnumerable<TSource> source, Func<IEnumerable<TSource>, IEnumerable<TResult>> selector)
  341. {
  342. return EnumerableEx.Share(source, selector);
  343. }
  344. #pragma warning restore 1591
  345. /// <summary>
  346. /// Publishes the source sequence within a selector function where each enumerator can obtain a view over a tail of the source sequence.
  347. /// </summary>
  348. /// <typeparam name="TSource">Source sequence element type.</typeparam>
  349. /// <typeparam name="TResult">Result sequence element type.</typeparam>
  350. /// <param name="source">Source sequence.</param>
  351. /// <param name="selector">Selector function with published access to the source sequence for each enumerator.</param>
  352. /// <returns>Sequence resulting from applying the selector function to the published view over the source sequence.</returns>
  353. public static IQueryable<TResult> Publish<TSource, TResult>(this IQueryable<TSource> source, Expression<Func<IEnumerable<TSource>, IEnumerable<TResult>>> selector)
  354. {
  355. if (source == null)
  356. throw new ArgumentNullException(nameof(source));
  357. if (selector == null)
  358. throw new ArgumentNullException(nameof(selector));
  359. return source.Provider.CreateQuery<TResult>(
  360. Expression.Call(
  361. null,
  362. ((MethodInfo)MethodInfo.GetCurrentMethod()).MakeGenericMethod(typeof(TSource), typeof(TResult)),
  363. source.Expression,
  364. selector
  365. )
  366. );
  367. }
  368. #pragma warning disable 1591
  369. [EditorBrowsable(EditorBrowsableState.Never)]
  370. public static IEnumerable<TResult> Publish<TSource, TResult>(IEnumerable<TSource> source, Func<IEnumerable<TSource>, IEnumerable<TResult>> selector)
  371. {
  372. return EnumerableEx.Publish(source, selector);
  373. }
  374. #pragma warning restore 1591
  375. /// <summary>
  376. /// Memoizes the source sequence within a selector function where each enumerator can get access to all of the sequence's elements without causing multiple enumerations over the source.
  377. /// </summary>
  378. /// <typeparam name="TSource">Source sequence element type.</typeparam>
  379. /// <typeparam name="TResult">Result sequence element type.</typeparam>
  380. /// <param name="source">Source sequence.</param>
  381. /// <param name="selector">Selector function with memoized access to the source sequence for each enumerator.</param>
  382. /// <returns>Sequence resulting from applying the selector function to the memoized view over the source sequence.</returns>
  383. public static IQueryable<TResult> Memoize<TSource, TResult>(this IQueryable<TSource> source, Expression<Func<IEnumerable<TSource>, IEnumerable<TResult>>> selector)
  384. {
  385. if (source == null)
  386. throw new ArgumentNullException(nameof(source));
  387. if (selector == null)
  388. throw new ArgumentNullException(nameof(selector));
  389. return source.Provider.CreateQuery<TResult>(
  390. Expression.Call(
  391. null,
  392. ((MethodInfo)MethodInfo.GetCurrentMethod()).MakeGenericMethod(typeof(TSource), typeof(TResult)),
  393. source.Expression,
  394. selector
  395. )
  396. );
  397. }
  398. #pragma warning disable 1591
  399. [EditorBrowsable(EditorBrowsableState.Never)]
  400. public static IEnumerable<TResult> Memoize<TSource, TResult>(IEnumerable<TSource> source, Func<IEnumerable<TSource>, IEnumerable<TResult>> selector)
  401. {
  402. return EnumerableEx.Memoize(source, selector);
  403. }
  404. #pragma warning restore 1591
  405. /// <summary>
  406. /// Memoizes the source sequence within a selector function where a specified number of enumerators can get access to all of the sequence's elements without causing multiple enumerations over the source.
  407. /// </summary>
  408. /// <typeparam name="TSource">Source sequence element type.</typeparam>
  409. /// <typeparam name="TResult">Result sequence element type.</typeparam>
  410. /// <param name="source">Source sequence.</param>
  411. /// <param name="readerCount">Number of enumerators that can access the underlying buffer. Once every enumerator has obtained an element from the buffer, the element is removed from the buffer.</param>
  412. /// <param name="selector">Selector function with memoized access to the source sequence for a specified number of enumerators.</param>
  413. /// <returns>Sequence resulting from applying the selector function to the memoized view over the source sequence.</returns>
  414. public static IQueryable<TResult> Memoize<TSource, TResult>(this IQueryable<TSource> source, int readerCount, Expression<Func<IEnumerable<TSource>, IEnumerable<TResult>>> selector)
  415. {
  416. if (source == null)
  417. throw new ArgumentNullException(nameof(source));
  418. if (selector == null)
  419. throw new ArgumentNullException(nameof(selector));
  420. return source.Provider.CreateQuery<TResult>(
  421. Expression.Call(
  422. null,
  423. ((MethodInfo)MethodInfo.GetCurrentMethod()).MakeGenericMethod(typeof(TSource), typeof(TResult)),
  424. source.Expression,
  425. Expression.Constant(readerCount, typeof(int)),
  426. selector
  427. )
  428. );
  429. }
  430. #pragma warning disable 1591
  431. [EditorBrowsable(EditorBrowsableState.Never)]
  432. public static IEnumerable<TResult> Memoize<TSource, TResult>(IEnumerable<TSource> source, int readerCount, Func<IEnumerable<TSource>, IEnumerable<TResult>> selector)
  433. {
  434. return EnumerableEx.Memoize(source, readerCount, selector);
  435. }
  436. #pragma warning restore 1591
  437. /// <summary>
  438. /// Creates an enumerable sequence based on an enumerator factory function.
  439. /// </summary>
  440. /// <typeparam name="TResult">Result sequence element type.</typeparam>
  441. /// <param name="provider">Query provider.</param>
  442. /// <param name="getEnumerator">Enumerator factory function.</param>
  443. /// <returns>Sequence that will invoke the enumerator factory upon a call to GetEnumerator.</returns>
  444. public static IQueryable<TResult> Create<TResult>(this IQueryProvider provider, Expression<Func<IEnumerator<TResult>>> getEnumerator)
  445. {
  446. if (provider == null)
  447. throw new ArgumentNullException(nameof(provider));
  448. if (getEnumerator == null)
  449. throw new ArgumentNullException(nameof(getEnumerator));
  450. return provider.CreateQuery<TResult>(
  451. Expression.Call(
  452. null,
  453. ((MethodInfo)MethodInfo.GetCurrentMethod()).MakeGenericMethod(typeof(TResult)),
  454. Expression.Constant(provider, typeof(IQueryProvider)),
  455. getEnumerator
  456. )
  457. );
  458. }
  459. #pragma warning disable 1591
  460. [EditorBrowsable(EditorBrowsableState.Never)]
  461. public static IEnumerable<TResult> Create<TResult>(Func<IEnumerator<TResult>> getEnumerator)
  462. {
  463. return EnumerableEx.Create(getEnumerator);
  464. }
  465. #pragma warning restore 1591
  466. /// <summary>
  467. /// Returns a sequence with a single element.
  468. /// </summary>
  469. /// <typeparam name="TResult">Result sequence element type.</typeparam>
  470. /// <param name="provider">Query provider.</param>
  471. /// <param name="value">Single element of the resulting sequence.</param>
  472. /// <returns>Sequence with a single element.</returns>
  473. public static IQueryable<TResult> Return<TResult>(this IQueryProvider provider, TResult value)
  474. {
  475. if (provider == null)
  476. throw new ArgumentNullException(nameof(provider));
  477. return provider.CreateQuery<TResult>(
  478. Expression.Call(
  479. null,
  480. ((MethodInfo)MethodInfo.GetCurrentMethod()).MakeGenericMethod(typeof(TResult)),
  481. Expression.Constant(provider, typeof(IQueryProvider)),
  482. Expression.Constant(value, typeof(TResult))
  483. )
  484. );
  485. }
  486. #pragma warning disable 1591
  487. [EditorBrowsable(EditorBrowsableState.Never)]
  488. public static /*!*/IQueryable<TResult> Return<TResult>(TResult value)
  489. {
  490. return EnumerableEx.Return(value).AsQueryable();
  491. }
  492. #pragma warning restore 1591
  493. /// <summary>
  494. /// Returns a sequence that throws an exception upon enumeration.
  495. /// </summary>
  496. /// <typeparam name="TResult">Result sequence element type.</typeparam>
  497. /// <param name="provider">Query provider.</param>
  498. /// <param name="exception">Exception to throw upon enumerating the resulting sequence.</param>
  499. /// <returns>Sequence that throws the specified exception upon enumeration.</returns>
  500. public static IQueryable<TResult> Throw<TResult>(this IQueryProvider provider, Exception exception)
  501. {
  502. if (provider == null)
  503. throw new ArgumentNullException(nameof(provider));
  504. if (exception == null)
  505. throw new ArgumentNullException(nameof(exception));
  506. return provider.CreateQuery<TResult>(
  507. Expression.Call(
  508. null,
  509. ((MethodInfo)MethodInfo.GetCurrentMethod()).MakeGenericMethod(typeof(TResult)),
  510. Expression.Constant(provider, typeof(IQueryProvider)),
  511. Expression.Constant(exception, typeof(Exception))
  512. )
  513. );
  514. }
  515. #pragma warning disable 1591
  516. [EditorBrowsable(EditorBrowsableState.Never)]
  517. public static /*!*/IQueryable<TResult> Throw<TResult>(Exception exception)
  518. {
  519. return EnumerableEx.Throw<TResult>(exception).AsQueryable();
  520. }
  521. #pragma warning restore 1591
  522. /// <summary>
  523. /// Creates an enumerable sequence based on an enumerable factory function.
  524. /// </summary>
  525. /// <typeparam name="TResult">Result sequence element type.</typeparam>
  526. /// <param name="provider">Query provider.</param>
  527. /// <param name="enumerableFactory">Enumerable factory function.</param>
  528. /// <returns>Sequence that will invoke the enumerable factory upon a call to GetEnumerator.</returns>
  529. public static IQueryable<TResult> Defer<TResult>(this IQueryProvider provider, Expression<Func<IEnumerable<TResult>>> enumerableFactory)
  530. {
  531. if (provider == null)
  532. throw new ArgumentNullException(nameof(provider));
  533. if (enumerableFactory == null)
  534. throw new ArgumentNullException(nameof(enumerableFactory));
  535. return provider.CreateQuery<TResult>(
  536. Expression.Call(
  537. null,
  538. ((MethodInfo)MethodInfo.GetCurrentMethod()).MakeGenericMethod(typeof(TResult)),
  539. Expression.Constant(provider, typeof(IQueryProvider)),
  540. enumerableFactory
  541. )
  542. );
  543. }
  544. #pragma warning disable 1591
  545. [EditorBrowsable(EditorBrowsableState.Never)]
  546. public static /*!*/IQueryable<TResult> Defer<TResult>(Func<IEnumerable<TResult>> enumerableFactory)
  547. {
  548. return EnumerableEx.Defer(enumerableFactory).AsQueryable();
  549. }
  550. #pragma warning restore 1591
  551. /// <summary>
  552. /// Generates a sequence by mimicking a for loop.
  553. /// </summary>
  554. /// <typeparam name="TState">State type.</typeparam>
  555. /// <typeparam name="TResult">Result sequence element type.</typeparam>
  556. /// <param name="provider">Query provider.</param>
  557. /// <param name="initialState">Initial state of the generator loop.</param>
  558. /// <param name="condition">Loop condition.</param>
  559. /// <param name="iterate">State update function to run after every iteration of the generator loop.</param>
  560. /// <param name="resultSelector">Result selector to compute resulting sequence elements.</param>
  561. /// <returns>Sequence obtained by running the generator loop, yielding computed elements.</returns>
  562. public static IQueryable<TResult> Generate<TState, TResult>(this IQueryProvider provider, TState initialState, Expression<Func<TState, bool>> condition, Expression<Func<TState, TState>> iterate, Expression<Func<TState, TResult>> resultSelector)
  563. {
  564. if (provider == null)
  565. throw new ArgumentNullException(nameof(provider));
  566. if (condition == null)
  567. throw new ArgumentNullException(nameof(condition));
  568. if (iterate == null)
  569. throw new ArgumentNullException(nameof(iterate));
  570. if (resultSelector == null)
  571. throw new ArgumentNullException(nameof(resultSelector));
  572. return provider.CreateQuery<TResult>(
  573. Expression.Call(
  574. null,
  575. ((MethodInfo)MethodInfo.GetCurrentMethod()).MakeGenericMethod(typeof(TState), typeof(TResult)),
  576. Expression.Constant(provider, typeof(IQueryProvider)),
  577. Expression.Constant(initialState),
  578. condition,
  579. iterate,
  580. resultSelector
  581. )
  582. );
  583. }
  584. #pragma warning disable 1591
  585. [EditorBrowsable(EditorBrowsableState.Never)]
  586. public static /*!*/IQueryable<TResult> Generate<TState, TResult>(TState initialState, Func<TState, bool> condition, Func<TState, TState> iterate, Func<TState, TResult> resultSelector)
  587. {
  588. return EnumerableEx.Generate(initialState, condition, iterate, resultSelector).AsQueryable();
  589. }
  590. #pragma warning restore 1591
  591. /// <summary>
  592. /// Generates a sequence that's dependent on a resource object whose lifetime is determined by the sequence usage duration.
  593. /// </summary>
  594. /// <typeparam name="TSource">Source element type.</typeparam>
  595. /// <typeparam name="TResource">Resource type.</typeparam>
  596. /// <param name="provider">Query provider.</param>
  597. /// <param name="resourceFactory">Resource factory function.</param>
  598. /// <param name="enumerableFactory">Enumerable factory function, having access to the obtained resource.</param>
  599. /// <returns>Sequence whose use controls the lifetime of the associated obtained resource.</returns>
  600. public static IQueryable<TSource> Using<TSource, TResource>(this IQueryProvider provider, Expression<Func<TResource>> resourceFactory, Expression<Func<TResource, IEnumerable<TSource>>> enumerableFactory) where TResource : IDisposable
  601. {
  602. if (provider == null)
  603. throw new ArgumentNullException(nameof(provider));
  604. if (resourceFactory == null)
  605. throw new ArgumentNullException(nameof(resourceFactory));
  606. if (enumerableFactory == null)
  607. throw new ArgumentNullException(nameof(enumerableFactory));
  608. return provider.CreateQuery<TSource>(
  609. Expression.Call(
  610. null,
  611. ((MethodInfo)MethodInfo.GetCurrentMethod()).MakeGenericMethod(typeof(TSource), typeof(TResource)),
  612. Expression.Constant(provider, typeof(IQueryProvider)),
  613. resourceFactory,
  614. enumerableFactory
  615. )
  616. );
  617. }
  618. #pragma warning disable 1591
  619. [EditorBrowsable(EditorBrowsableState.Never)]
  620. public static /*!*/IQueryable<TSource> Using<TSource, TResource>(Func<TResource> resourceFactory, Func<TResource, IEnumerable<TSource>> enumerableFactory) where TResource : IDisposable
  621. {
  622. return EnumerableEx.Using(resourceFactory, enumerableFactory).AsQueryable();
  623. }
  624. #pragma warning restore 1591
  625. /// <summary>
  626. /// Generates a sequence by repeating the given value infinitely.
  627. /// </summary>
  628. /// <typeparam name="TResult">Result sequence element type.</typeparam>
  629. /// <param name="provider">Query provider.</param>
  630. /// <param name="value">Value to repreat in the resulting sequence.</param>
  631. /// <returns>Sequence repeating the given value infinitely.</returns>
  632. public static IEnumerable<TResult> Repeat<TResult>(this IQueryProvider provider, TResult value)
  633. {
  634. if (provider == null)
  635. throw new ArgumentNullException(nameof(provider));
  636. return provider.CreateQuery<TResult>(
  637. Expression.Call(
  638. null,
  639. ((MethodInfo)MethodInfo.GetCurrentMethod()).MakeGenericMethod(typeof(TResult)),
  640. Expression.Constant(provider, typeof(IQueryProvider)),
  641. Expression.Constant(value, typeof(TResult))
  642. )
  643. );
  644. }
  645. #pragma warning disable 1591
  646. [EditorBrowsable(EditorBrowsableState.Never)]
  647. public static /*!*/IQueryable<TResult> Repeat<TResult>(TResult value)
  648. {
  649. return EnumerableEx.Repeat(value).AsQueryable();
  650. }
  651. #pragma warning restore 1591
  652. /// <summary>
  653. /// Creates a sequence that corresponds to the source sequence, concatenating it with the sequence resulting from calling an exception handler function in case of an error.
  654. /// </summary>
  655. /// <typeparam name="TSource">Source sequence element type.</typeparam>
  656. /// <typeparam name="TException">Exception type to catch.</typeparam>
  657. /// <param name="source">Source sequence.</param>
  658. /// <param name="handler">Handler to invoke when an exception of the specified type occurs.</param>
  659. /// <returns>Source sequence, concatenated with an exception handler result sequence in case of an error.</returns>
  660. public static IQueryable<TSource> Catch<TSource, TException>(this IQueryable<TSource> source, Expression<Func<TException, IEnumerable<TSource>>> handler)
  661. where TException : Exception
  662. {
  663. if (source == null)
  664. throw new ArgumentNullException(nameof(source));
  665. if (handler == null)
  666. throw new ArgumentNullException(nameof(handler));
  667. return source.Provider.CreateQuery<TSource>(
  668. Expression.Call(
  669. null,
  670. ((MethodInfo)MethodInfo.GetCurrentMethod()).MakeGenericMethod(typeof(TSource), typeof(TException)),
  671. source.Expression,
  672. handler
  673. )
  674. );
  675. }
  676. #pragma warning disable 1591
  677. [EditorBrowsable(EditorBrowsableState.Never)]
  678. public static IEnumerable<TSource> Catch<TSource, TException>(IEnumerable<TSource> source, Func<TException, IEnumerable<TSource>> handler)
  679. where TException : Exception
  680. {
  681. return EnumerableEx.Catch(source, handler);
  682. }
  683. #pragma warning restore 1591
  684. /// <summary>
  685. /// Creates a sequence by concatenating source sequences until a source sequence completes successfully.
  686. /// </summary>
  687. /// <typeparam name="TSource">Source sequence element type.</typeparam>
  688. /// <param name="sources">Source sequences.</param>
  689. /// <returns>Sequence that continues to concatenate source sequences while errors occur.</returns>
  690. public static IQueryable<TSource> Catch<TSource>(this IQueryable<IEnumerable<TSource>> sources)
  691. {
  692. if (sources == null)
  693. throw new ArgumentNullException(nameof(sources));
  694. return sources.Provider.CreateQuery<TSource>(
  695. Expression.Call(
  696. null,
  697. ((MethodInfo)MethodInfo.GetCurrentMethod()).MakeGenericMethod(typeof(TSource)),
  698. sources.Expression
  699. )
  700. );
  701. }
  702. #pragma warning disable 1591
  703. [EditorBrowsable(EditorBrowsableState.Never)]
  704. public static IEnumerable<TSource> Catch<TSource>(IEnumerable<IEnumerable<TSource>> sources)
  705. {
  706. return EnumerableEx.Catch(sources);
  707. }
  708. #pragma warning restore 1591
  709. /// <summary>
  710. /// Creates a sequence by concatenating source sequences until a source sequence completes successfully.
  711. /// </summary>
  712. /// <typeparam name="TSource">Source sequence element type.</typeparam>
  713. /// <param name="provider">Query provider.</param>
  714. /// <param name="sources">Source sequences.</param>
  715. /// <returns>Sequence that continues to concatenate source sequences while errors occur.</returns>
  716. public static IQueryable<TSource> Catch<TSource>(this IQueryProvider provider, params IEnumerable<TSource>[] sources)
  717. {
  718. if (provider == null)
  719. throw new ArgumentNullException(nameof(provider));
  720. if (sources == null)
  721. throw new ArgumentNullException(nameof(sources));
  722. return provider.CreateQuery<TSource>(
  723. Expression.Call(
  724. null,
  725. ((MethodInfo)MethodInfo.GetCurrentMethod()).MakeGenericMethod(typeof(TSource)),
  726. Expression.Constant(provider, typeof(IQueryProvider)),
  727. GetSourceExpression(sources)
  728. )
  729. );
  730. }
  731. #pragma warning disable 1591
  732. [EditorBrowsable(EditorBrowsableState.Never)]
  733. public static /*!*/IQueryable<TSource> Catch<TSource>(params IEnumerable<TSource>[] sources)
  734. {
  735. return EnumerableEx.Catch(sources).AsQueryable();
  736. }
  737. #pragma warning restore 1591
  738. /// <summary>
  739. /// Creates a sequence that returns the elements of the first sequence, switching to the second in case of an error.
  740. /// </summary>
  741. /// <typeparam name="TSource">Source sequence element type.</typeparam>
  742. /// <param name="first">First sequence.</param>
  743. /// <param name="second">Second sequence, concatenated to the result in case the first sequence completes exceptionally.</param>
  744. /// <returns>The first sequence, followed by the second sequence in case an error is produced.</returns>
  745. public static IQueryable<TSource> Catch<TSource>(this IQueryable<TSource> first, IEnumerable<TSource> second)
  746. {
  747. if (first == null)
  748. throw new ArgumentNullException(nameof(first));
  749. if (second == null)
  750. throw new ArgumentNullException(nameof(second));
  751. return first.Provider.CreateQuery<TSource>(
  752. Expression.Call(
  753. null,
  754. ((MethodInfo)MethodInfo.GetCurrentMethod()).MakeGenericMethod(typeof(TSource)),
  755. first.Expression,
  756. GetSourceExpression(second)
  757. )
  758. );
  759. }
  760. #pragma warning disable 1591
  761. [EditorBrowsable(EditorBrowsableState.Never)]
  762. public static IEnumerable<TSource> Catch<TSource>(IEnumerable<TSource> first, IEnumerable<TSource> second)
  763. {
  764. return EnumerableEx.Catch(first, second);
  765. }
  766. #pragma warning restore 1591
  767. /// <summary>
  768. /// Creates a sequence whose termination or disposal of an enumerator causes a finally action to be executed.
  769. /// </summary>
  770. /// <typeparam name="TSource">Source sequence element type.</typeparam>
  771. /// <param name="source">Source sequence.</param>
  772. /// <param name="finallyAction">Action to run upon termination of the sequence, or when an enumerator is disposed.</param>
  773. /// <returns>Source sequence with guarantees on the invocation of the finally action.</returns>
  774. public static IQueryable<TSource> Finally<TSource>(this IQueryable<TSource> source, Expression<Action> finallyAction)
  775. {
  776. if (source == null)
  777. throw new ArgumentNullException(nameof(source));
  778. if (finallyAction == null)
  779. throw new ArgumentNullException(nameof(finallyAction));
  780. return source.Provider.CreateQuery<TSource>(
  781. Expression.Call(
  782. null,
  783. ((MethodInfo)MethodInfo.GetCurrentMethod()).MakeGenericMethod(typeof(TSource)),
  784. source.Expression,
  785. finallyAction
  786. )
  787. );
  788. }
  789. #pragma warning disable 1591
  790. [EditorBrowsable(EditorBrowsableState.Never)]
  791. public static IEnumerable<TSource> Finally<TSource>(IEnumerable<TSource> source, Action finallyAction)
  792. {
  793. return EnumerableEx.Finally(source, finallyAction);
  794. }
  795. #pragma warning restore 1591
  796. /// <summary>
  797. /// Creates a sequence that concatenates both given sequences, regardless of whether an error occurs.
  798. /// </summary>
  799. /// <typeparam name="TSource">Source sequence element type.</typeparam>
  800. /// <param name="first">First sequence.</param>
  801. /// <param name="second">Second sequence.</param>
  802. /// <returns>Sequence concatenating the elements of both sequences, ignoring errors.</returns>
  803. public static IQueryable<TSource> OnErrorResumeNext<TSource>(this IQueryable<TSource> first, IEnumerable<TSource> second)
  804. {
  805. if (first == null)
  806. throw new ArgumentNullException(nameof(first));
  807. if (second == null)
  808. throw new ArgumentNullException(nameof(second));
  809. return first.Provider.CreateQuery<TSource>(
  810. Expression.Call(
  811. null,
  812. ((MethodInfo)MethodInfo.GetCurrentMethod()).MakeGenericMethod(typeof(TSource)),
  813. first.Expression,
  814. GetSourceExpression(second)
  815. )
  816. );
  817. }
  818. #pragma warning disable 1591
  819. [EditorBrowsable(EditorBrowsableState.Never)]
  820. public static IEnumerable<TSource> OnErrorResumeNext<TSource>(IEnumerable<TSource> first, IEnumerable<TSource> second)
  821. {
  822. return EnumerableEx.OnErrorResumeNext(first, second);
  823. }
  824. #pragma warning restore 1591
  825. /// <summary>
  826. /// Creates a sequence that concatenates the given sequences, regardless of whether an error occurs in any of the sequences.
  827. /// </summary>
  828. /// <typeparam name="TSource">Source sequence element type.</typeparam>
  829. /// <param name="provider">Query provider.</param>
  830. /// <param name="sources">Source sequences.</param>
  831. /// <returns>Sequence concatenating the elements of the given sequences, ignoring errors.</returns>
  832. public static IEnumerable<TSource> OnErrorResumeNext<TSource>(this IQueryProvider provider, params IEnumerable<TSource>[] sources)
  833. {
  834. if (provider == null)
  835. throw new ArgumentNullException(nameof(provider));
  836. if (sources == null)
  837. throw new ArgumentNullException(nameof(sources));
  838. return provider.CreateQuery<TSource>(
  839. Expression.Call(
  840. null,
  841. ((MethodInfo)MethodInfo.GetCurrentMethod()).MakeGenericMethod(typeof(TSource)),
  842. Expression.Constant(provider, typeof(IQueryProvider)),
  843. GetSourceExpression(sources)
  844. )
  845. );
  846. }
  847. #pragma warning disable 1591
  848. [EditorBrowsable(EditorBrowsableState.Never)]
  849. public static /*!*/IQueryable<TSource> OnErrorResumeNext<TSource>(params IEnumerable<TSource>[] sources)
  850. {
  851. return EnumerableEx.OnErrorResumeNext(sources).AsQueryable();
  852. }
  853. #pragma warning restore 1591
  854. /// <summary>
  855. /// Creates a sequence that concatenates the given sequences, regardless of whether an error occurs in any of the sequences.
  856. /// </summary>
  857. /// <typeparam name="TSource">Source sequence element type.</typeparam>
  858. /// <param name="sources">Source sequences.</param>
  859. /// <returns>Sequence concatenating the elements of the given sequences, ignoring errors.</returns>
  860. public static IQueryable<TSource> OnErrorResumeNext<TSource>(this IQueryable<IEnumerable<TSource>> sources)
  861. {
  862. if (sources == null)
  863. throw new ArgumentNullException(nameof(sources));
  864. return sources.Provider.CreateQuery<TSource>(
  865. Expression.Call(
  866. null,
  867. ((MethodInfo)MethodInfo.GetCurrentMethod()).MakeGenericMethod(typeof(TSource)),
  868. sources.Expression
  869. )
  870. );
  871. }
  872. #pragma warning disable 1591
  873. [EditorBrowsable(EditorBrowsableState.Never)]
  874. public static IEnumerable<TSource> OnErrorResumeNext<TSource>(IEnumerable<IEnumerable<TSource>> sources)
  875. {
  876. return EnumerableEx.OnErrorResumeNext(sources);
  877. }
  878. #pragma warning restore 1591
  879. /// <summary>
  880. /// Creates a sequence that retries enumerating the source sequence as long as an error occurs.
  881. /// </summary>
  882. /// <typeparam name="TSource">Source sequence element type.</typeparam>
  883. /// <param name="source">Source sequence.</param>
  884. /// <returns>Sequence concatenating the results of the source sequence as long as an error occurs.</returns>
  885. public static IQueryable<TSource> Retry<TSource>(this IQueryable<TSource> source)
  886. {
  887. if (source == null)
  888. throw new ArgumentNullException(nameof(source));
  889. return source.Provider.CreateQuery<TSource>(
  890. Expression.Call(
  891. null,
  892. ((MethodInfo)MethodInfo.GetCurrentMethod()).MakeGenericMethod(typeof(TSource)),
  893. source.Expression
  894. )
  895. );
  896. }
  897. #pragma warning disable 1591
  898. [EditorBrowsable(EditorBrowsableState.Never)]
  899. public static IEnumerable<TSource> Retry<TSource>(IEnumerable<TSource> source)
  900. {
  901. return EnumerableEx.Retry(source);
  902. }
  903. #pragma warning restore 1591
  904. /// <summary>
  905. /// Creates a sequence that retries enumerating the source sequence as long as an error occurs, with the specified maximum number of retries.
  906. /// </summary>
  907. /// <typeparam name="TSource">Source sequence element type.</typeparam>
  908. /// <param name="source">Source sequence.</param>
  909. /// <param name="retryCount">Maximum number of retries.</param>
  910. /// <returns>Sequence concatenating the results of the source sequence as long as an error occurs.</returns>
  911. public static IQueryable<TSource> Retry<TSource>(this IQueryable<TSource> source, int retryCount)
  912. {
  913. if (source == null)
  914. throw new ArgumentNullException(nameof(source));
  915. return source.Provider.CreateQuery<TSource>(
  916. Expression.Call(
  917. null,
  918. ((MethodInfo)MethodInfo.GetCurrentMethod()).MakeGenericMethod(typeof(TSource)),
  919. source.Expression,
  920. Expression.Constant(retryCount, typeof(int))
  921. )
  922. );
  923. }
  924. #pragma warning disable 1591
  925. [EditorBrowsable(EditorBrowsableState.Never)]
  926. public static IEnumerable<TSource> Retry<TSource>(IEnumerable<TSource> source, int retryCount)
  927. {
  928. return EnumerableEx.Retry(source, retryCount);
  929. }
  930. #pragma warning restore 1591
  931. /// <summary>
  932. /// Generates an enumerable sequence by repeating a source sequence as long as the given loop condition holds.
  933. /// </summary>
  934. /// <typeparam name="TResult">Result sequence element type.</typeparam>
  935. /// <param name="provider">Query provider.</param>
  936. /// <param name="condition">Loop condition.</param>
  937. /// <param name="source">Sequence to repeat while the condition evaluates true.</param>
  938. /// <returns>Sequence generated by repeating the given sequence while the condition evaluates to true.</returns>
  939. public static IQueryable<TResult> While<TResult>(this IQueryProvider provider, Expression<Func<bool>> condition, IEnumerable<TResult> source)
  940. {
  941. if (provider == null)
  942. throw new ArgumentNullException(nameof(provider));
  943. if (condition == null)
  944. throw new ArgumentNullException(nameof(condition));
  945. if (source == null)
  946. throw new ArgumentNullException(nameof(source));
  947. return provider.CreateQuery<TResult>(
  948. Expression.Call(
  949. null,
  950. ((MethodInfo)MethodInfo.GetCurrentMethod()).MakeGenericMethod(typeof(TResult)),
  951. Expression.Constant(provider, typeof(IQueryProvider)),
  952. condition,
  953. GetSourceExpression(source)
  954. )
  955. );
  956. }
  957. #pragma warning disable 1591
  958. [EditorBrowsable(EditorBrowsableState.Never)]
  959. public static /*!*/IQueryable<TResult> While<TResult>(Func<bool> condition, IEnumerable<TResult> source)
  960. {
  961. return EnumerableEx.While(condition, source).AsQueryable();
  962. }
  963. #pragma warning restore 1591
  964. /// <summary>
  965. /// Returns an enumerable sequence based on the evaluation result of the given condition.
  966. /// </summary>
  967. /// <typeparam name="TResult">Result sequence element type.</typeparam>
  968. /// <param name="provider">Query provider.</param>
  969. /// <param name="condition">Condition to evaluate.</param>
  970. /// <param name="thenSource">Sequence to return in case the condition evaluates true.</param>
  971. /// <param name="elseSource">Sequence to return in case the condition evaluates false.</param>
  972. /// <returns>Either of the two input sequences based on the result of evaluating the condition.</returns>
  973. public static IQueryable<TResult> If<TResult>(this IQueryProvider provider, Expression<Func<bool>> condition, IEnumerable<TResult> thenSource, IEnumerable<TResult> elseSource)
  974. {
  975. if (provider == null)
  976. throw new ArgumentNullException(nameof(provider));
  977. if (condition == null)
  978. throw new ArgumentNullException(nameof(condition));
  979. if (thenSource == null)
  980. throw new ArgumentNullException(nameof(thenSource));
  981. if (elseSource == null)
  982. throw new ArgumentNullException(nameof(elseSource));
  983. return provider.CreateQuery<TResult>(
  984. Expression.Call(
  985. null,
  986. ((MethodInfo)MethodInfo.GetCurrentMethod()).MakeGenericMethod(typeof(TResult)),
  987. Expression.Constant(provider, typeof(IQueryProvider)),
  988. condition,
  989. GetSourceExpression(thenSource),
  990. GetSourceExpression(elseSource)
  991. )
  992. );
  993. }
  994. #pragma warning disable 1591
  995. [EditorBrowsable(EditorBrowsableState.Never)]
  996. public static /*!*/IQueryable<TResult> If<TResult>(Func<bool> condition, IEnumerable<TResult> thenSource, IEnumerable<TResult> elseSource)
  997. {
  998. return EnumerableEx.If(condition, thenSource, elseSource).AsQueryable();
  999. }
  1000. #pragma warning restore 1591
  1001. /// <summary>
  1002. /// Returns an enumerable sequence if the evaluation result of the given condition is true, otherwise returns an empty sequence.
  1003. /// </summary>
  1004. /// <typeparam name="TResult">Result sequence element type.</typeparam>
  1005. /// <param name="provider">Query provider.</param>
  1006. /// <param name="condition">Condition to evaluate.</param>
  1007. /// <param name="thenSource">Sequence to return in case the condition evaluates true.</param>
  1008. /// <returns>The given input sequence if the condition evaluates true; otherwise, an empty sequence.</returns>
  1009. public static IQueryable<TResult> If<TResult>(this IQueryProvider provider, Expression<Func<bool>> condition, IEnumerable<TResult> thenSource)
  1010. {
  1011. if (provider == null)
  1012. throw new ArgumentNullException(nameof(provider));
  1013. if (condition == null)
  1014. throw new ArgumentNullException(nameof(condition));
  1015. if (thenSource == null)
  1016. throw new ArgumentNullException(nameof(thenSource));
  1017. return provider.CreateQuery<TResult>(
  1018. Expression.Call(
  1019. null,
  1020. ((MethodInfo)MethodInfo.GetCurrentMethod()).MakeGenericMethod(typeof(TResult)),
  1021. Expression.Constant(provider, typeof(IQueryProvider)),
  1022. condition,
  1023. GetSourceExpression(thenSource)
  1024. )
  1025. );
  1026. }
  1027. #pragma warning disable 1591
  1028. [EditorBrowsable(EditorBrowsableState.Never)]
  1029. public static /*!*/IQueryable<TResult> If<TResult>(Func<bool> condition, IEnumerable<TResult> thenSource)
  1030. {
  1031. return EnumerableEx.If(condition, thenSource).AsQueryable();
  1032. }
  1033. #pragma warning restore 1591
  1034. /// <summary>
  1035. /// Generates an enumerable sequence by repeating a source sequence as long as the given loop postcondition holds.
  1036. /// </summary>
  1037. /// <typeparam name="TResult">Result sequence element type.</typeparam>
  1038. /// <param name="source">Source sequence to repeat while the condition evaluates true.</param>
  1039. /// <param name="condition">Loop condition.</param>
  1040. /// <returns>Sequence generated by repeating the given sequence until the condition evaluates to false.</returns>
  1041. public static IQueryable<TResult> DoWhile<TResult>(this IQueryable<TResult> source, Expression<Func<bool>> condition)
  1042. {
  1043. if (source == null)
  1044. throw new ArgumentNullException(nameof(source));
  1045. if (condition == null)
  1046. throw new ArgumentNullException(nameof(condition));
  1047. return source.Provider.CreateQuery<TResult>(
  1048. Expression.Call(
  1049. null,
  1050. ((MethodInfo)MethodInfo.GetCurrentMethod()).MakeGenericMethod(typeof(TResult)),
  1051. source.Expression,
  1052. condition
  1053. )
  1054. );
  1055. }
  1056. #pragma warning disable 1591
  1057. [EditorBrowsable(EditorBrowsableState.Never)]
  1058. public static IEnumerable<TResult> DoWhile<TResult>(IEnumerable<TResult> source, Func<bool> condition)
  1059. {
  1060. return EnumerableEx.DoWhile(source, condition);
  1061. }
  1062. #pragma warning restore 1591
  1063. /// <summary>
  1064. /// Returns a sequence from a dictionary based on the result of evaluating a selector function.
  1065. /// </summary>
  1066. /// <typeparam name="TValue">Type of the selector value.</typeparam>
  1067. /// <typeparam name="TResult">Result sequence element type.</typeparam>
  1068. /// <param name="provider">Query provider.</param>
  1069. /// <param name="selector">Selector function used to pick a sequence from the given sources.</param>
  1070. /// <param name="sources">Dictionary mapping selector values onto resulting sequences.</param>
  1071. /// <returns>The source sequence corresponding with the evaluated selector value; otherwise, an empty sequence.</returns>
  1072. public static IQueryable<TResult> Case<TValue, TResult>(this IQueryProvider provider, Expression<Func<TValue>> selector, IDictionary<TValue, IEnumerable<TResult>> sources)
  1073. {
  1074. if (provider == null)
  1075. throw new ArgumentNullException(nameof(provider));
  1076. if (selector == null)
  1077. throw new ArgumentNullException(nameof(selector));
  1078. if (sources == null)
  1079. throw new ArgumentNullException(nameof(sources));
  1080. return provider.CreateQuery<TResult>(
  1081. Expression.Call(
  1082. null,
  1083. ((MethodInfo)MethodInfo.GetCurrentMethod()).MakeGenericMethod(typeof(TValue), typeof(TResult)),
  1084. selector,
  1085. Expression.Constant(sources, typeof(IDictionary<TValue, IEnumerable<TResult>>))
  1086. )
  1087. );
  1088. }
  1089. #pragma warning disable 1591
  1090. [EditorBrowsable(EditorBrowsableState.Never)]
  1091. public static /*!*/IQueryable<TResult> Case<TValue, TResult>(Func<TValue> selector, IDictionary<TValue, IEnumerable<TResult>> sources)
  1092. {
  1093. return EnumerableEx.Case(selector, sources).AsQueryable();
  1094. }
  1095. #pragma warning restore 1591
  1096. /// <summary>
  1097. /// Returns a sequence from a dictionary based on the result of evaluating a selector function, also specifying a default sequence.
  1098. /// </summary>
  1099. /// <typeparam name="TValue">Type of the selector value.</typeparam>
  1100. /// <typeparam name="TResult">Result sequence element type.</typeparam>
  1101. /// <param name="provider">Query provider.</param>
  1102. /// <param name="selector">Selector function used to pick a sequence from the given sources.</param>
  1103. /// <param name="sources">Dictionary mapping selector values onto resulting sequences.</param>
  1104. /// <param name="defaultSource">Default sequence to return in case there's no corresponding source for the computed selector value.</param>
  1105. /// <returns>The source sequence corresponding with the evaluated selector value; otherwise, the default source.</returns>
  1106. public static IQueryable<TResult> Case<TValue, TResult>(this IQueryProvider provider, Expression<Func<TValue>> selector, IDictionary<TValue, IEnumerable<TResult>> sources, IEnumerable<TResult> defaultSource)
  1107. {
  1108. if (provider == null)
  1109. throw new ArgumentNullException(nameof(provider));
  1110. if (selector == null)
  1111. throw new ArgumentNullException(nameof(selector));
  1112. if (sources == null)
  1113. throw new ArgumentNullException(nameof(sources));
  1114. if (defaultSource == null)
  1115. throw new ArgumentNullException(nameof(defaultSource));
  1116. return provider.CreateQuery<TResult>(
  1117. Expression.Call(
  1118. null,
  1119. ((MethodInfo)MethodInfo.GetCurrentMethod()).MakeGenericMethod(typeof(TValue), typeof(TResult)),
  1120. selector,
  1121. Expression.Constant(sources, typeof(IDictionary<TValue, IEnumerable<TResult>>))
  1122. )
  1123. );
  1124. }
  1125. #pragma warning disable 1591
  1126. [EditorBrowsable(EditorBrowsableState.Never)]
  1127. public static /*!*/IQueryable<TResult> Case<TValue, TResult>(Func<TValue> selector, IDictionary<TValue, IEnumerable<TResult>> sources, IEnumerable<TResult> defaultSource)
  1128. {
  1129. return EnumerableEx.Case(selector, sources, defaultSource).AsQueryable();
  1130. }
  1131. #pragma warning restore 1591
  1132. /// <summary>
  1133. /// Generates a sequence by enumerating a source sequence, mapping its elements on result sequences, and concatenating those sequences.
  1134. /// </summary>
  1135. /// <typeparam name="TSource">Source sequence element type.</typeparam>
  1136. /// <typeparam name="TResult">Result sequence element type.</typeparam>
  1137. /// <param name="provider">Query provider.</param>
  1138. /// <param name="source">Source sequence.</param>
  1139. /// <param name="resultSelector">Result selector to evaluate for each iteration over the source.</param>
  1140. /// <returns>Sequence concatenating the inner sequences that result from evaluating the result selector on elements from the source.</returns>
  1141. public static IQueryable<TResult> For<TSource, TResult>(this IQueryProvider provider, IEnumerable<TSource> source, Expression<Func<TSource, IEnumerable<TResult>>> resultSelector)
  1142. {
  1143. if (provider == null)
  1144. throw new ArgumentNullException(nameof(provider));
  1145. if (source == null)
  1146. throw new ArgumentNullException(nameof(source));
  1147. if (resultSelector == null)
  1148. throw new ArgumentNullException(nameof(resultSelector));
  1149. return provider.CreateQuery<TResult>(
  1150. Expression.Call(
  1151. null,
  1152. ((MethodInfo)MethodInfo.GetCurrentMethod()).MakeGenericMethod(typeof(TSource), typeof(TResult)),
  1153. GetSourceExpression(source),
  1154. resultSelector
  1155. )
  1156. );
  1157. }
  1158. #pragma warning disable 1591
  1159. [EditorBrowsable(EditorBrowsableState.Never)]
  1160. public static /*!*/IQueryable<TResult> For<TSource, TResult>(IEnumerable<TSource> source, Func<TSource, IEnumerable<TResult>> resultSelector)
  1161. {
  1162. return EnumerableEx.For(source, resultSelector).AsQueryable();
  1163. }
  1164. #pragma warning restore 1591
  1165. /// <summary>
  1166. /// Concatenates the input sequences.
  1167. /// </summary>
  1168. /// <typeparam name="TSource">Source sequence element type.</typeparam>
  1169. /// <param name="sources">Source sequences.</param>
  1170. /// <returns>Sequence with the elements of the source sequences concatenated.</returns>
  1171. public static IQueryable<TSource> Concat<TSource>(this IQueryable<IEnumerable<TSource>> sources)
  1172. {
  1173. if (sources == null)
  1174. throw new ArgumentNullException(nameof(sources));
  1175. return sources.Provider.CreateQuery<TSource>(
  1176. Expression.Call(
  1177. null,
  1178. ((MethodInfo)MethodInfo.GetCurrentMethod()).MakeGenericMethod(typeof(TSource)),
  1179. GetSourceExpression(sources)
  1180. )
  1181. );
  1182. }
  1183. #pragma warning disable 1591
  1184. [EditorBrowsable(EditorBrowsableState.Never)]
  1185. public static IEnumerable<TSource> Concat<TSource>(IEnumerable<IEnumerable<TSource>> sources)
  1186. {
  1187. return EnumerableEx.Concat(sources);
  1188. }
  1189. #pragma warning restore 1591
  1190. /// <summary>
  1191. /// Concatenates the input sequences.
  1192. /// </summary>
  1193. /// <typeparam name="TSource">Source sequence element type.</typeparam>
  1194. /// <param name="provider">Query provider.</param>
  1195. /// <param name="sources">Source sequences.</param>
  1196. /// <returns>Sequence with the elements of the source sequences concatenated.</returns>
  1197. public static IQueryable<TSource> Concat<TSource>(this IQueryProvider provider, params IEnumerable<TSource>[] sources)
  1198. {
  1199. if (provider == null)
  1200. throw new ArgumentNullException(nameof(provider));
  1201. if (sources == null)
  1202. throw new ArgumentNullException(nameof(sources));
  1203. return provider.CreateQuery<TSource>(
  1204. Expression.Call(
  1205. null,
  1206. ((MethodInfo)MethodInfo.GetCurrentMethod()).MakeGenericMethod(typeof(TSource)),
  1207. Expression.Constant(provider, typeof(IQueryProvider)),
  1208. GetSourceExpression(sources)
  1209. )
  1210. );
  1211. }
  1212. #pragma warning disable 1591
  1213. [EditorBrowsable(EditorBrowsableState.Never)]
  1214. public static /*!*/IQueryable<TSource> Concat<TSource>(params IEnumerable<TSource>[] sources)
  1215. {
  1216. return EnumerableEx.Concat(sources).AsQueryable();
  1217. }
  1218. #pragma warning restore 1591
  1219. /// <summary>
  1220. /// Projects each element of a sequence to an given sequence and flattens the resulting sequences into one sequence.
  1221. /// </summary>
  1222. /// <typeparam name="TSource">First source sequence element type.</typeparam>
  1223. /// <typeparam name="TOther">Second source sequence element type.</typeparam>
  1224. /// <param name="source">A sequence of values to project.</param>
  1225. /// <param name="other">Inner sequence each source sequenec element is projected onto.</param>
  1226. /// <returns>Sequence flattening the sequences that result from projecting elements in the source sequence.</returns>
  1227. public static IQueryable<TOther> SelectMany<TSource, TOther>(this IQueryable<TSource> source, IEnumerable<TOther> other)
  1228. {
  1229. if (source == null)
  1230. throw new ArgumentNullException(nameof(source));
  1231. if (other == null)
  1232. throw new ArgumentNullException(nameof(other));
  1233. return source.Provider.CreateQuery<TOther>(
  1234. Expression.Call(
  1235. null,
  1236. ((MethodInfo)MethodInfo.GetCurrentMethod()).MakeGenericMethod(typeof(TSource), typeof(TOther)),
  1237. source.Expression,
  1238. GetSourceExpression(other)
  1239. )
  1240. );
  1241. }
  1242. #pragma warning disable 1591
  1243. [EditorBrowsable(EditorBrowsableState.Never)]
  1244. public static IEnumerable<TOther> SelectMany<TSource, TOther>(IEnumerable<TSource> source, IEnumerable<TOther> other)
  1245. {
  1246. return EnumerableEx.SelectMany(source, other);
  1247. }
  1248. #pragma warning restore 1591
  1249. /// <summary>
  1250. /// Hides the enumerable sequence object identity.
  1251. /// </summary>
  1252. /// <typeparam name="TSource">Source sequence element type.</typeparam>
  1253. /// <param name="source">Source sequence.</param>
  1254. /// <returns>Enumerable sequence with the same behavior as the original, but hiding the source object identity.</returns>
  1255. /// <remarks>AsQueryable doesn't hide the object identity, and simply acts as a cast to the IQueryable&lt;TSource&gt; interface.</remarks>
  1256. public static IQueryable<TSource> Hide<TSource>(this IQueryable<TSource> source)
  1257. {
  1258. if (source == null)
  1259. throw new ArgumentNullException(nameof(source));
  1260. return source.Provider.CreateQuery<TSource>(
  1261. Expression.Call(
  1262. null,
  1263. ((MethodInfo)MethodInfo.GetCurrentMethod()).MakeGenericMethod(typeof(TSource)),
  1264. source.Expression
  1265. )
  1266. );
  1267. }
  1268. #pragma warning disable 1591
  1269. [EditorBrowsable(EditorBrowsableState.Never)]
  1270. public static IEnumerable<TSource> Hide<TSource>(IEnumerable<TSource> source)
  1271. {
  1272. return EnumerableEx.Hide(source);
  1273. }
  1274. #pragma warning restore 1591
  1275. /// <summary>
  1276. /// Lazily invokes an action for each value in the sequence.
  1277. /// </summary>
  1278. /// <typeparam name="TSource">Source sequence element type.</typeparam>
  1279. /// <param name="source">Source sequence.</param>
  1280. /// <param name="onNext">Action to invoke for each element.</param>
  1281. /// <returns>Sequence exhibiting the specified side-effects upon enumeration.</returns>
  1282. public static IQueryable<TSource> Do<TSource>(this IQueryable<TSource> source, Expression<Action<TSource>> onNext)
  1283. {
  1284. if (source == null)
  1285. throw new ArgumentNullException(nameof(source));
  1286. if (onNext == null)
  1287. throw new ArgumentNullException(nameof(onNext));
  1288. return source.Provider.CreateQuery<TSource>(
  1289. Expression.Call(
  1290. null,
  1291. ((MethodInfo)MethodInfo.GetCurrentMethod()).MakeGenericMethod(typeof(TSource)),
  1292. source.Expression,
  1293. onNext
  1294. )
  1295. );
  1296. }
  1297. #pragma warning disable 1591
  1298. [EditorBrowsable(EditorBrowsableState.Never)]
  1299. public static IEnumerable<TSource> Do<TSource>(IEnumerable<TSource> source, Action<TSource> onNext)
  1300. {
  1301. return EnumerableEx.Do(source, onNext);
  1302. }
  1303. #pragma warning restore 1591
  1304. /// <summary>
  1305. /// Lazily invokes an action for each value in the sequence, and executes an action for successful termination.
  1306. /// </summary>
  1307. /// <typeparam name="TSource">Source sequence element type.</typeparam>
  1308. /// <param name="source">Source sequence.</param>
  1309. /// <param name="onNext">Action to invoke for each element.</param>
  1310. /// <param name="onCompleted">Action to invoke on successful termination of the sequence.</param>
  1311. /// <returns>Sequence exhibiting the specified side-effects upon enumeration.</returns>
  1312. public static IQueryable<TSource> Do<TSource>(this IQueryable<TSource> source, Expression<Action<TSource>> onNext, Expression<Action> onCompleted)
  1313. {
  1314. if (source == null)
  1315. throw new ArgumentNullException(nameof(source));
  1316. if (onNext == null)
  1317. throw new ArgumentNullException(nameof(onNext));
  1318. if (onCompleted == null)
  1319. throw new ArgumentNullException(nameof(onCompleted));
  1320. return source.Provider.CreateQuery<TSource>(
  1321. Expression.Call(
  1322. null,
  1323. ((MethodInfo)MethodInfo.GetCurrentMethod()).MakeGenericMethod(typeof(TSource)),
  1324. source.Expression,
  1325. onNext,
  1326. onCompleted
  1327. )
  1328. );
  1329. }
  1330. #pragma warning disable 1591
  1331. [EditorBrowsable(EditorBrowsableState.Never)]
  1332. public static IEnumerable<TSource> Do<TSource>(IEnumerable<TSource> source, Action<TSource> onNext, Action onCompleted)
  1333. {
  1334. return EnumerableEx.Do(source, onNext, onCompleted);
  1335. }
  1336. #pragma warning restore 1591
  1337. /// <summary>
  1338. /// Lazily invokes an action for each value in the sequence, and executes an action upon exceptional termination.
  1339. /// </summary>
  1340. /// <typeparam name="TSource">Source sequence element type.</typeparam>
  1341. /// <param name="source">Source sequence.</param>
  1342. /// <param name="onNext">Action to invoke for each element.</param>
  1343. /// <param name="onError">Action to invoke on exceptional termination of the sequence.</param>
  1344. /// <returns>Sequence exhibiting the specified side-effects upon enumeration.</returns>
  1345. public static IQueryable<TSource> Do<TSource>(this IQueryable<TSource> source, Expression<Action<TSource>> onNext, Expression<Action<Exception>> onError)
  1346. {
  1347. if (source == null)
  1348. throw new ArgumentNullException(nameof(source));
  1349. if (onNext == null)
  1350. throw new ArgumentNullException(nameof(onNext));
  1351. if (onError == null)
  1352. throw new ArgumentNullException(nameof(onError));
  1353. return source.Provider.CreateQuery<TSource>(
  1354. Expression.Call(
  1355. null,
  1356. ((MethodInfo)MethodInfo.GetCurrentMethod()).MakeGenericMethod(typeof(TSource)),
  1357. source.Expression,
  1358. onNext,
  1359. onError
  1360. )
  1361. );
  1362. }
  1363. #pragma warning disable 1591
  1364. [EditorBrowsable(EditorBrowsableState.Never)]
  1365. public static IEnumerable<TSource> Do<TSource>(IEnumerable<TSource> source, Action<TSource> onNext, Action<Exception> onError)
  1366. {
  1367. return EnumerableEx.Do(source, onNext, onError);
  1368. }
  1369. #pragma warning restore 1591
  1370. /// <summary>
  1371. /// Lazily invokes an action for each value in the sequence, and executes an action upon successful or exceptional termination.
  1372. /// </summary>
  1373. /// <typeparam name="TSource">Source sequence element type.</typeparam>
  1374. /// <param name="source">Source sequence.</param>
  1375. /// <param name="onNext">Action to invoke for each element.</param>
  1376. /// <param name="onError">Action to invoke on exceptional termination of the sequence.</param>
  1377. /// <param name="onCompleted">Action to invoke on successful termination of the sequence.</param>
  1378. /// <returns>Sequence exhibiting the specified side-effects upon enumeration.</returns>
  1379. public static IQueryable<TSource> Do<TSource>(this IQueryable<TSource> source, Expression<Action<TSource>> onNext, Expression<Action<Exception>> onError, Expression<Action> onCompleted)
  1380. {
  1381. if (source == null)
  1382. throw new ArgumentNullException(nameof(source));
  1383. if (onNext == null)
  1384. throw new ArgumentNullException(nameof(onNext));
  1385. if (onError == null)
  1386. throw new ArgumentNullException(nameof(onError));
  1387. if (onCompleted == null)
  1388. throw new ArgumentNullException(nameof(onCompleted));
  1389. return source.Provider.CreateQuery<TSource>(
  1390. Expression.Call(
  1391. null,
  1392. ((MethodInfo)MethodInfo.GetCurrentMethod()).MakeGenericMethod(typeof(TSource)),
  1393. source.Expression,
  1394. onNext,
  1395. onError,
  1396. onCompleted
  1397. )
  1398. );
  1399. }
  1400. #pragma warning disable 1591
  1401. [EditorBrowsable(EditorBrowsableState.Never)]
  1402. public static IEnumerable<TSource> Do<TSource>(IEnumerable<TSource> source, Action<TSource> onNext, Action<Exception> onError, Action onCompleted)
  1403. {
  1404. return EnumerableEx.Do(source, onNext, onError, onCompleted);
  1405. }
  1406. #pragma warning restore 1591
  1407. /// <summary>
  1408. /// Lazily invokes observer methods for each value in the sequence, and upon successful or exceptional termination.
  1409. /// </summary>
  1410. /// <typeparam name="TSource">Source sequence element type.</typeparam>
  1411. /// <param name="source">Source sequence.</param>
  1412. /// <param name="observer">Observer to invoke notification calls on.</param>
  1413. /// <returns>Sequence exhibiting the side-effects of observer method invocation upon enumeration.</returns>
  1414. public static IQueryable<TSource> Do<TSource>(this IQueryable<TSource> source, IObserver<TSource> observer)
  1415. {
  1416. if (source == null)
  1417. throw new ArgumentNullException(nameof(source));
  1418. if (observer == null)
  1419. throw new ArgumentNullException(nameof(observer));
  1420. return source.Provider.CreateQuery<TSource>(
  1421. Expression.Call(
  1422. null,
  1423. ((MethodInfo)MethodInfo.GetCurrentMethod()).MakeGenericMethod(typeof(TSource)),
  1424. source.Expression,
  1425. Expression.Constant(observer, typeof(IObserver<TSource>))
  1426. )
  1427. );
  1428. }
  1429. #pragma warning disable 1591
  1430. [EditorBrowsable(EditorBrowsableState.Never)]
  1431. public static IEnumerable<TSource> Do<TSource>(IEnumerable<TSource> source, IObserver<TSource> observer)
  1432. {
  1433. return EnumerableEx.Do(source, observer);
  1434. }
  1435. #pragma warning restore 1591
  1436. /// <summary>
  1437. /// Generates a sequence of non-overlapping adjacent buffers over the source sequence.
  1438. /// </summary>
  1439. /// <typeparam name="TSource">Source sequence element type.</typeparam>
  1440. /// <param name="source">Source sequence.</param>
  1441. /// <param name="count">Number of elements for allocated buffers.</param>
  1442. /// <returns>Sequence of buffers containing source sequence elements.</returns>
  1443. public static IQueryable<IList<TSource>> Buffer<TSource>(this IQueryable<TSource> source, int count)
  1444. {
  1445. if (source == null)
  1446. throw new ArgumentNullException(nameof(source));
  1447. return source.Provider.CreateQuery<IList<TSource>>(
  1448. Expression.Call(
  1449. null,
  1450. ((MethodInfo)MethodInfo.GetCurrentMethod()).MakeGenericMethod(typeof(TSource)),
  1451. source.Expression,
  1452. Expression.Constant(count, typeof(int))
  1453. )
  1454. );
  1455. }
  1456. #pragma warning disable 1591
  1457. [EditorBrowsable(EditorBrowsableState.Never)]
  1458. public static IEnumerable<IList<TSource>> Buffer<TSource>(IEnumerable<TSource> source, int count)
  1459. {
  1460. return EnumerableEx.Buffer(source, count);
  1461. }
  1462. #pragma warning restore 1591
  1463. /// <summary>
  1464. /// Generates a sequence of buffers over the source sequence, with specified length and possible overlap.
  1465. /// </summary>
  1466. /// <typeparam name="TSource">Source sequence element type.</typeparam>
  1467. /// <param name="source">Source sequence.</param>
  1468. /// <param name="count">Number of elements for allocated buffers.</param>
  1469. /// <param name="skip">Number of elements to skip between the start of consecutive buffers.</param>
  1470. /// <returns>Sequence of buffers containing source sequence elements.</returns>
  1471. public static IQueryable<IList<TSource>> Buffer<TSource>(this IQueryable<TSource> source, int count, int skip)
  1472. {
  1473. if (source == null)
  1474. throw new ArgumentNullException(nameof(source));
  1475. return source.Provider.CreateQuery<IList<TSource>>(
  1476. Expression.Call(
  1477. null,
  1478. ((MethodInfo)MethodInfo.GetCurrentMethod()).MakeGenericMethod(typeof(TSource)),
  1479. source.Expression,
  1480. Expression.Constant(count, typeof(int)),
  1481. Expression.Constant(skip, typeof(int))
  1482. )
  1483. );
  1484. }
  1485. #pragma warning disable 1591
  1486. [EditorBrowsable(EditorBrowsableState.Never)]
  1487. public static IEnumerable<IList<TSource>> Buffer<TSource>(IEnumerable<TSource> source, int count, int skip)
  1488. {
  1489. return EnumerableEx.Buffer(source, count, skip);
  1490. }
  1491. #pragma warning restore 1591
  1492. /// <summary>
  1493. /// Ignores all elements in the source sequence.
  1494. /// </summary>
  1495. /// <typeparam name="TSource">Source sequence element type.</typeparam>
  1496. /// <param name="source">Source sequence.</param>
  1497. /// <returns>Source sequence without its elements.</returns>
  1498. public static IQueryable<TSource> IgnoreElements<TSource>(this IQueryable<TSource> source)
  1499. {
  1500. if (source == null)
  1501. throw new ArgumentNullException(nameof(source));
  1502. return source.Provider.CreateQuery<TSource>(
  1503. Expression.Call(
  1504. null,
  1505. ((MethodInfo)MethodInfo.GetCurrentMethod()).MakeGenericMethod(typeof(TSource)),
  1506. source.Expression
  1507. )
  1508. );
  1509. }
  1510. #pragma warning disable 1591
  1511. [EditorBrowsable(EditorBrowsableState.Never)]
  1512. public static IEnumerable<TSource> IgnoreElements<TSource>(IEnumerable<TSource> source)
  1513. {
  1514. return EnumerableEx.IgnoreElements(source);
  1515. }
  1516. #pragma warning restore 1591
  1517. /// <summary>
  1518. /// Returns elements with a distinct key value by using the default equality comparer to compare key values.
  1519. /// </summary>
  1520. /// <typeparam name="TSource">Source sequence element type.</typeparam>
  1521. /// <typeparam name="TKey">Key type.</typeparam>
  1522. /// <param name="source">Source sequence.</param>
  1523. /// <param name="keySelector">Key selector.</param>
  1524. /// <returns>Sequence that contains the elements from the source sequence with distinct key values.</returns>
  1525. public static IQueryable<TSource> Distinct<TSource, TKey>(this IQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector)
  1526. {
  1527. if (source == null)
  1528. throw new ArgumentNullException(nameof(source));
  1529. if (keySelector == null)
  1530. throw new ArgumentNullException(nameof(keySelector));
  1531. return source.Provider.CreateQuery<TSource>(
  1532. Expression.Call(
  1533. null,
  1534. ((MethodInfo)MethodInfo.GetCurrentMethod()).MakeGenericMethod(typeof(TSource), typeof(TKey)),
  1535. source.Expression,
  1536. keySelector
  1537. )
  1538. );
  1539. }
  1540. #pragma warning disable 1591
  1541. [EditorBrowsable(EditorBrowsableState.Never)]
  1542. public static IEnumerable<TSource> Distinct<TSource, TKey>(IEnumerable<TSource> source, Func<TSource, TKey> keySelector)
  1543. {
  1544. return EnumerableEx.Distinct(source, keySelector);
  1545. }
  1546. #pragma warning restore 1591
  1547. /// <summary>
  1548. /// Returns elements with a distinct key value by using the specified equality comparer to compare key values.
  1549. /// </summary>
  1550. /// <typeparam name="TSource">Source sequence element type.</typeparam>
  1551. /// <typeparam name="TKey">Key type.</typeparam>
  1552. /// <param name="source">Source sequence.</param>
  1553. /// <param name="keySelector">Key selector.</param>
  1554. /// <param name="comparer">Comparer used to compare key values.</param>
  1555. /// <returns>Sequence that contains the elements from the source sequence with distinct key values.</returns>
  1556. public static IQueryable<TSource> Distinct<TSource, TKey>(this IQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector, IEqualityComparer<TKey> comparer)
  1557. {
  1558. if (source == null)
  1559. throw new ArgumentNullException(nameof(source));
  1560. if (keySelector == null)
  1561. throw new ArgumentNullException(nameof(keySelector));
  1562. if (comparer == null)
  1563. throw new ArgumentNullException(nameof(comparer));
  1564. return source.Provider.CreateQuery<TSource>(
  1565. Expression.Call(
  1566. null,
  1567. ((MethodInfo)MethodInfo.GetCurrentMethod()).MakeGenericMethod(typeof(TSource), typeof(TKey)),
  1568. source.Expression,
  1569. keySelector,
  1570. Expression.Constant(comparer, typeof(IEqualityComparer<TKey>))
  1571. )
  1572. );
  1573. }
  1574. #pragma warning disable 1591
  1575. [EditorBrowsable(EditorBrowsableState.Never)]
  1576. public static IEnumerable<TSource> Distinct<TSource, TKey>(IEnumerable<TSource> source, Func<TSource, TKey> keySelector, IEqualityComparer<TKey> comparer)
  1577. {
  1578. return EnumerableEx.Distinct(source, keySelector, comparer);
  1579. }
  1580. #pragma warning restore 1591
  1581. /// <summary>
  1582. /// Returns consecutive distinct elements by using the default equality comparer to compare values.
  1583. /// </summary>
  1584. /// <typeparam name="TSource">Source sequence element type.</typeparam>
  1585. /// <param name="source">Source sequence.</param>
  1586. /// <returns>Sequence without adjacent non-distinct elements.</returns>
  1587. public static IQueryable<TSource> DistinctUntilChanged<TSource>(this IQueryable<TSource> source)
  1588. {
  1589. if (source == null)
  1590. throw new ArgumentNullException(nameof(source));
  1591. return source.Provider.CreateQuery<TSource>(
  1592. Expression.Call(
  1593. null,
  1594. ((MethodInfo)MethodInfo.GetCurrentMethod()).MakeGenericMethod(typeof(TSource)),
  1595. source.Expression
  1596. )
  1597. );
  1598. }
  1599. #pragma warning disable 1591
  1600. [EditorBrowsable(EditorBrowsableState.Never)]
  1601. public static IEnumerable<TSource> DistinctUntilChanged<TSource>(IEnumerable<TSource> source)
  1602. {
  1603. return EnumerableEx.DistinctUntilChanged(source);
  1604. }
  1605. #pragma warning restore 1591
  1606. /// <summary>
  1607. /// Returns consecutive distinct elements by using the specified equality comparer to compare values.
  1608. /// </summary>
  1609. /// <typeparam name="TSource">Source sequence element type.</typeparam>
  1610. /// <param name="source">Source sequence.</param>
  1611. /// <param name="comparer">Comparer used to compare values.</param>
  1612. /// <returns>Sequence without adjacent non-distinct elements.</returns>
  1613. public static IQueryable<TSource> DistinctUntilChanged<TSource>(this IQueryable<TSource> source, IEqualityComparer<TSource> comparer)
  1614. {
  1615. if (source == null)
  1616. throw new ArgumentNullException(nameof(source));
  1617. if (comparer == null)
  1618. throw new ArgumentNullException(nameof(comparer));
  1619. return source.Provider.CreateQuery<TSource>(
  1620. Expression.Call(
  1621. null,
  1622. ((MethodInfo)MethodInfo.GetCurrentMethod()).MakeGenericMethod(typeof(TSource)),
  1623. source.Expression,
  1624. Expression.Constant(comparer, typeof(IEqualityComparer<TSource>))
  1625. )
  1626. );
  1627. }
  1628. #pragma warning disable 1591
  1629. [EditorBrowsable(EditorBrowsableState.Never)]
  1630. public static IEnumerable<TSource> DistinctUntilChanged<TSource>(IEnumerable<TSource> source, IEqualityComparer<TSource> comparer)
  1631. {
  1632. return EnumerableEx.DistinctUntilChanged(source, comparer);
  1633. }
  1634. #pragma warning restore 1591
  1635. /// <summary>
  1636. /// Returns consecutive distinct elements based on a key value by using the specified equality comparer to compare key values.
  1637. /// </summary>
  1638. /// <typeparam name="TSource">Source sequence element type.</typeparam>
  1639. /// <typeparam name="TKey">Key type.</typeparam>
  1640. /// <param name="source">Source sequence.</param>
  1641. /// <param name="keySelector">Key selector.</param>
  1642. /// <returns>Sequence without adjacent non-distinct elements.</returns>
  1643. public static IQueryable<TSource> DistinctUntilChanged<TSource, TKey>(this IQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector)
  1644. {
  1645. if (source == null)
  1646. throw new ArgumentNullException(nameof(source));
  1647. if (keySelector == null)
  1648. throw new ArgumentNullException(nameof(keySelector));
  1649. return source.Provider.CreateQuery<TSource>(
  1650. Expression.Call(
  1651. null,
  1652. ((MethodInfo)MethodInfo.GetCurrentMethod()).MakeGenericMethod(typeof(TSource), typeof(TKey)),
  1653. source.Expression,
  1654. keySelector
  1655. )
  1656. );
  1657. }
  1658. #pragma warning disable 1591
  1659. [EditorBrowsable(EditorBrowsableState.Never)]
  1660. public static IEnumerable<TSource> DistinctUntilChanged<TSource, TKey>(IEnumerable<TSource> source, Func<TSource, TKey> keySelector)
  1661. {
  1662. return EnumerableEx.DistinctUntilChanged(source, keySelector);
  1663. }
  1664. #pragma warning restore 1591
  1665. /// <summary>
  1666. /// Returns consecutive distinct elements based on a key value by using the specified equality comparer to compare key values.
  1667. /// </summary>
  1668. /// <typeparam name="TSource">Source sequence element type.</typeparam>
  1669. /// <typeparam name="TKey">Key type.</typeparam>
  1670. /// <param name="source">Source sequence.</param>
  1671. /// <param name="keySelector">Key selector.</param>
  1672. /// <param name="comparer">Comparer used to compare key values.</param>
  1673. /// <returns>Sequence without adjacent non-distinct elements.</returns>
  1674. public static IQueryable<TSource> DistinctUntilChanged<TSource, TKey>(this IQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector, IEqualityComparer<TKey> comparer)
  1675. {
  1676. if (source == null)
  1677. throw new ArgumentNullException(nameof(source));
  1678. if (keySelector == null)
  1679. throw new ArgumentNullException(nameof(keySelector));
  1680. if (comparer == null)
  1681. throw new ArgumentNullException(nameof(comparer));
  1682. return source.Provider.CreateQuery<TSource>(
  1683. Expression.Call(
  1684. null,
  1685. ((MethodInfo)MethodInfo.GetCurrentMethod()).MakeGenericMethod(typeof(TSource), typeof(TKey)),
  1686. source.Expression,
  1687. keySelector,
  1688. Expression.Constant(comparer, typeof(IEqualityComparer<TKey>))
  1689. )
  1690. );
  1691. }
  1692. #pragma warning disable 1591
  1693. [EditorBrowsable(EditorBrowsableState.Never)]
  1694. public static IEnumerable<TSource> DistinctUntilChanged<TSource, TKey>(IEnumerable<TSource> source, Func<TSource, TKey> keySelector, IEqualityComparer<TKey> comparer)
  1695. {
  1696. return EnumerableEx.DistinctUntilChanged(source, keySelector, comparer);
  1697. }
  1698. #pragma warning restore 1591
  1699. /// <summary>
  1700. /// Expands the sequence by recursively applying a selector function.
  1701. /// </summary>
  1702. /// <typeparam name="TSource">Source sequence element type.</typeparam>
  1703. /// <param name="source">Source sequence.</param>
  1704. /// <param name="selector">Selector function to retrieve the next sequence to expand.</param>
  1705. /// <returns>Sequence with results from the recursive expansion of the source sequence.</returns>
  1706. public static IQueryable<TSource> Expand<TSource>(this IQueryable<TSource> source, Expression<Func<TSource, IEnumerable<TSource>>> selector)
  1707. {
  1708. if (source == null)
  1709. throw new ArgumentNullException(nameof(source));
  1710. if (selector == null)
  1711. throw new ArgumentNullException(nameof(selector));
  1712. return source.Provider.CreateQuery<TSource>(
  1713. Expression.Call(
  1714. null,
  1715. ((MethodInfo)MethodInfo.GetCurrentMethod()).MakeGenericMethod(typeof(TSource)),
  1716. source.Expression,
  1717. selector
  1718. )
  1719. );
  1720. }
  1721. #pragma warning disable 1591
  1722. [EditorBrowsable(EditorBrowsableState.Never)]
  1723. public static IEnumerable<TSource> Expand<TSource>(IEnumerable<TSource> source, Func<TSource, IEnumerable<TSource>> selector)
  1724. {
  1725. return EnumerableEx.Expand(source, selector);
  1726. }
  1727. #pragma warning restore 1591
  1728. /// <summary>
  1729. /// Returns the source sequence prefixed with the specified value.
  1730. /// </summary>
  1731. /// <typeparam name="TSource">Source sequence element type.</typeparam>
  1732. /// <param name="source">Source sequence.</param>
  1733. /// <param name="values">Values to prefix the sequence with.</param>
  1734. /// <returns>Sequence starting with the specified prefix value, followed by the source sequence.</returns>
  1735. public static IQueryable<TSource> StartWith<TSource>(this IQueryable<TSource> source, params TSource[] values)
  1736. {
  1737. if (source == null)
  1738. throw new ArgumentNullException(nameof(source));
  1739. return source.Provider.CreateQuery<TSource>(
  1740. Expression.Call(
  1741. null,
  1742. ((MethodInfo)MethodInfo.GetCurrentMethod()).MakeGenericMethod(typeof(TSource)),
  1743. source.Expression,
  1744. Expression.Constant(values, typeof(TSource[]))
  1745. )
  1746. );
  1747. }
  1748. #pragma warning disable 1591
  1749. [EditorBrowsable(EditorBrowsableState.Never)]
  1750. public static IEnumerable<TSource> StartWith<TSource>(IEnumerable<TSource> source, params TSource[] values)
  1751. {
  1752. return EnumerableEx.StartWith(source, values);
  1753. }
  1754. #pragma warning restore 1591
  1755. /// <summary>
  1756. /// Generates a sequence of accumulated values by scanning the source sequence and applying an accumulator function.
  1757. /// </summary>
  1758. /// <typeparam name="TSource">Source sequence element type.</typeparam>
  1759. /// <typeparam name="TAccumulate">Accumulation type.</typeparam>
  1760. /// <param name="source">Source sequence.</param>
  1761. /// <param name="seed">Accumulator seed value.</param>
  1762. /// <param name="accumulator">Accumulation function to apply to the current accumulation value and each element of the sequence.</param>
  1763. /// <returns>Sequence with all intermediate accumulation values resulting from scanning the sequence.</returns>
  1764. public static IQueryable<TAccumulate> Scan<TSource, TAccumulate>(this IQueryable<TSource> source, TAccumulate seed, Expression<Func<TAccumulate, TSource, TAccumulate>> accumulator)
  1765. {
  1766. if (source == null)
  1767. throw new ArgumentNullException(nameof(source));
  1768. if (accumulator == null)
  1769. throw new ArgumentNullException(nameof(accumulator));
  1770. return source.Provider.CreateQuery<TAccumulate>(
  1771. Expression.Call(
  1772. null,
  1773. ((MethodInfo)MethodInfo.GetCurrentMethod()).MakeGenericMethod(typeof(TSource), typeof(TAccumulate)),
  1774. source.Expression,
  1775. Expression.Constant(seed, typeof(TAccumulate)),
  1776. accumulator
  1777. )
  1778. );
  1779. }
  1780. #pragma warning disable 1591
  1781. [EditorBrowsable(EditorBrowsableState.Never)]
  1782. public static IEnumerable<TAccumulate> Scan<TSource, TAccumulate>(IEnumerable<TSource> source, TAccumulate seed, Func<TAccumulate, TSource, TAccumulate> accumulator)
  1783. {
  1784. return EnumerableEx.Scan(source, seed, accumulator);
  1785. }
  1786. #pragma warning restore 1591
  1787. /// <summary>
  1788. /// Generates a sequence of accumulated values by scanning the source sequence and applying an accumulator function.
  1789. /// </summary>
  1790. /// <typeparam name="TSource">Source sequence element type.</typeparam>
  1791. /// <param name="source">Source sequence.</param>
  1792. /// <param name="accumulator">Accumulation function to apply to the current accumulation value and each element of the sequence.</param>
  1793. /// <returns>Sequence with all intermediate accumulation values resulting from scanning the sequence.</returns>
  1794. public static IQueryable<TSource> Scan<TSource>(this IQueryable<TSource> source, Expression<Func<TSource, TSource, TSource>> accumulator)
  1795. {
  1796. if (source == null)
  1797. throw new ArgumentNullException(nameof(source));
  1798. if (accumulator == null)
  1799. throw new ArgumentNullException(nameof(accumulator));
  1800. return source.Provider.CreateQuery<TSource>(
  1801. Expression.Call(
  1802. null,
  1803. ((MethodInfo)MethodInfo.GetCurrentMethod()).MakeGenericMethod(typeof(TSource)),
  1804. source.Expression,
  1805. accumulator
  1806. )
  1807. );
  1808. }
  1809. #pragma warning disable 1591
  1810. [EditorBrowsable(EditorBrowsableState.Never)]
  1811. public static IEnumerable<TSource> Scan<TSource>(IEnumerable<TSource> source, Func<TSource, TSource, TSource> accumulator)
  1812. {
  1813. return EnumerableEx.Scan(source, accumulator);
  1814. }
  1815. #pragma warning restore 1591
  1816. /// <summary>
  1817. /// Returns a specified number of contiguous elements from the end of the sequence.
  1818. /// </summary>
  1819. /// <typeparam name="TSource">Source sequence element type.</typeparam>
  1820. /// <param name="source">Source sequence.</param>
  1821. /// <param name="count">The number of elements to take from the end of the sequence.</param>
  1822. /// <returns>Sequence with the specified number of elements counting from the end of the source sequence.</returns>
  1823. public static IQueryable<TSource> TakeLast<TSource>(this IQueryable<TSource> source, int count)
  1824. {
  1825. if (source == null)
  1826. throw new ArgumentNullException(nameof(source));
  1827. return source.Provider.CreateQuery<TSource>(
  1828. Expression.Call(
  1829. null,
  1830. ((MethodInfo)MethodInfo.GetCurrentMethod()).MakeGenericMethod(typeof(TSource)),
  1831. source.Expression,
  1832. Expression.Constant(count, typeof(int))
  1833. )
  1834. );
  1835. }
  1836. #pragma warning disable 1591
  1837. [EditorBrowsable(EditorBrowsableState.Never)]
  1838. public static IEnumerable<TSource> TakeLast<TSource>(IEnumerable<TSource> source, int count)
  1839. {
  1840. #if REFERENCE_ASSEMBLY
  1841. return default;
  1842. #else
  1843. return EnumerableEx.TakeLast(source, count);
  1844. #endif
  1845. }
  1846. #pragma warning restore 1591
  1847. /// <summary>
  1848. /// Bypasses a specified number of contiguous elements from the end of the sequence and returns the remaining elements.
  1849. /// </summary>
  1850. /// <typeparam name="TSource">Source sequence element type.</typeparam>
  1851. /// <param name="source">Source sequence.</param>
  1852. /// <param name="count">The number of elements to skip from the end of the sequence before returning the remaining elements.</param>
  1853. /// <returns>Sequence bypassing the specified number of elements counting from the end of the source sequence.</returns>
  1854. public static IQueryable<TSource> SkipLast<TSource>(this IQueryable<TSource> source, int count)
  1855. {
  1856. if (source == null)
  1857. throw new ArgumentNullException(nameof(source));
  1858. return source.Provider.CreateQuery<TSource>(
  1859. Expression.Call(
  1860. null,
  1861. ((MethodInfo)MethodInfo.GetCurrentMethod()).MakeGenericMethod(typeof(TSource)),
  1862. source.Expression,
  1863. Expression.Constant(count, typeof(int))
  1864. )
  1865. );
  1866. }
  1867. #pragma warning disable 1591
  1868. [EditorBrowsable(EditorBrowsableState.Never)]
  1869. public static IEnumerable<TSource> SkipLast<TSource>(IEnumerable<TSource> source, int count)
  1870. {
  1871. #if REFERENCE_ASSEMBLY
  1872. return default;
  1873. #else
  1874. return EnumerableEx.SkipLast(source, count);
  1875. #endif
  1876. }
  1877. #pragma warning restore 1591
  1878. /// <summary>
  1879. /// Repeats and concatenates the source sequence infinitely.
  1880. /// </summary>
  1881. /// <typeparam name="TSource">Source sequence element type.</typeparam>
  1882. /// <param name="source">Source sequence.</param>
  1883. /// <returns>Sequence obtained by concatenating the source sequence to itself infinitely.</returns>
  1884. public static IQueryable<TSource> Repeat<TSource>(this IQueryable<TSource> source)
  1885. {
  1886. if (source == null)
  1887. throw new ArgumentNullException(nameof(source));
  1888. return source.Provider.CreateQuery<TSource>(
  1889. Expression.Call(
  1890. null,
  1891. ((MethodInfo)MethodInfo.GetCurrentMethod()).MakeGenericMethod(typeof(TSource)),
  1892. source.Expression
  1893. )
  1894. );
  1895. }
  1896. #pragma warning disable 1591
  1897. [EditorBrowsable(EditorBrowsableState.Never)]
  1898. public static IEnumerable<TSource> Repeat<TSource>(IEnumerable<TSource> source)
  1899. {
  1900. return EnumerableEx.Repeat(source);
  1901. }
  1902. #pragma warning restore 1591
  1903. /// <summary>
  1904. /// Repeats and concatenates the source sequence the given number of times.
  1905. /// </summary>
  1906. /// <typeparam name="TSource">Source sequence element type.</typeparam>
  1907. /// <param name="source">Source sequence.</param>
  1908. /// <param name="count">Number of times to repeat the source sequence.</param>
  1909. /// <returns>Sequence obtained by concatenating the source sequence to itself the specified number of times.</returns>
  1910. public static IQueryable<TSource> Repeat<TSource>(this IQueryable<TSource> source, int count)
  1911. {
  1912. if (source == null)
  1913. throw new ArgumentNullException(nameof(source));
  1914. return source.Provider.CreateQuery<TSource>(
  1915. Expression.Call(
  1916. null,
  1917. ((MethodInfo)MethodInfo.GetCurrentMethod()).MakeGenericMethod(typeof(TSource)),
  1918. source.Expression,
  1919. Expression.Constant(count, typeof(int))
  1920. )
  1921. );
  1922. }
  1923. #pragma warning disable 1591
  1924. [EditorBrowsable(EditorBrowsableState.Never)]
  1925. public static IEnumerable<TSource> Repeat<TSource>(IEnumerable<TSource> source, int count)
  1926. {
  1927. return EnumerableEx.Repeat(source, count);
  1928. }
  1929. #pragma warning restore 1591
  1930. /// <summary>
  1931. /// Returns a sequence with no elements.
  1932. /// </summary>
  1933. /// <typeparam name="TResult">Result sequence element type.</typeparam>
  1934. /// <param name="provider">Query provider.</param>
  1935. /// <returns>Sequence with no elements.</returns>
  1936. public static IQueryable<TResult> Empty<TResult>(this IQueryProvider provider)
  1937. {
  1938. if (provider == null)
  1939. throw new ArgumentNullException(nameof(provider));
  1940. return provider.CreateQuery<TResult>(
  1941. Expression.Call(
  1942. null,
  1943. ((MethodInfo)MethodInfo.GetCurrentMethod()).MakeGenericMethod(typeof(TResult)),
  1944. Expression.Constant(provider, typeof(IQueryProvider))
  1945. )
  1946. );
  1947. }
  1948. #pragma warning disable 1591
  1949. [EditorBrowsable(EditorBrowsableState.Never)]
  1950. public static /*!*/IQueryable<TResult> Empty<TResult>()
  1951. {
  1952. return Enumerable.Empty<TResult>().AsQueryable();
  1953. }
  1954. #pragma warning restore 1591
  1955. /// <summary>
  1956. /// Generates a sequence of integral numbers within a specified range.
  1957. /// </summary>
  1958. /// <param name="provider">Query provider.</param>
  1959. /// <param name="start">The value of the first integer in the sequence.</param>
  1960. /// <param name="count">The number of sequential integers to generate.</param>
  1961. /// <returns>Sequence that contains a range of sequential integral numbers.</returns>
  1962. public static IQueryable<int> Range(this IQueryProvider provider, int start, int count)
  1963. {
  1964. if (provider == null)
  1965. throw new ArgumentNullException(nameof(provider));
  1966. return provider.CreateQuery<int>(
  1967. Expression.Call(
  1968. null,
  1969. (MethodInfo)MethodInfo.GetCurrentMethod(),
  1970. Expression.Constant(provider, typeof(IQueryProvider)),
  1971. Expression.Constant(start, typeof(int)),
  1972. Expression.Constant(count, typeof(int))
  1973. )
  1974. );
  1975. }
  1976. #pragma warning disable 1591
  1977. [EditorBrowsable(EditorBrowsableState.Never)]
  1978. public static /*!*/IQueryable<int> Range(int start, int count)
  1979. {
  1980. return Enumerable.Range(start, count).AsQueryable();
  1981. }
  1982. #pragma warning restore 1591
  1983. /// <summary>
  1984. /// Generates a sequence that contains one repeated value.
  1985. /// </summary>
  1986. /// <typeparam name="TResult">Result sequence element type.</typeparam>
  1987. /// <param name="provider">Query provider.</param>
  1988. /// <param name="element">The value to be repeated.</param>
  1989. /// <param name="count">The number of times to repeat the value in the generated sequence.</param>
  1990. /// <returns>Sequence that contains a repeated value.</returns>
  1991. public static IQueryable<TResult> Repeat<TResult>(this IQueryProvider provider, TResult element, int count)
  1992. {
  1993. if (provider == null)
  1994. throw new ArgumentNullException(nameof(provider));
  1995. return provider.CreateQuery<TResult>(
  1996. Expression.Call(
  1997. null,
  1998. ((MethodInfo)MethodInfo.GetCurrentMethod()).MakeGenericMethod(typeof(TResult)),
  1999. Expression.Constant(provider, typeof(IQueryProvider)),
  2000. Expression.Constant(element, typeof(TResult)),
  2001. Expression.Constant(count, typeof(int))
  2002. )
  2003. );
  2004. }
  2005. #pragma warning disable 1591
  2006. [EditorBrowsable(EditorBrowsableState.Never)]
  2007. public static /*!*/IQueryable<TResult> Repeat<TResult>(TResult element, int count)
  2008. {
  2009. return EnumerableEx.Repeat<TResult>(element, count).AsQueryable();
  2010. }
  2011. #pragma warning restore 1591
  2012. }
  2013. }