QueryableEx.Generated.cs 99 KB

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