QueryableEx.cs 117 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545
  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. /// <summary>
  1314. /// Hides the enumerable sequence object identity.
  1315. /// </summary>
  1316. /// <typeparam name="TSource">Source sequence element type.</typeparam>
  1317. /// <param name="source">Source sequence.</param>
  1318. /// <returns>Enumerable sequence with the same behavior as the original, but hiding the source object identity.</returns>
  1319. /// <remarks>AsQueryable doesn't hide the object identity, and simply acts as a cast to the IQueryable&lt;TSource&gt; interface.</remarks>
  1320. public static IQueryable<TSource> Hide<TSource>(this IQueryable<TSource> source)
  1321. {
  1322. if (source == null)
  1323. throw new ArgumentNullException(nameof(source));
  1324. return source.Provider.CreateQuery<TSource>(
  1325. Expression.Call(
  1326. null,
  1327. #if CRIPPLED_REFLECTION
  1328. InfoOf(() => QueryableEx.Hide<TSource>(default(IQueryable<TSource>))),
  1329. #else
  1330. ((MethodInfo)MethodInfo.GetCurrentMethod()).MakeGenericMethod(typeof(TSource)),
  1331. #endif
  1332. source.Expression
  1333. )
  1334. );
  1335. }
  1336. #pragma warning disable 1591
  1337. [EditorBrowsable(EditorBrowsableState.Never)]
  1338. public static IEnumerable<TSource> Hide<TSource>(IEnumerable<TSource> source)
  1339. {
  1340. return EnumerableEx.Hide(source);
  1341. }
  1342. #pragma warning restore 1591
  1343. /// <summary>
  1344. /// Lazily invokes an action for each value in the sequence.
  1345. /// </summary>
  1346. /// <typeparam name="TSource">Source sequence element type.</typeparam>
  1347. /// <param name="source">Source sequence.</param>
  1348. /// <param name="onNext">Action to invoke for each element.</param>
  1349. /// <returns>Sequence exhibiting the specified side-effects upon enumeration.</returns>
  1350. public static IQueryable<TSource> Do<TSource>(this IQueryable<TSource> source, Expression<Action<TSource>> onNext)
  1351. {
  1352. if (source == null)
  1353. throw new ArgumentNullException(nameof(source));
  1354. if (onNext == null)
  1355. throw new ArgumentNullException(nameof(onNext));
  1356. return source.Provider.CreateQuery<TSource>(
  1357. Expression.Call(
  1358. null,
  1359. #if CRIPPLED_REFLECTION
  1360. InfoOf(() => QueryableEx.Do<TSource>(default(IQueryable<TSource>), default(Expression<Action<TSource>>))),
  1361. #else
  1362. ((MethodInfo)MethodInfo.GetCurrentMethod()).MakeGenericMethod(typeof(TSource)),
  1363. #endif
  1364. source.Expression,
  1365. onNext
  1366. )
  1367. );
  1368. }
  1369. #pragma warning disable 1591
  1370. [EditorBrowsable(EditorBrowsableState.Never)]
  1371. public static IEnumerable<TSource> Do<TSource>(IEnumerable<TSource> source, Action<TSource> onNext)
  1372. {
  1373. return EnumerableEx.Do(source, onNext);
  1374. }
  1375. #pragma warning restore 1591
  1376. /// <summary>
  1377. /// Lazily invokes an action for each value in the sequence, and executes an action for successful termination.
  1378. /// </summary>
  1379. /// <typeparam name="TSource">Source sequence element type.</typeparam>
  1380. /// <param name="source">Source sequence.</param>
  1381. /// <param name="onNext">Action to invoke for each element.</param>
  1382. /// <param name="onCompleted">Action to invoke on successful termination of the sequence.</param>
  1383. /// <returns>Sequence exhibiting the specified side-effects upon enumeration.</returns>
  1384. public static IQueryable<TSource> Do<TSource>(this IQueryable<TSource> source, Expression<Action<TSource>> onNext, Expression<Action> onCompleted)
  1385. {
  1386. if (source == null)
  1387. throw new ArgumentNullException(nameof(source));
  1388. if (onNext == null)
  1389. throw new ArgumentNullException(nameof(onNext));
  1390. if (onCompleted == null)
  1391. throw new ArgumentNullException(nameof(onCompleted));
  1392. return source.Provider.CreateQuery<TSource>(
  1393. Expression.Call(
  1394. null,
  1395. #if CRIPPLED_REFLECTION
  1396. InfoOf(() => QueryableEx.Do<TSource>(default(IQueryable<TSource>), default(Expression<Action<TSource>>), default(Expression<Action>))),
  1397. #else
  1398. ((MethodInfo)MethodInfo.GetCurrentMethod()).MakeGenericMethod(typeof(TSource)),
  1399. #endif
  1400. source.Expression,
  1401. onNext,
  1402. onCompleted
  1403. )
  1404. );
  1405. }
  1406. #pragma warning disable 1591
  1407. [EditorBrowsable(EditorBrowsableState.Never)]
  1408. public static IEnumerable<TSource> Do<TSource>(IEnumerable<TSource> source, Action<TSource> onNext, Action onCompleted)
  1409. {
  1410. return EnumerableEx.Do(source, onNext, onCompleted);
  1411. }
  1412. #pragma warning restore 1591
  1413. /// <summary>
  1414. /// Lazily invokes an action for each value in the sequence, and executes an action upon exceptional termination.
  1415. /// </summary>
  1416. /// <typeparam name="TSource">Source sequence element type.</typeparam>
  1417. /// <param name="source">Source sequence.</param>
  1418. /// <param name="onNext">Action to invoke for each element.</param>
  1419. /// <param name="onError">Action to invoke on exceptional termination of the sequence.</param>
  1420. /// <returns>Sequence exhibiting the specified side-effects upon enumeration.</returns>
  1421. public static IQueryable<TSource> Do<TSource>(this IQueryable<TSource> source, Expression<Action<TSource>> onNext, Expression<Action<Exception>> onError)
  1422. {
  1423. if (source == null)
  1424. throw new ArgumentNullException(nameof(source));
  1425. if (onNext == null)
  1426. throw new ArgumentNullException(nameof(onNext));
  1427. if (onError == null)
  1428. throw new ArgumentNullException(nameof(onError));
  1429. return source.Provider.CreateQuery<TSource>(
  1430. Expression.Call(
  1431. null,
  1432. #if CRIPPLED_REFLECTION
  1433. InfoOf(() => QueryableEx.Do<TSource>(default(IQueryable<TSource>), default(Expression<Action<TSource>>), default(Expression<Action<Exception>>))),
  1434. #else
  1435. ((MethodInfo)MethodInfo.GetCurrentMethod()).MakeGenericMethod(typeof(TSource)),
  1436. #endif
  1437. source.Expression,
  1438. onNext,
  1439. onError
  1440. )
  1441. );
  1442. }
  1443. #pragma warning disable 1591
  1444. [EditorBrowsable(EditorBrowsableState.Never)]
  1445. public static IEnumerable<TSource> Do<TSource>(IEnumerable<TSource> source, Action<TSource> onNext, Action<Exception> onError)
  1446. {
  1447. return EnumerableEx.Do(source, onNext, onError);
  1448. }
  1449. #pragma warning restore 1591
  1450. /// <summary>
  1451. /// Lazily invokes an action for each value in the sequence, and executes an action upon successful or exceptional termination.
  1452. /// </summary>
  1453. /// <typeparam name="TSource">Source sequence element type.</typeparam>
  1454. /// <param name="source">Source sequence.</param>
  1455. /// <param name="onNext">Action to invoke for each element.</param>
  1456. /// <param name="onError">Action to invoke on exceptional termination of the sequence.</param>
  1457. /// <param name="onCompleted">Action to invoke on successful termination of the sequence.</param>
  1458. /// <returns>Sequence exhibiting the specified side-effects upon enumeration.</returns>
  1459. public static IQueryable<TSource> Do<TSource>(this IQueryable<TSource> source, Expression<Action<TSource>> onNext, Expression<Action<Exception>> onError, Expression<Action> onCompleted)
  1460. {
  1461. if (source == null)
  1462. throw new ArgumentNullException(nameof(source));
  1463. if (onNext == null)
  1464. throw new ArgumentNullException(nameof(onNext));
  1465. if (onError == null)
  1466. throw new ArgumentNullException(nameof(onError));
  1467. if (onCompleted == null)
  1468. throw new ArgumentNullException(nameof(onCompleted));
  1469. return source.Provider.CreateQuery<TSource>(
  1470. Expression.Call(
  1471. null,
  1472. #if CRIPPLED_REFLECTION
  1473. InfoOf(() => QueryableEx.Do<TSource>(default(IQueryable<TSource>), default(Expression<Action<TSource>>), default(Expression<Action<Exception>>), default(Expression<Action>))),
  1474. #else
  1475. ((MethodInfo)MethodInfo.GetCurrentMethod()).MakeGenericMethod(typeof(TSource)),
  1476. #endif
  1477. source.Expression,
  1478. onNext,
  1479. onError,
  1480. onCompleted
  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, Action onCompleted)
  1487. {
  1488. return EnumerableEx.Do(source, onNext, onError, onCompleted);
  1489. }
  1490. #pragma warning restore 1591
  1491. /// <summary>
  1492. /// Lazily invokes observer methods for each value in the sequence, and 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="observer">Observer to invoke notification calls on.</param>
  1497. /// <returns>Sequence exhibiting the side-effects of observer method invocation upon enumeration.</returns>
  1498. public static IQueryable<TSource> Do<TSource>(this IQueryable<TSource> source, IObserver<TSource> observer)
  1499. {
  1500. if (source == null)
  1501. throw new ArgumentNullException(nameof(source));
  1502. if (observer == null)
  1503. throw new ArgumentNullException(nameof(observer));
  1504. return source.Provider.CreateQuery<TSource>(
  1505. Expression.Call(
  1506. null,
  1507. #if CRIPPLED_REFLECTION
  1508. InfoOf(() => QueryableEx.Do<TSource>(default(IQueryable<TSource>), default(IObserver<TSource>))),
  1509. #else
  1510. ((MethodInfo)MethodInfo.GetCurrentMethod()).MakeGenericMethod(typeof(TSource)),
  1511. #endif
  1512. source.Expression,
  1513. Expression.Constant(observer, typeof(IObserver<TSource>))
  1514. )
  1515. );
  1516. }
  1517. #pragma warning disable 1591
  1518. [EditorBrowsable(EditorBrowsableState.Never)]
  1519. public static IEnumerable<TSource> Do<TSource>(IEnumerable<TSource> source, IObserver<TSource> observer)
  1520. {
  1521. return EnumerableEx.Do(source, observer);
  1522. }
  1523. #pragma warning restore 1591
  1524. /// <summary>
  1525. /// Generates a sequence of non-overlapping adjacent buffers over the source sequence.
  1526. /// </summary>
  1527. /// <typeparam name="TSource">Source sequence element type.</typeparam>
  1528. /// <param name="source">Source sequence.</param>
  1529. /// <param name="count">Number of elements for allocated buffers.</param>
  1530. /// <returns>Sequence of buffers containing source sequence elements.</returns>
  1531. public static IQueryable<IList<TSource>> Buffer<TSource>(this IQueryable<TSource> source, int count)
  1532. {
  1533. if (source == null)
  1534. throw new ArgumentNullException(nameof(source));
  1535. return source.Provider.CreateQuery<IList<TSource>>(
  1536. Expression.Call(
  1537. null,
  1538. #if CRIPPLED_REFLECTION
  1539. InfoOf(() => QueryableEx.Buffer<TSource>(default(IQueryable<TSource>), default(int))),
  1540. #else
  1541. ((MethodInfo)MethodInfo.GetCurrentMethod()).MakeGenericMethod(typeof(TSource)),
  1542. #endif
  1543. source.Expression,
  1544. Expression.Constant(count, typeof(int))
  1545. )
  1546. );
  1547. }
  1548. #pragma warning disable 1591
  1549. [EditorBrowsable(EditorBrowsableState.Never)]
  1550. public static IEnumerable<IList<TSource>> Buffer<TSource>(IEnumerable<TSource> source, int count)
  1551. {
  1552. return EnumerableEx.Buffer(source, count);
  1553. }
  1554. #pragma warning restore 1591
  1555. /// <summary>
  1556. /// Generates a sequence of buffers over the source sequence, with specified length and possible overlap.
  1557. /// </summary>
  1558. /// <typeparam name="TSource">Source sequence element type.</typeparam>
  1559. /// <param name="source">Source sequence.</param>
  1560. /// <param name="count">Number of elements for allocated buffers.</param>
  1561. /// <param name="skip">Number of elements to skip between the start of consecutive buffers.</param>
  1562. /// <returns>Sequence of buffers containing source sequence elements.</returns>
  1563. public static IQueryable<IList<TSource>> Buffer<TSource>(this IQueryable<TSource> source, int count, int skip)
  1564. {
  1565. if (source == null)
  1566. throw new ArgumentNullException(nameof(source));
  1567. return source.Provider.CreateQuery<IList<TSource>>(
  1568. Expression.Call(
  1569. null,
  1570. #if CRIPPLED_REFLECTION
  1571. InfoOf(() => QueryableEx.Buffer<TSource>(default(IQueryable<TSource>), default(int), default(int))),
  1572. #else
  1573. ((MethodInfo)MethodInfo.GetCurrentMethod()).MakeGenericMethod(typeof(TSource)),
  1574. #endif
  1575. source.Expression,
  1576. Expression.Constant(count, typeof(int)),
  1577. Expression.Constant(skip, typeof(int))
  1578. )
  1579. );
  1580. }
  1581. #pragma warning disable 1591
  1582. [EditorBrowsable(EditorBrowsableState.Never)]
  1583. public static IEnumerable<IList<TSource>> Buffer<TSource>(IEnumerable<TSource> source, int count, int skip)
  1584. {
  1585. return EnumerableEx.Buffer(source, count, skip);
  1586. }
  1587. #pragma warning restore 1591
  1588. /// <summary>
  1589. /// Ignores all elements in the source sequence.
  1590. /// </summary>
  1591. /// <typeparam name="TSource">Source sequence element type.</typeparam>
  1592. /// <param name="source">Source sequence.</param>
  1593. /// <returns>Source sequence without its elements.</returns>
  1594. public static IQueryable<TSource> IgnoreElements<TSource>(this IQueryable<TSource> source)
  1595. {
  1596. if (source == null)
  1597. throw new ArgumentNullException(nameof(source));
  1598. return source.Provider.CreateQuery<TSource>(
  1599. Expression.Call(
  1600. null,
  1601. #if CRIPPLED_REFLECTION
  1602. InfoOf(() => QueryableEx.IgnoreElements<TSource>(default(IQueryable<TSource>))),
  1603. #else
  1604. ((MethodInfo)MethodInfo.GetCurrentMethod()).MakeGenericMethod(typeof(TSource)),
  1605. #endif
  1606. source.Expression
  1607. )
  1608. );
  1609. }
  1610. #pragma warning disable 1591
  1611. [EditorBrowsable(EditorBrowsableState.Never)]
  1612. public static IEnumerable<TSource> IgnoreElements<TSource>(IEnumerable<TSource> source)
  1613. {
  1614. return EnumerableEx.IgnoreElements(source);
  1615. }
  1616. #pragma warning restore 1591
  1617. /// <summary>
  1618. /// Returns elements with a distinct key value by using the default equality comparer to compare key values.
  1619. /// </summary>
  1620. /// <typeparam name="TSource">Source sequence element type.</typeparam>
  1621. /// <typeparam name="TKey">Key type.</typeparam>
  1622. /// <param name="source">Source sequence.</param>
  1623. /// <param name="keySelector">Key selector.</param>
  1624. /// <returns>Sequence that contains the elements from the source sequence with distinct key values.</returns>
  1625. public static IQueryable<TSource> Distinct<TSource, TKey>(this IQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector)
  1626. {
  1627. if (source == null)
  1628. throw new ArgumentNullException(nameof(source));
  1629. if (keySelector == null)
  1630. throw new ArgumentNullException(nameof(keySelector));
  1631. return source.Provider.CreateQuery<TSource>(
  1632. Expression.Call(
  1633. null,
  1634. #if CRIPPLED_REFLECTION
  1635. InfoOf(() => QueryableEx.Distinct<TSource, TKey>(default(IQueryable<TSource>), default(Expression<Func<TSource, TKey>>))),
  1636. #else
  1637. ((MethodInfo)MethodInfo.GetCurrentMethod()).MakeGenericMethod(typeof(TSource), typeof(TKey)),
  1638. #endif
  1639. source.Expression,
  1640. keySelector
  1641. )
  1642. );
  1643. }
  1644. #pragma warning disable 1591
  1645. [EditorBrowsable(EditorBrowsableState.Never)]
  1646. public static IEnumerable<TSource> Distinct<TSource, TKey>(IEnumerable<TSource> source, Func<TSource, TKey> keySelector)
  1647. {
  1648. return EnumerableEx.Distinct(source, keySelector);
  1649. }
  1650. #pragma warning restore 1591
  1651. /// <summary>
  1652. /// Returns elements with a distinct key value by using the specified equality comparer to compare key values.
  1653. /// </summary>
  1654. /// <typeparam name="TSource">Source sequence element type.</typeparam>
  1655. /// <typeparam name="TKey">Key type.</typeparam>
  1656. /// <param name="source">Source sequence.</param>
  1657. /// <param name="keySelector">Key selector.</param>
  1658. /// <param name="comparer">Comparer used to compare key values.</param>
  1659. /// <returns>Sequence that contains the elements from the source sequence with distinct key values.</returns>
  1660. public static IQueryable<TSource> Distinct<TSource, TKey>(this IQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector, IEqualityComparer<TKey> comparer)
  1661. {
  1662. if (source == null)
  1663. throw new ArgumentNullException(nameof(source));
  1664. if (keySelector == null)
  1665. throw new ArgumentNullException(nameof(keySelector));
  1666. if (comparer == null)
  1667. throw new ArgumentNullException(nameof(comparer));
  1668. return source.Provider.CreateQuery<TSource>(
  1669. Expression.Call(
  1670. null,
  1671. #if CRIPPLED_REFLECTION
  1672. InfoOf(() => QueryableEx.Distinct<TSource, TKey>(default(IQueryable<TSource>), default(Expression<Func<TSource, TKey>>), default(IEqualityComparer<TKey>))),
  1673. #else
  1674. ((MethodInfo)MethodInfo.GetCurrentMethod()).MakeGenericMethod(typeof(TSource), typeof(TKey)),
  1675. #endif
  1676. source.Expression,
  1677. keySelector,
  1678. Expression.Constant(comparer, typeof(IEqualityComparer<TKey>))
  1679. )
  1680. );
  1681. }
  1682. #pragma warning disable 1591
  1683. [EditorBrowsable(EditorBrowsableState.Never)]
  1684. public static IEnumerable<TSource> Distinct<TSource, TKey>(IEnumerable<TSource> source, Func<TSource, TKey> keySelector, IEqualityComparer<TKey> comparer)
  1685. {
  1686. return EnumerableEx.Distinct(source, keySelector, comparer);
  1687. }
  1688. #pragma warning restore 1591
  1689. /// <summary>
  1690. /// Returns consecutive distinct elements by using the default equality comparer to compare values.
  1691. /// </summary>
  1692. /// <typeparam name="TSource">Source sequence element type.</typeparam>
  1693. /// <param name="source">Source sequence.</param>
  1694. /// <returns>Sequence without adjacent non-distinct elements.</returns>
  1695. public static IQueryable<TSource> DistinctUntilChanged<TSource>(this IQueryable<TSource> source)
  1696. {
  1697. if (source == null)
  1698. throw new ArgumentNullException(nameof(source));
  1699. return source.Provider.CreateQuery<TSource>(
  1700. Expression.Call(
  1701. null,
  1702. #if CRIPPLED_REFLECTION
  1703. InfoOf(() => QueryableEx.DistinctUntilChanged<TSource>(default(IQueryable<TSource>))),
  1704. #else
  1705. ((MethodInfo)MethodInfo.GetCurrentMethod()).MakeGenericMethod(typeof(TSource)),
  1706. #endif
  1707. source.Expression
  1708. )
  1709. );
  1710. }
  1711. #pragma warning disable 1591
  1712. [EditorBrowsable(EditorBrowsableState.Never)]
  1713. public static IEnumerable<TSource> DistinctUntilChanged<TSource>(IEnumerable<TSource> source)
  1714. {
  1715. return EnumerableEx.DistinctUntilChanged(source);
  1716. }
  1717. #pragma warning restore 1591
  1718. /// <summary>
  1719. /// Returns consecutive distinct elements by using the specified equality comparer to compare values.
  1720. /// </summary>
  1721. /// <typeparam name="TSource">Source sequence element type.</typeparam>
  1722. /// <param name="source">Source sequence.</param>
  1723. /// <param name="comparer">Comparer used to compare values.</param>
  1724. /// <returns>Sequence without adjacent non-distinct elements.</returns>
  1725. public static IQueryable<TSource> DistinctUntilChanged<TSource>(this IQueryable<TSource> source, IEqualityComparer<TSource> comparer)
  1726. {
  1727. if (source == null)
  1728. throw new ArgumentNullException(nameof(source));
  1729. if (comparer == null)
  1730. throw new ArgumentNullException(nameof(comparer));
  1731. return source.Provider.CreateQuery<TSource>(
  1732. Expression.Call(
  1733. null,
  1734. #if CRIPPLED_REFLECTION
  1735. InfoOf(() => QueryableEx.DistinctUntilChanged<TSource>(default(IQueryable<TSource>), default(IEqualityComparer<TSource>))),
  1736. #else
  1737. ((MethodInfo)MethodInfo.GetCurrentMethod()).MakeGenericMethod(typeof(TSource)),
  1738. #endif
  1739. source.Expression,
  1740. Expression.Constant(comparer, typeof(IEqualityComparer<TSource>))
  1741. )
  1742. );
  1743. }
  1744. #pragma warning disable 1591
  1745. [EditorBrowsable(EditorBrowsableState.Never)]
  1746. public static IEnumerable<TSource> DistinctUntilChanged<TSource>(IEnumerable<TSource> source, IEqualityComparer<TSource> comparer)
  1747. {
  1748. return EnumerableEx.DistinctUntilChanged(source, comparer);
  1749. }
  1750. #pragma warning restore 1591
  1751. /// <summary>
  1752. /// Returns consecutive distinct elements based on a key value by using the specified equality comparer to compare key values.
  1753. /// </summary>
  1754. /// <typeparam name="TSource">Source sequence element type.</typeparam>
  1755. /// <typeparam name="TKey">Key type.</typeparam>
  1756. /// <param name="source">Source sequence.</param>
  1757. /// <param name="keySelector">Key selector.</param>
  1758. /// <returns>Sequence without adjacent non-distinct elements.</returns>
  1759. public static IQueryable<TSource> DistinctUntilChanged<TSource, TKey>(this IQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector)
  1760. {
  1761. if (source == null)
  1762. throw new ArgumentNullException(nameof(source));
  1763. if (keySelector == null)
  1764. throw new ArgumentNullException(nameof(keySelector));
  1765. return source.Provider.CreateQuery<TSource>(
  1766. Expression.Call(
  1767. null,
  1768. #if CRIPPLED_REFLECTION
  1769. InfoOf(() => QueryableEx.DistinctUntilChanged<TSource, TKey>(default(IQueryable<TSource>), default(Expression<Func<TSource, TKey>>))),
  1770. #else
  1771. ((MethodInfo)MethodInfo.GetCurrentMethod()).MakeGenericMethod(typeof(TSource), typeof(TKey)),
  1772. #endif
  1773. source.Expression,
  1774. keySelector
  1775. )
  1776. );
  1777. }
  1778. #pragma warning disable 1591
  1779. [EditorBrowsable(EditorBrowsableState.Never)]
  1780. public static IEnumerable<TSource> DistinctUntilChanged<TSource, TKey>(IEnumerable<TSource> source, Func<TSource, TKey> keySelector)
  1781. {
  1782. return EnumerableEx.DistinctUntilChanged(source, keySelector);
  1783. }
  1784. #pragma warning restore 1591
  1785. /// <summary>
  1786. /// Returns consecutive distinct elements based on a key value by using the specified equality comparer to compare key values.
  1787. /// </summary>
  1788. /// <typeparam name="TSource">Source sequence element type.</typeparam>
  1789. /// <typeparam name="TKey">Key type.</typeparam>
  1790. /// <param name="source">Source sequence.</param>
  1791. /// <param name="keySelector">Key selector.</param>
  1792. /// <param name="comparer">Comparer used to compare key values.</param>
  1793. /// <returns>Sequence without adjacent non-distinct elements.</returns>
  1794. public static IQueryable<TSource> DistinctUntilChanged<TSource, TKey>(this IQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector, IEqualityComparer<TKey> comparer)
  1795. {
  1796. if (source == null)
  1797. throw new ArgumentNullException(nameof(source));
  1798. if (keySelector == null)
  1799. throw new ArgumentNullException(nameof(keySelector));
  1800. if (comparer == null)
  1801. throw new ArgumentNullException(nameof(comparer));
  1802. return source.Provider.CreateQuery<TSource>(
  1803. Expression.Call(
  1804. null,
  1805. #if CRIPPLED_REFLECTION
  1806. InfoOf(() => QueryableEx.DistinctUntilChanged<TSource, TKey>(default(IQueryable<TSource>), default(Expression<Func<TSource, TKey>>), default(IEqualityComparer<TKey>))),
  1807. #else
  1808. ((MethodInfo)MethodInfo.GetCurrentMethod()).MakeGenericMethod(typeof(TSource), typeof(TKey)),
  1809. #endif
  1810. source.Expression,
  1811. keySelector,
  1812. Expression.Constant(comparer, typeof(IEqualityComparer<TKey>))
  1813. )
  1814. );
  1815. }
  1816. #pragma warning disable 1591
  1817. [EditorBrowsable(EditorBrowsableState.Never)]
  1818. public static IEnumerable<TSource> DistinctUntilChanged<TSource, TKey>(IEnumerable<TSource> source, Func<TSource, TKey> keySelector, IEqualityComparer<TKey> comparer)
  1819. {
  1820. return EnumerableEx.DistinctUntilChanged(source, keySelector, comparer);
  1821. }
  1822. #pragma warning restore 1591
  1823. /// <summary>
  1824. /// Expands the sequence by recursively applying a selector function.
  1825. /// </summary>
  1826. /// <typeparam name="TSource">Source sequence element type.</typeparam>
  1827. /// <param name="source">Source sequence.</param>
  1828. /// <param name="selector">Selector function to retrieve the next sequence to expand.</param>
  1829. /// <returns>Sequence with results from the recursive expansion of the source sequence.</returns>
  1830. public static IQueryable<TSource> Expand<TSource>(this IQueryable<TSource> source, Expression<Func<TSource, IEnumerable<TSource>>> selector)
  1831. {
  1832. if (source == null)
  1833. throw new ArgumentNullException(nameof(source));
  1834. if (selector == null)
  1835. throw new ArgumentNullException(nameof(selector));
  1836. return source.Provider.CreateQuery<TSource>(
  1837. Expression.Call(
  1838. null,
  1839. #if CRIPPLED_REFLECTION
  1840. InfoOf(() => QueryableEx.Expand<TSource>(default(IQueryable<TSource>), default(Expression<Func<TSource, IEnumerable<TSource>>>))),
  1841. #else
  1842. ((MethodInfo)MethodInfo.GetCurrentMethod()).MakeGenericMethod(typeof(TSource)),
  1843. #endif
  1844. source.Expression,
  1845. selector
  1846. )
  1847. );
  1848. }
  1849. #pragma warning disable 1591
  1850. [EditorBrowsable(EditorBrowsableState.Never)]
  1851. public static IEnumerable<TSource> Expand<TSource>(IEnumerable<TSource> source, Func<TSource, IEnumerable<TSource>> selector)
  1852. {
  1853. return EnumerableEx.Expand(source, selector);
  1854. }
  1855. #pragma warning restore 1591
  1856. /// <summary>
  1857. /// Returns the source sequence prefixed with the specified value.
  1858. /// </summary>
  1859. /// <typeparam name="TSource">Source sequence element type.</typeparam>
  1860. /// <param name="source">Source sequence.</param>
  1861. /// <param name="values">Values to prefix the sequence with.</param>
  1862. /// <returns>Sequence starting with the specified prefix value, followed by the source sequence.</returns>
  1863. public static IQueryable<TSource> StartWith<TSource>(this IQueryable<TSource> source, params TSource[] values)
  1864. {
  1865. if (source == null)
  1866. throw new ArgumentNullException(nameof(source));
  1867. return source.Provider.CreateQuery<TSource>(
  1868. Expression.Call(
  1869. null,
  1870. #if CRIPPLED_REFLECTION
  1871. InfoOf(() => QueryableEx.StartWith<TSource>(default(IQueryable<TSource>), default(TSource[]))),
  1872. #else
  1873. ((MethodInfo)MethodInfo.GetCurrentMethod()).MakeGenericMethod(typeof(TSource)),
  1874. #endif
  1875. source.Expression,
  1876. Expression.Constant(values, typeof(TSource[]))
  1877. )
  1878. );
  1879. }
  1880. #pragma warning disable 1591
  1881. [EditorBrowsable(EditorBrowsableState.Never)]
  1882. public static IEnumerable<TSource> StartWith<TSource>(IEnumerable<TSource> source, params TSource[] values)
  1883. {
  1884. return EnumerableEx.StartWith(source, values);
  1885. }
  1886. #pragma warning restore 1591
  1887. /// <summary>
  1888. /// Generates a sequence of accumulated values by scanning the source sequence and applying an accumulator function.
  1889. /// </summary>
  1890. /// <typeparam name="TSource">Source sequence element type.</typeparam>
  1891. /// <typeparam name="TAccumulate">Accumulation type.</typeparam>
  1892. /// <param name="source">Source sequence.</param>
  1893. /// <param name="seed">Accumulator seed value.</param>
  1894. /// <param name="accumulator">Accumulation function to apply to the current accumulation value and each element of the sequence.</param>
  1895. /// <returns>Sequence with all intermediate accumulation values resulting from scanning the sequence.</returns>
  1896. public static IQueryable<TAccumulate> Scan<TSource, TAccumulate>(this IQueryable<TSource> source, TAccumulate seed, Expression<Func<TAccumulate, TSource, TAccumulate>> accumulator)
  1897. {
  1898. if (source == null)
  1899. throw new ArgumentNullException(nameof(source));
  1900. if (accumulator == null)
  1901. throw new ArgumentNullException(nameof(accumulator));
  1902. return source.Provider.CreateQuery<TAccumulate>(
  1903. Expression.Call(
  1904. null,
  1905. #if CRIPPLED_REFLECTION
  1906. InfoOf(() => QueryableEx.Scan<TSource, TAccumulate>(default(IQueryable<TSource>), default(TAccumulate), default(Expression<Func<TAccumulate, TSource, TAccumulate>>))),
  1907. #else
  1908. ((MethodInfo)MethodInfo.GetCurrentMethod()).MakeGenericMethod(typeof(TSource), typeof(TAccumulate)),
  1909. #endif
  1910. source.Expression,
  1911. Expression.Constant(seed, typeof(TAccumulate)),
  1912. accumulator
  1913. )
  1914. );
  1915. }
  1916. #pragma warning disable 1591
  1917. [EditorBrowsable(EditorBrowsableState.Never)]
  1918. public static IEnumerable<TAccumulate> Scan<TSource, TAccumulate>(IEnumerable<TSource> source, TAccumulate seed, Func<TAccumulate, TSource, TAccumulate> accumulator)
  1919. {
  1920. return EnumerableEx.Scan(source, seed, accumulator);
  1921. }
  1922. #pragma warning restore 1591
  1923. /// <summary>
  1924. /// Generates a sequence of accumulated values by scanning the source sequence and applying an accumulator function.
  1925. /// </summary>
  1926. /// <typeparam name="TSource">Source sequence element type.</typeparam>
  1927. /// <param name="source">Source sequence.</param>
  1928. /// <param name="accumulator">Accumulation function to apply to the current accumulation value and each element of the sequence.</param>
  1929. /// <returns>Sequence with all intermediate accumulation values resulting from scanning the sequence.</returns>
  1930. public static IQueryable<TSource> Scan<TSource>(this IQueryable<TSource> source, Expression<Func<TSource, TSource, TSource>> accumulator)
  1931. {
  1932. if (source == null)
  1933. throw new ArgumentNullException(nameof(source));
  1934. if (accumulator == null)
  1935. throw new ArgumentNullException(nameof(accumulator));
  1936. return source.Provider.CreateQuery<TSource>(
  1937. Expression.Call(
  1938. null,
  1939. #if CRIPPLED_REFLECTION
  1940. InfoOf(() => QueryableEx.Scan<TSource>(default(IQueryable<TSource>), default(Expression<Func<TSource, TSource, TSource>>))),
  1941. #else
  1942. ((MethodInfo)MethodInfo.GetCurrentMethod()).MakeGenericMethod(typeof(TSource)),
  1943. #endif
  1944. source.Expression,
  1945. accumulator
  1946. )
  1947. );
  1948. }
  1949. #pragma warning disable 1591
  1950. [EditorBrowsable(EditorBrowsableState.Never)]
  1951. public static IEnumerable<TSource> Scan<TSource>(IEnumerable<TSource> source, Func<TSource, TSource, TSource> accumulator)
  1952. {
  1953. return EnumerableEx.Scan(source, accumulator);
  1954. }
  1955. #pragma warning restore 1591
  1956. /// <summary>
  1957. /// Returns a specified number of contiguous elements from the end of the sequence.
  1958. /// </summary>
  1959. /// <typeparam name="TSource">Source sequence element type.</typeparam>
  1960. /// <param name="source">Source sequence.</param>
  1961. /// <param name="count">The number of elements to take from the end of the sequence.</param>
  1962. /// <returns>Sequence with the specified number of elements counting from the end of the source sequence.</returns>
  1963. public static IQueryable<TSource> TakeLast<TSource>(this IQueryable<TSource> source, int count)
  1964. {
  1965. if (source == null)
  1966. throw new ArgumentNullException(nameof(source));
  1967. return source.Provider.CreateQuery<TSource>(
  1968. Expression.Call(
  1969. null,
  1970. #if CRIPPLED_REFLECTION
  1971. InfoOf(() => QueryableEx.TakeLast<TSource>(default(IQueryable<TSource>), default(int))),
  1972. #else
  1973. ((MethodInfo)MethodInfo.GetCurrentMethod()).MakeGenericMethod(typeof(TSource)),
  1974. #endif
  1975. source.Expression,
  1976. Expression.Constant(count, typeof(int))
  1977. )
  1978. );
  1979. }
  1980. #pragma warning disable 1591
  1981. [EditorBrowsable(EditorBrowsableState.Never)]
  1982. public static IEnumerable<TSource> TakeLast<TSource>(IEnumerable<TSource> source, int count)
  1983. {
  1984. return EnumerableEx.TakeLast(source, count);
  1985. }
  1986. #pragma warning restore 1591
  1987. /// <summary>
  1988. /// Bypasses a specified number of contiguous elements from the end of the sequence and returns the remaining elements.
  1989. /// </summary>
  1990. /// <typeparam name="TSource">Source sequence element type.</typeparam>
  1991. /// <param name="source">Source sequence.</param>
  1992. /// <param name="count">The number of elements to skip from the end of the sequence before returning the remaining elements.</param>
  1993. /// <returns>Sequence bypassing the specified number of elements counting from the end of the source sequence.</returns>
  1994. public static IQueryable<TSource> SkipLast<TSource>(this IQueryable<TSource> source, int count)
  1995. {
  1996. if (source == null)
  1997. throw new ArgumentNullException(nameof(source));
  1998. return source.Provider.CreateQuery<TSource>(
  1999. Expression.Call(
  2000. null,
  2001. #if CRIPPLED_REFLECTION
  2002. InfoOf(() => QueryableEx.SkipLast<TSource>(default(IQueryable<TSource>), default(int))),
  2003. #else
  2004. ((MethodInfo)MethodInfo.GetCurrentMethod()).MakeGenericMethod(typeof(TSource)),
  2005. #endif
  2006. source.Expression,
  2007. Expression.Constant(count, typeof(int))
  2008. )
  2009. );
  2010. }
  2011. #pragma warning disable 1591
  2012. [EditorBrowsable(EditorBrowsableState.Never)]
  2013. public static IEnumerable<TSource> SkipLast<TSource>(IEnumerable<TSource> source, int count)
  2014. {
  2015. return EnumerableEx.SkipLast(source, count);
  2016. }
  2017. #pragma warning restore 1591
  2018. /// <summary>
  2019. /// Repeats and concatenates the source sequence infinitely.
  2020. /// </summary>
  2021. /// <typeparam name="TSource">Source sequence element type.</typeparam>
  2022. /// <param name="source">Source sequence.</param>
  2023. /// <returns>Sequence obtained by concatenating the source sequence to itself infinitely.</returns>
  2024. public static IQueryable<TSource> Repeat<TSource>(this IQueryable<TSource> source)
  2025. {
  2026. if (source == null)
  2027. throw new ArgumentNullException(nameof(source));
  2028. return source.Provider.CreateQuery<TSource>(
  2029. Expression.Call(
  2030. null,
  2031. #if CRIPPLED_REFLECTION
  2032. InfoOf(() => QueryableEx.Repeat<TSource>(default(IQueryable<TSource>))),
  2033. #else
  2034. ((MethodInfo)MethodInfo.GetCurrentMethod()).MakeGenericMethod(typeof(TSource)),
  2035. #endif
  2036. source.Expression
  2037. )
  2038. );
  2039. }
  2040. #pragma warning disable 1591
  2041. [EditorBrowsable(EditorBrowsableState.Never)]
  2042. public static IEnumerable<TSource> Repeat<TSource>(IEnumerable<TSource> source)
  2043. {
  2044. return EnumerableEx.Repeat(source);
  2045. }
  2046. #pragma warning restore 1591
  2047. /// <summary>
  2048. /// Repeats and concatenates the source sequence the given number of times.
  2049. /// </summary>
  2050. /// <typeparam name="TSource">Source sequence element type.</typeparam>
  2051. /// <param name="source">Source sequence.</param>
  2052. /// <param name="count">Number of times to repeat the source sequence.</param>
  2053. /// <returns>Sequence obtained by concatenating the source sequence to itself the specified number of times.</returns>
  2054. public static IQueryable<TSource> Repeat<TSource>(this IQueryable<TSource> source, int count)
  2055. {
  2056. if (source == null)
  2057. throw new ArgumentNullException(nameof(source));
  2058. return source.Provider.CreateQuery<TSource>(
  2059. Expression.Call(
  2060. null,
  2061. #if CRIPPLED_REFLECTION
  2062. InfoOf(() => QueryableEx.Repeat<TSource>(default(IQueryable<TSource>), default(int))),
  2063. #else
  2064. ((MethodInfo)MethodInfo.GetCurrentMethod()).MakeGenericMethod(typeof(TSource)),
  2065. #endif
  2066. source.Expression,
  2067. Expression.Constant(count, typeof(int))
  2068. )
  2069. );
  2070. }
  2071. #pragma warning disable 1591
  2072. [EditorBrowsable(EditorBrowsableState.Never)]
  2073. public static IEnumerable<TSource> Repeat<TSource>(IEnumerable<TSource> source, int count)
  2074. {
  2075. return EnumerableEx.Repeat(source, count);
  2076. }
  2077. #pragma warning restore 1591
  2078. /// <summary>
  2079. /// Returns a sequence with no elements.
  2080. /// </summary>
  2081. /// <typeparam name="TResult">Result sequence element type.</typeparam>
  2082. /// <param name="provider">Query provider.</param>
  2083. /// <returns>Sequence with no elements.</returns>
  2084. public static IQueryable<TResult> Empty<TResult>(this IQueryProvider provider)
  2085. {
  2086. if (provider == null)
  2087. throw new ArgumentNullException(nameof(provider));
  2088. return provider.CreateQuery<TResult>(
  2089. Expression.Call(
  2090. null,
  2091. #if CRIPPLED_REFLECTION
  2092. InfoOf(() => QueryableEx.Empty<TResult>(default(IQueryProvider))),
  2093. #else
  2094. ((MethodInfo)MethodInfo.GetCurrentMethod()).MakeGenericMethod(typeof(TResult)),
  2095. #endif
  2096. Expression.Constant(provider, typeof(IQueryProvider))
  2097. )
  2098. );
  2099. }
  2100. #pragma warning disable 1591
  2101. [EditorBrowsable(EditorBrowsableState.Never)]
  2102. public static /*!*/IQueryable<TResult> Empty<TResult>()
  2103. {
  2104. return Enumerable.Empty<TResult>().AsQueryable();
  2105. }
  2106. #pragma warning restore 1591
  2107. /// <summary>
  2108. /// Generates a sequence of integral numbers within a specified range.
  2109. /// </summary>
  2110. /// <param name="provider">Query provider.</param>
  2111. /// <param name="start">The value of the first integer in the sequence.</param>
  2112. /// <param name="count">The number of sequential integers to generate.</param>
  2113. /// <returns>Sequence that contains a range of sequential integral numbers.</returns>
  2114. public static IQueryable<int> Range(this IQueryProvider provider, int start, int count)
  2115. {
  2116. if (provider == null)
  2117. throw new ArgumentNullException(nameof(provider));
  2118. return provider.CreateQuery<int>(
  2119. Expression.Call(
  2120. null,
  2121. #if CRIPPLED_REFLECTION
  2122. InfoOf(() => QueryableEx.Range(default(IQueryProvider), default(int), default(int))),
  2123. #else
  2124. (MethodInfo)MethodInfo.GetCurrentMethod(),
  2125. #endif
  2126. Expression.Constant(provider, typeof(IQueryProvider)),
  2127. Expression.Constant(start, typeof(int)),
  2128. Expression.Constant(count, typeof(int))
  2129. )
  2130. );
  2131. }
  2132. #pragma warning disable 1591
  2133. [EditorBrowsable(EditorBrowsableState.Never)]
  2134. public static /*!*/IQueryable<int> Range(int start, int count)
  2135. {
  2136. return Enumerable.Range(start, count).AsQueryable();
  2137. }
  2138. #pragma warning restore 1591
  2139. /// <summary>
  2140. /// Generates a sequence that contains one repeated value.
  2141. /// </summary>
  2142. /// <typeparam name="TResult">Result sequence element type.</typeparam>
  2143. /// <param name="provider">Query provider.</param>
  2144. /// <param name="element">The value to be repeated.</param>
  2145. /// <param name="count">The number of times to repeat the value in the generated sequence.</param>
  2146. /// <returns>Sequence that contains a repeated value.</returns>
  2147. public static IQueryable<TResult> Repeat<TResult>(this IQueryProvider provider, TResult element, int count)
  2148. {
  2149. if (provider == null)
  2150. throw new ArgumentNullException(nameof(provider));
  2151. return provider.CreateQuery<TResult>(
  2152. Expression.Call(
  2153. null,
  2154. #if CRIPPLED_REFLECTION
  2155. InfoOf(() => QueryableEx.Repeat<TResult>(default(IQueryProvider), default(TResult), default(int))),
  2156. #else
  2157. ((MethodInfo)MethodInfo.GetCurrentMethod()).MakeGenericMethod(typeof(TResult)),
  2158. #endif
  2159. Expression.Constant(provider, typeof(IQueryProvider)),
  2160. Expression.Constant(element, typeof(TResult)),
  2161. Expression.Constant(count, typeof(int))
  2162. )
  2163. );
  2164. }
  2165. #pragma warning disable 1591
  2166. [EditorBrowsable(EditorBrowsableState.Never)]
  2167. public static /*!*/IQueryable<TResult> Repeat<TResult>(TResult element, int count)
  2168. {
  2169. return EnumerableEx.Repeat<TResult>(element, count).AsQueryable();
  2170. }
  2171. #pragma warning restore 1591
  2172. /// <summary>
  2173. /// Gets the local Queryable provider.
  2174. /// </summary>
  2175. public static IQueryProvider Provider
  2176. {
  2177. get
  2178. {
  2179. return new QueryProviderShim();
  2180. }
  2181. }
  2182. class QueryProviderShim : IQueryProvider
  2183. {
  2184. public IQueryable<TElement> CreateQuery<TElement>(Expression expression)
  2185. {
  2186. var provider = new TElement[0].AsQueryable().Provider;
  2187. var res = Redir(expression);
  2188. return provider.CreateQuery<TElement>(res);
  2189. }
  2190. public IQueryable CreateQuery(Expression expression)
  2191. {
  2192. return CreateQuery<object>(expression);
  2193. }
  2194. public TResult Execute<TResult>(Expression expression)
  2195. {
  2196. var provider = new TResult[0].AsQueryable().Provider;
  2197. var res = Redir(expression);
  2198. return provider.Execute<TResult>(res);
  2199. }
  2200. public object Execute(Expression expression)
  2201. {
  2202. return Execute<object>(expression);
  2203. }
  2204. private static Expression Redir(Expression expression)
  2205. {
  2206. if (expression is MethodCallExpression mce && mce.Method.DeclaringType == typeof(QueryableEx))
  2207. {
  2208. if (mce.Arguments.Count >= 1 && typeof(IQueryProvider).IsAssignableFrom(mce.Arguments[0].Type))
  2209. {
  2210. if (mce.Arguments[0] is ConstantExpression ce)
  2211. {
  2212. if (ce.Value is QueryProviderShim)
  2213. {
  2214. var targetType = typeof(QueryableEx);
  2215. var method = mce.Method;
  2216. var methods = GetMethods(targetType);
  2217. var arguments = mce.Arguments.Skip(1).ToList();
  2218. //
  2219. // From all the operators with the method's name, find the one that matches all arguments.
  2220. //
  2221. var typeArgs = method.IsGenericMethod ? method.GetGenericArguments() : null;
  2222. var targetMethod = methods[method.Name].FirstOrDefault(candidateMethod => ArgsMatch(candidateMethod, arguments, typeArgs));
  2223. if (targetMethod == null)
  2224. throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, "There is no method '{0}' on type '{1}' that matches the specified arguments", method.Name, targetType.Name));
  2225. //
  2226. // Restore generic arguments.
  2227. //
  2228. if (typeArgs != null)
  2229. targetMethod = targetMethod.MakeGenericMethod(typeArgs);
  2230. //
  2231. // Finally, we need to deal with mismatches on Expression<Func<...>> versus Func<...>.
  2232. //
  2233. var parameters = targetMethod.GetParameters();
  2234. for (int i = 0, n = parameters.Length; i < n; i++)
  2235. {
  2236. arguments[i] = Unquote(arguments[i]);
  2237. }
  2238. //
  2239. // Emit a new call to the discovered target method.
  2240. //
  2241. return Expression.Call(null, targetMethod, arguments);
  2242. }
  2243. }
  2244. }
  2245. }
  2246. return expression;
  2247. }
  2248. private static ILookup<string, MethodInfo> GetMethods(Type type)
  2249. {
  2250. return type.GetMethods(BindingFlags.Static | BindingFlags.Public).ToLookup(m => m.Name);
  2251. }
  2252. private static bool ArgsMatch(MethodInfo method, IList<Expression> arguments, Type[] typeArgs)
  2253. {
  2254. //
  2255. // Number of parameters should match. Notice we've sanitized IQueryProvider "this"
  2256. // parameters first (see Redir).
  2257. //
  2258. var parameters = method.GetParameters();
  2259. if (parameters.Length != arguments.Count)
  2260. return false;
  2261. //
  2262. // Genericity should match too.
  2263. //
  2264. if (!method.IsGenericMethod && typeArgs != null && typeArgs.Length > 0)
  2265. return false;
  2266. //
  2267. // Reconstruct the generic method if needed.
  2268. //
  2269. if (method.IsGenericMethodDefinition)
  2270. {
  2271. if (typeArgs == null)
  2272. return false;
  2273. if (method.GetGenericArguments().Length != typeArgs.Length)
  2274. return false;
  2275. var result = method.MakeGenericMethod(typeArgs);
  2276. parameters = result.GetParameters();
  2277. }
  2278. //
  2279. // Check compatibility for the parameter types.
  2280. //
  2281. for (int i = 0, n = arguments.Count; i < n; i++)
  2282. {
  2283. var parameterType = parameters[i].ParameterType;
  2284. var argument = arguments[i];
  2285. //
  2286. // For operators that take a function (like Where, Select), we'll be faced
  2287. // with a quoted argument and a discrepancy between Expression<Func<...>>
  2288. // and the underlying Func<...>.
  2289. //
  2290. if (!parameterType.IsAssignableFrom(argument.Type))
  2291. {
  2292. argument = Unquote(argument);
  2293. if (!parameterType.IsAssignableFrom(argument.Type))
  2294. return false;
  2295. }
  2296. }
  2297. return true;
  2298. }
  2299. private static Expression Unquote(Expression expression)
  2300. {
  2301. //
  2302. // Get rid of all outer quotes around an expression.
  2303. //
  2304. while (expression.NodeType == ExpressionType.Quote)
  2305. expression = ((UnaryExpression)expression).Operand;
  2306. return expression;
  2307. }
  2308. }
  2309. internal static Expression GetSourceExpression<TSource>(IEnumerable<TSource> source)
  2310. {
  2311. if (source is IQueryable<TSource> q)
  2312. return q.Expression;
  2313. return Expression.Constant(source, typeof(IEnumerable<TSource>));
  2314. }
  2315. internal static Expression GetSourceExpression<TSource>(IEnumerable<TSource>[] sources)
  2316. {
  2317. return Expression.NewArrayInit(
  2318. typeof(IEnumerable<TSource>),
  2319. sources.Select(source => GetSourceExpression(source))
  2320. );
  2321. }
  2322. internal static MethodInfo InfoOf<R>(Expression<Func<R>> f)
  2323. {
  2324. return ((MethodCallExpression)f.Body).Method;
  2325. }
  2326. }
  2327. }