QueryableEx.cs 107 KB

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