QueryableEx.cs 119 KB

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