1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545 |
- // Licensed to the .NET Foundation under one or more agreements.
- // The .NET Foundation licenses this file to you under the Apache 2.0 License.
- // See the LICENSE file in the project root for more information.
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Globalization;
- using System.Linq.Expressions;
- using System.Reflection;
- namespace System.Linq
- {
- /// <summary>
- /// Provides a set of additional static methods that allow querying enumerable sequences.
- /// </summary>
- public static class QueryableEx
- {
- /// <summary>
- /// Determines whether an enumerable sequence is empty.
- /// </summary>
- /// <typeparam name="TSource">Source sequence element type.</typeparam>
- /// <param name="source">Source sequence.</param>
- /// <returns>true if the sequence is empty; false otherwise.</returns>
- public static bool IsEmpty<TSource>(this IQueryable<TSource> source)
- {
- if (source == null)
- throw new ArgumentNullException(nameof(source));
- return source.Provider.Execute<bool>(
- Expression.Call(
- null,
- #if CRIPPLED_REFLECTION
- InfoOf(() => QueryableEx.IsEmpty<TSource>(default(IQueryable<TSource>))),
- #else
- ((MethodInfo)MethodInfo.GetCurrentMethod()).MakeGenericMethod(typeof(TSource)),
- #endif
- source.Expression
- )
- );
- }
- #pragma warning disable 1591
- [EditorBrowsable(EditorBrowsableState.Never)]
- public static bool IsEmpty<TSource>(IEnumerable<TSource> source)
- {
- return EnumerableEx.IsEmpty(source);
- }
- #pragma warning restore 1591
- /// <summary>
- /// Returns the minimum value in the enumerable sequence by using the specified comparer to compare values.
- /// </summary>
- /// <typeparam name="TSource">Source sequence element type.</typeparam>
- /// <param name="source">Source sequence.</param>
- /// <param name="comparer">Comparer used to determine the minimum value.</param>
- /// <returns>Minimum value in the sequence.</returns>
- public static TSource Min<TSource>(this IQueryable<TSource> source, IComparer<TSource> comparer)
- {
- if (source == null)
- throw new ArgumentNullException(nameof(source));
- if (comparer == null)
- throw new ArgumentNullException(nameof(comparer));
- return source.Provider.Execute<TSource>(
- Expression.Call(
- null,
- #if CRIPPLED_REFLECTION
- InfoOf(() => QueryableEx.Min<TSource>(default(IQueryable<TSource>), default(IComparer<TSource>))),
- #else
- ((MethodInfo)MethodInfo.GetCurrentMethod()).MakeGenericMethod(typeof(TSource)),
- #endif
- source.Expression,
- Expression.Constant(comparer, typeof(IComparer<TSource>))
- )
- );
- }
- #pragma warning disable 1591
- [EditorBrowsable(EditorBrowsableState.Never)]
- public static TSource Min<TSource>(IEnumerable<TSource> source, IComparer<TSource> comparer)
- {
- return EnumerableEx.Min(source, comparer);
- }
- #pragma warning restore 1591
- /// <summary>
- /// Returns the elements with the minimum key value by using the default comparer to compare key values.
- /// </summary>
- /// <typeparam name="TSource">Source sequence element type.</typeparam>
- /// <typeparam name="TKey">Key type.</typeparam>
- /// <param name="source">Source sequence.</param>
- /// <param name="keySelector">Key selector used to extract the key for each element in the sequence.</param>
- /// <returns>List with the elements that share the same minimum key value.</returns>
- public static IList<TSource> MinBy<TSource, TKey>(this IQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector)
- {
- if (source == null)
- throw new ArgumentNullException(nameof(source));
- if (keySelector == null)
- throw new ArgumentNullException(nameof(keySelector));
- return source.Provider.Execute<IList<TSource>>(
- Expression.Call(
- null,
- #if CRIPPLED_REFLECTION
- InfoOf(() => QueryableEx.MinBy<TSource, TKey>(default(IQueryable<TSource>), default(Expression<Func<TSource, TKey>>))),
- #else
- ((MethodInfo)MethodInfo.GetCurrentMethod()).MakeGenericMethod(typeof(TSource), typeof(TKey)),
- #endif
- source.Expression,
- keySelector
- )
- );
- }
- #pragma warning disable 1591
- [EditorBrowsable(EditorBrowsableState.Never)]
- public static IList<TSource> MinBy<TSource, TKey>(IEnumerable<TSource> source, Func<TSource, TKey> keySelector)
- {
- return EnumerableEx.MinBy(source, keySelector);
- }
- #pragma warning restore 1591
- /// <summary>
- /// Returns the elements with the minimum key value by using the specified comparer to compare key values.
- /// </summary>
- /// <typeparam name="TSource">Source sequence element type.</typeparam>
- /// <typeparam name="TKey">Key type.</typeparam>
- /// <param name="source">Source sequence.</param>
- /// <param name="keySelector">Key selector used to extract the key for each element in the sequence.</param>
- /// <param name="comparer">Comparer used to determine the minimum key value.</param>
- /// <returns>List with the elements that share the same minimum key value.</returns>
- public static IList<TSource> MinBy<TSource, TKey>(this IQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector, IComparer<TKey> comparer)
- {
- if (source == null)
- throw new ArgumentNullException(nameof(source));
- if (keySelector == null)
- throw new ArgumentNullException(nameof(keySelector));
- if (comparer == null)
- throw new ArgumentNullException(nameof(comparer));
- return source.Provider.Execute<IList<TSource>>(
- Expression.Call(
- null,
- #if CRIPPLED_REFLECTION
- InfoOf(() => QueryableEx.MinBy<TSource, TKey>(default(IQueryable<TSource>), default(Expression<Func<TSource, TKey>>), default(IComparer<TKey>))),
- #else
- ((MethodInfo)MethodInfo.GetCurrentMethod()).MakeGenericMethod(typeof(TSource), typeof(TKey)),
- #endif
- source.Expression,
- keySelector,
- Expression.Constant(comparer, typeof(IComparer<TKey>))
- )
- );
- }
- #pragma warning disable 1591
- [EditorBrowsable(EditorBrowsableState.Never)]
- public static IList<TSource> MinBy<TSource, TKey>(IEnumerable<TSource> source, Func<TSource, TKey> keySelector, IComparer<TKey> comparer)
- {
- return EnumerableEx.MinBy(source, keySelector, comparer);
- }
- #pragma warning restore 1591
- /// <summary>
- /// Returns the maximum value in the enumerable sequence by using the specified comparer to compare values.
- /// </summary>
- /// <typeparam name="TSource">Source sequence element type.</typeparam>
- /// <param name="source">Source sequence.</param>
- /// <param name="comparer">Comparer used to determine the maximum value.</param>
- /// <returns>Maximum value in the sequence.</returns>
- public static TSource Max<TSource>(this IQueryable<TSource> source, IComparer<TSource> comparer)
- {
- if (source == null)
- throw new ArgumentNullException(nameof(source));
- if (comparer == null)
- throw new ArgumentNullException(nameof(comparer));
- return source.Provider.Execute<TSource>(
- Expression.Call(
- null,
- #if CRIPPLED_REFLECTION
- InfoOf(() => QueryableEx.Max<TSource>(default(IQueryable<TSource>), default(IComparer<TSource>))),
- #else
- ((MethodInfo)MethodInfo.GetCurrentMethod()).MakeGenericMethod(typeof(TSource)),
- #endif
- source.Expression,
- Expression.Constant(comparer, typeof(IComparer<TSource>))
- )
- );
- }
- #pragma warning disable 1591
- [EditorBrowsable(EditorBrowsableState.Never)]
- public static TSource Max<TSource>(IEnumerable<TSource> source, IComparer<TSource> comparer)
- {
- return EnumerableEx.Max(source, comparer);
- }
- #pragma warning restore 1591
- /// <summary>
- /// Returns the elements with the maximum key value by using the default comparer to compare key values.
- /// </summary>
- /// <typeparam name="TSource">Source sequence element type.</typeparam>
- /// <typeparam name="TKey">Key type.</typeparam>
- /// <param name="source">Source sequence.</param>
- /// <param name="keySelector">Key selector used to extract the key for each element in the sequence.</param>
- /// <returns>List with the elements that share the same maximum key value.</returns>
- public static IList<TSource> MaxBy<TSource, TKey>(this IQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector)
- {
- if (source == null)
- throw new ArgumentNullException(nameof(source));
- if (keySelector == null)
- throw new ArgumentNullException(nameof(keySelector));
- return source.Provider.Execute<IList<TSource>>(
- Expression.Call(
- null,
- #if CRIPPLED_REFLECTION
- InfoOf(() => QueryableEx.MaxBy<TSource, TKey>(default(IQueryable<TSource>), default(Expression<Func<TSource, TKey>>))),
- #else
- ((MethodInfo)MethodInfo.GetCurrentMethod()).MakeGenericMethod(typeof(TSource), typeof(TKey)),
- #endif
- source.Expression,
- keySelector
- )
- );
- }
- #pragma warning disable 1591
- [EditorBrowsable(EditorBrowsableState.Never)]
- public static IList<TSource> MaxBy<TSource, TKey>(IEnumerable<TSource> source, Func<TSource, TKey> keySelector)
- {
- return EnumerableEx.MaxBy(source, keySelector);
- }
- #pragma warning restore 1591
- /// <summary>
- /// Returns the elements with the minimum key value by using the specified comparer to compare key values.
- /// </summary>
- /// <typeparam name="TSource">Source sequence element type.</typeparam>
- /// <typeparam name="TKey">Key type.</typeparam>
- /// <param name="source">Source sequence.</param>
- /// <param name="keySelector">Key selector used to extract the key for each element in the sequence.</param>
- /// <param name="comparer">Comparer used to determine the maximum key value.</param>
- /// <returns>List with the elements that share the same maximum key value.</returns>
- public static IList<TSource> MaxBy<TSource, TKey>(this IQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector, IComparer<TKey> comparer)
- {
- if (source == null)
- throw new ArgumentNullException(nameof(source));
- if (keySelector == null)
- throw new ArgumentNullException(nameof(keySelector));
- if (comparer == null)
- throw new ArgumentNullException(nameof(comparer));
- return source.Provider.Execute<IList<TSource>>(
- Expression.Call(
- null,
- #if CRIPPLED_REFLECTION
- InfoOf(() => QueryableEx.MaxBy<TSource, TKey>(default(IQueryable<TSource>), default(Expression<Func<TSource, TKey>>), default(IComparer<TKey>))),
- #else
- ((MethodInfo)MethodInfo.GetCurrentMethod()).MakeGenericMethod(typeof(TSource), typeof(TKey)),
- #endif
- source.Expression,
- keySelector,
- Expression.Constant(comparer, typeof(IComparer<TKey>))
- )
- );
- }
- #pragma warning disable 1591
- [EditorBrowsable(EditorBrowsableState.Never)]
- public static IList<TSource> MaxBy<TSource, TKey>(IEnumerable<TSource> source, Func<TSource, TKey> keySelector, IComparer<TKey> comparer)
- {
- return EnumerableEx.MaxBy(source, keySelector, comparer);
- }
- #pragma warning restore 1591
- /// <summary>
- /// Shares the source sequence within a selector function where each enumerator can fetch the next element from the source sequence.
- /// </summary>
- /// <typeparam name="TSource">Source sequence element type.</typeparam>
- /// <typeparam name="TResult">Result sequence element type.</typeparam>
- /// <param name="source">Source sequence.</param>
- /// <param name="selector">Selector function with shared access to the source sequence for each enumerator.</param>
- /// <returns>Sequence resulting from applying the selector function to the shared view over the source sequence.</returns>
- public static IQueryable<TResult> Share<TSource, TResult>(this IQueryable<TSource> source, Expression<Func<IEnumerable<TSource>, IEnumerable<TResult>>> selector)
- {
- if (source == null)
- throw new ArgumentNullException(nameof(source));
- if (selector == null)
- throw new ArgumentNullException(nameof(selector));
- return source.Provider.CreateQuery<TResult>(
- Expression.Call(
- null,
- #if CRIPPLED_REFLECTION
- InfoOf(() => QueryableEx.Share<TSource, TResult>(default(IQueryable<TSource>), default(Expression<Func<IEnumerable<TSource>, IEnumerable<TResult>>>))),
- #else
- ((MethodInfo)MethodInfo.GetCurrentMethod()).MakeGenericMethod(typeof(TSource), typeof(TResult)),
- #endif
- source.Expression,
- selector
- )
- );
- }
- #pragma warning disable 1591
- [EditorBrowsable(EditorBrowsableState.Never)]
- public static IEnumerable<TResult> Share<TSource, TResult>(IEnumerable<TSource> source, Func<IEnumerable<TSource>, IEnumerable<TResult>> selector)
- {
- return EnumerableEx.Share(source, selector);
- }
- #pragma warning restore 1591
- /// <summary>
- /// Publishes the source sequence within a selector function where each enumerator can obtain a view over a tail of the source sequence.
- /// </summary>
- /// <typeparam name="TSource">Source sequence element type.</typeparam>
- /// <typeparam name="TResult">Result sequence element type.</typeparam>
- /// <param name="source">Source sequence.</param>
- /// <param name="selector">Selector function with published access to the source sequence for each enumerator.</param>
- /// <returns>Sequence resulting from applying the selector function to the published view over the source sequence.</returns>
- public static IQueryable<TResult> Publish<TSource, TResult>(this IQueryable<TSource> source, Expression<Func<IEnumerable<TSource>, IEnumerable<TResult>>> selector)
- {
- if (source == null)
- throw new ArgumentNullException(nameof(source));
- if (selector == null)
- throw new ArgumentNullException(nameof(selector));
- return source.Provider.CreateQuery<TResult>(
- Expression.Call(
- null,
- #if CRIPPLED_REFLECTION
- InfoOf(() => QueryableEx.Publish<TSource, TResult>(default(IQueryable<TSource>), default(Expression<Func<IEnumerable<TSource>, IEnumerable<TResult>>>))),
- #else
- ((MethodInfo)MethodInfo.GetCurrentMethod()).MakeGenericMethod(typeof(TSource), typeof(TResult)),
- #endif
- source.Expression,
- selector
- )
- );
- }
- #pragma warning disable 1591
- [EditorBrowsable(EditorBrowsableState.Never)]
- public static IEnumerable<TResult> Publish<TSource, TResult>(IEnumerable<TSource> source, Func<IEnumerable<TSource>, IEnumerable<TResult>> selector)
- {
- return EnumerableEx.Publish(source, selector);
- }
- #pragma warning restore 1591
- /// <summary>
- /// 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.
- /// </summary>
- /// <typeparam name="TSource">Source sequence element type.</typeparam>
- /// <typeparam name="TResult">Result sequence element type.</typeparam>
- /// <param name="source">Source sequence.</param>
- /// <param name="selector">Selector function with memoized access to the source sequence for each enumerator.</param>
- /// <returns>Sequence resulting from applying the selector function to the memoized view over the source sequence.</returns>
- public static IQueryable<TResult> Memoize<TSource, TResult>(this IQueryable<TSource> source, Expression<Func<IEnumerable<TSource>, IEnumerable<TResult>>> selector)
- {
- if (source == null)
- throw new ArgumentNullException(nameof(source));
- if (selector == null)
- throw new ArgumentNullException(nameof(selector));
- return source.Provider.CreateQuery<TResult>(
- Expression.Call(
- null,
- #if CRIPPLED_REFLECTION
- InfoOf(() => QueryableEx.Memoize<TSource, TResult>(default(IQueryable<TSource>), default(Expression<Func<IEnumerable<TSource>, IEnumerable<TResult>>>))),
- #else
- ((MethodInfo)MethodInfo.GetCurrentMethod()).MakeGenericMethod(typeof(TSource), typeof(TResult)),
- #endif
- source.Expression,
- selector
- )
- );
- }
- #pragma warning disable 1591
- [EditorBrowsable(EditorBrowsableState.Never)]
- public static IEnumerable<TResult> Memoize<TSource, TResult>(IEnumerable<TSource> source, Func<IEnumerable<TSource>, IEnumerable<TResult>> selector)
- {
- return EnumerableEx.Memoize(source, selector);
- }
- #pragma warning restore 1591
- /// <summary>
- /// 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.
- /// </summary>
- /// <typeparam name="TSource">Source sequence element type.</typeparam>
- /// <typeparam name="TResult">Result sequence element type.</typeparam>
- /// <param name="source">Source sequence.</param>
- /// <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>
- /// <param name="selector">Selector function with memoized access to the source sequence for a specified number of enumerators.</param>
- /// <returns>Sequence resulting from applying the selector function to the memoized view over the source sequence.</returns>
- public static IQueryable<TResult> Memoize<TSource, TResult>(this IQueryable<TSource> source, int readerCount, Expression<Func<IEnumerable<TSource>, IEnumerable<TResult>>> selector)
- {
- if (source == null)
- throw new ArgumentNullException(nameof(source));
- if (selector == null)
- throw new ArgumentNullException(nameof(selector));
- return source.Provider.CreateQuery<TResult>(
- Expression.Call(
- null,
- #if CRIPPLED_REFLECTION
- InfoOf(() => QueryableEx.Memoize<TSource, TResult>(default(IQueryable<TSource>), default(int), default(Expression<Func<IEnumerable<TSource>, IEnumerable<TResult>>>))),
- #else
- ((MethodInfo)MethodInfo.GetCurrentMethod()).MakeGenericMethod(typeof(TSource), typeof(TResult)),
- #endif
- source.Expression,
- Expression.Constant(readerCount, typeof(int)),
- selector
- )
- );
- }
- #pragma warning disable 1591
- [EditorBrowsable(EditorBrowsableState.Never)]
- public static IEnumerable<TResult> Memoize<TSource, TResult>(IEnumerable<TSource> source, int readerCount, Func<IEnumerable<TSource>, IEnumerable<TResult>> selector)
- {
- return EnumerableEx.Memoize(source, readerCount, selector);
- }
- #pragma warning restore 1591
- /// <summary>
- /// Creates an enumerable sequence based on an enumerator factory function.
- /// </summary>
- /// <typeparam name="TResult">Result sequence element type.</typeparam>
- /// <param name="provider">Query provider.</param>
- /// <param name="getEnumerator">Enumerator factory function.</param>
- /// <returns>Sequence that will invoke the enumerator factory upon a call to GetEnumerator.</returns>
- public static IQueryable<TResult> Create<TResult>(this IQueryProvider provider, Expression<Func<IEnumerator<TResult>>> getEnumerator)
- {
- if (provider == null)
- throw new ArgumentNullException(nameof(provider));
- if (getEnumerator == null)
- throw new ArgumentNullException(nameof(getEnumerator));
- return provider.CreateQuery<TResult>(
- Expression.Call(
- null,
- #if CRIPPLED_REFLECTION
- InfoOf(() => QueryableEx.Create<TResult>(default(IQueryProvider), default(Expression<Func<IEnumerator<TResult>>>))),
- #else
- ((MethodInfo)MethodInfo.GetCurrentMethod()).MakeGenericMethod(typeof(TResult)),
- #endif
- Expression.Constant(provider, typeof(IQueryProvider)),
- getEnumerator
- )
- );
- }
- #pragma warning disable 1591
- [EditorBrowsable(EditorBrowsableState.Never)]
- public static IEnumerable<TResult> Create<TResult>(Func<IEnumerator<TResult>> getEnumerator)
- {
- return EnumerableEx.Create(getEnumerator);
- }
- #pragma warning restore 1591
- /// <summary>
- /// Returns a sequence with a single element.
- /// </summary>
- /// <typeparam name="TResult">Result sequence element type.</typeparam>
- /// <param name="provider">Query provider.</param>
- /// <param name="value">Single element of the resulting sequence.</param>
- /// <returns>Sequence with a single element.</returns>
- public static IQueryable<TResult> Return<TResult>(this IQueryProvider provider, TResult value)
- {
- if (provider == null)
- throw new ArgumentNullException(nameof(provider));
- return provider.CreateQuery<TResult>(
- Expression.Call(
- null,
- #if CRIPPLED_REFLECTION
- InfoOf(() => QueryableEx.Return<TResult>(default(IQueryProvider), default(TResult))),
- #else
- ((MethodInfo)MethodInfo.GetCurrentMethod()).MakeGenericMethod(typeof(TResult)),
- #endif
- Expression.Constant(provider, typeof(IQueryProvider)),
- Expression.Constant(value, typeof(TResult))
- )
- );
- }
- #pragma warning disable 1591
- [EditorBrowsable(EditorBrowsableState.Never)]
- public static /*!*/IQueryable<TResult> Return<TResult>(TResult value)
- {
- return EnumerableEx.Return(value).AsQueryable();
- }
- #pragma warning restore 1591
- /// <summary>
- /// Returns a sequence that throws an exception upon enumeration.
- /// </summary>
- /// <typeparam name="TResult">Result sequence element type.</typeparam>
- /// <param name="provider">Query provider.</param>
- /// <param name="exception">Exception to throw upon enumerating the resulting sequence.</param>
- /// <returns>Sequence that throws the specified exception upon enumeration.</returns>
- public static IQueryable<TResult> Throw<TResult>(this IQueryProvider provider, Exception exception)
- {
- if (provider == null)
- throw new ArgumentNullException(nameof(provider));
- if (exception == null)
- throw new ArgumentNullException(nameof(exception));
- return provider.CreateQuery<TResult>(
- Expression.Call(
- null,
- #if CRIPPLED_REFLECTION
- InfoOf(() => QueryableEx.Throw<TResult>(default(IQueryProvider), default(Exception))),
- #else
- ((MethodInfo)MethodInfo.GetCurrentMethod()).MakeGenericMethod(typeof(TResult)),
- #endif
- Expression.Constant(provider, typeof(IQueryProvider)),
- Expression.Constant(exception, typeof(Exception))
- )
- );
- }
- #pragma warning disable 1591
- [EditorBrowsable(EditorBrowsableState.Never)]
- public static /*!*/IQueryable<TResult> Throw<TResult>(Exception exception)
- {
- return EnumerableEx.Throw<TResult>(exception).AsQueryable();
- }
- #pragma warning restore 1591
- /// <summary>
- /// Creates an enumerable sequence based on an enumerable factory function.
- /// </summary>
- /// <typeparam name="TResult">Result sequence element type.</typeparam>
- /// <param name="provider">Query provider.</param>
- /// <param name="enumerableFactory">Enumerable factory function.</param>
- /// <returns>Sequence that will invoke the enumerable factory upon a call to GetEnumerator.</returns>
- public static IQueryable<TResult> Defer<TResult>(this IQueryProvider provider, Expression<Func<IEnumerable<TResult>>> enumerableFactory)
- {
- if (provider == null)
- throw new ArgumentNullException(nameof(provider));
- if (enumerableFactory == null)
- throw new ArgumentNullException(nameof(enumerableFactory));
- return provider.CreateQuery<TResult>(
- Expression.Call(
- null,
- #if CRIPPLED_REFLECTION
- InfoOf(() => QueryableEx.Defer<TResult>(default(IQueryProvider), default(Expression<Func<IEnumerable<TResult>>>))),
- #else
- ((MethodInfo)MethodInfo.GetCurrentMethod()).MakeGenericMethod(typeof(TResult)),
- #endif
- Expression.Constant(provider, typeof(IQueryProvider)),
- enumerableFactory
- )
- );
- }
- #pragma warning disable 1591
- [EditorBrowsable(EditorBrowsableState.Never)]
- public static /*!*/IQueryable<TResult> Defer<TResult>(Func<IEnumerable<TResult>> enumerableFactory)
- {
- return EnumerableEx.Defer(enumerableFactory).AsQueryable();
- }
- #pragma warning restore 1591
- /// <summary>
- /// Generates a sequence by mimicking a for loop.
- /// </summary>
- /// <typeparam name="TState">State type.</typeparam>
- /// <typeparam name="TResult">Result sequence element type.</typeparam>
- /// <param name="provider">Query provider.</param>
- /// <param name="initialState">Initial state of the generator loop.</param>
- /// <param name="condition">Loop condition.</param>
- /// <param name="iterate">State update function to run after every iteration of the generator loop.</param>
- /// <param name="resultSelector">Result selector to compute resulting sequence elements.</param>
- /// <returns>Sequence obtained by running the generator loop, yielding computed elements.</returns>
- 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)
- {
- if (provider == null)
- throw new ArgumentNullException(nameof(provider));
- if (condition == null)
- throw new ArgumentNullException(nameof(condition));
- if (iterate == null)
- throw new ArgumentNullException(nameof(iterate));
- if (resultSelector == null)
- throw new ArgumentNullException(nameof(resultSelector));
- return provider.CreateQuery<TResult>(
- Expression.Call(
- null,
- #if CRIPPLED_REFLECTION
- InfoOf(() => QueryableEx.Generate<TState, TResult>(default(IQueryProvider), default(TState), default(Expression<Func<TState, bool>>), default(Expression<Func<TState, TState>>), default(Expression<Func<TState, TResult>>))),
- #else
- ((MethodInfo)MethodInfo.GetCurrentMethod()).MakeGenericMethod(typeof(TState), typeof(TResult)),
- #endif
- Expression.Constant(provider, typeof(IQueryProvider)),
- Expression.Constant(initialState),
- condition,
- iterate,
- resultSelector
- )
- );
- }
- #pragma warning disable 1591
- [EditorBrowsable(EditorBrowsableState.Never)]
- public static /*!*/IQueryable<TResult> Generate<TState, TResult>(TState initialState, Func<TState, bool> condition, Func<TState, TState> iterate, Func<TState, TResult> resultSelector)
- {
- return EnumerableEx.Generate(initialState, condition, iterate, resultSelector).AsQueryable();
- }
- #pragma warning restore 1591
- /// <summary>
- /// Generates a sequence that's dependent on a resource object whose lifetime is determined by the sequence usage duration.
- /// </summary>
- /// <typeparam name="TSource">Source element type.</typeparam>
- /// <typeparam name="TResource">Resource type.</typeparam>
- /// <param name="provider">Query provider.</param>
- /// <param name="resourceFactory">Resource factory function.</param>
- /// <param name="enumerableFactory">Enumerable factory function, having access to the obtained resource.</param>
- /// <returns>Sequence whose use controls the lifetime of the associated obtained resource.</returns>
- public static IQueryable<TSource> Using<TSource, TResource>(this IQueryProvider provider, Expression<Func<TResource>> resourceFactory, Expression<Func<TResource, IEnumerable<TSource>>> enumerableFactory) where TResource : IDisposable
- {
- if (provider == null)
- throw new ArgumentNullException(nameof(provider));
- if (resourceFactory == null)
- throw new ArgumentNullException(nameof(resourceFactory));
- if (enumerableFactory == null)
- throw new ArgumentNullException(nameof(enumerableFactory));
- return provider.CreateQuery<TSource>(
- Expression.Call(
- null,
- #if CRIPPLED_REFLECTION
- InfoOf(() => QueryableEx.Using<TSource, TResource>(default(IQueryProvider), default(Expression<Func<TResource>>), default(Expression<Func<TResource, IEnumerable<TSource>>>))),
- #else
- ((MethodInfo)MethodInfo.GetCurrentMethod()).MakeGenericMethod(typeof(TSource), typeof(TResource)),
- #endif
- Expression.Constant(provider, typeof(IQueryProvider)),
- resourceFactory,
- enumerableFactory
- )
- );
- }
- #pragma warning disable 1591
- [EditorBrowsable(EditorBrowsableState.Never)]
- public static /*!*/IQueryable<TSource> Using<TSource, TResource>(Func<TResource> resourceFactory, Func<TResource, IEnumerable<TSource>> enumerableFactory) where TResource : IDisposable
- {
- return EnumerableEx.Using(resourceFactory, enumerableFactory).AsQueryable();
- }
- #pragma warning restore 1591
- /// <summary>
- /// Generates a sequence by repeating the given value infinitely.
- /// </summary>
- /// <typeparam name="TResult">Result sequence element type.</typeparam>
- /// <param name="provider">Query provider.</param>
- /// <param name="value">Value to repreat in the resulting sequence.</param>
- /// <returns>Sequence repeating the given value infinitely.</returns>
- public static IEnumerable<TResult> Repeat<TResult>(this IQueryProvider provider, TResult value)
- {
- if (provider == null)
- throw new ArgumentNullException(nameof(provider));
- return provider.CreateQuery<TResult>(
- Expression.Call(
- null,
- #if CRIPPLED_REFLECTION
- InfoOf(() => QueryableEx.Repeat<TResult>(default(IQueryProvider), default(TResult))),
- #else
- ((MethodInfo)MethodInfo.GetCurrentMethod()).MakeGenericMethod(typeof(TResult)),
- #endif
- Expression.Constant(provider, typeof(IQueryProvider)),
- Expression.Constant(value, typeof(TResult))
- )
- );
- }
- #pragma warning disable 1591
- [EditorBrowsable(EditorBrowsableState.Never)]
- public static /*!*/IQueryable<TResult> Repeat<TResult>(TResult value)
- {
- return EnumerableEx.Repeat(value).AsQueryable();
- }
- #pragma warning restore 1591
- /// <summary>
- /// 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.
- /// </summary>
- /// <typeparam name="TSource">Source sequence element type.</typeparam>
- /// <typeparam name="TException">Exception type to catch.</typeparam>
- /// <param name="source">Source sequence.</param>
- /// <param name="handler">Handler to invoke when an exception of the specified type occurs.</param>
- /// <returns>Source sequence, concatenated with an exception handler result sequence in case of an error.</returns>
- public static IQueryable<TSource> Catch<TSource, TException>(this IQueryable<TSource> source, Expression<Func<TException, IEnumerable<TSource>>> handler)
- where TException : Exception
- {
- if (source == null)
- throw new ArgumentNullException(nameof(source));
- if (handler == null)
- throw new ArgumentNullException(nameof(handler));
- return source.Provider.CreateQuery<TSource>(
- Expression.Call(
- null,
- #if CRIPPLED_REFLECTION
- InfoOf(() => QueryableEx.Catch<TSource, TException>(default(IQueryable<TSource>), default(Expression<Func<TException, IEnumerable<TSource>>>))),
- #else
- ((MethodInfo)MethodInfo.GetCurrentMethod()).MakeGenericMethod(typeof(TSource), typeof(TException)),
- #endif
- source.Expression,
- handler
- )
- );
- }
- #pragma warning disable 1591
- [EditorBrowsable(EditorBrowsableState.Never)]
- public static IEnumerable<TSource> Catch<TSource, TException>(IEnumerable<TSource> source, Func<TException, IEnumerable<TSource>> handler)
- where TException : Exception
- {
- return EnumerableEx.Catch(source, handler);
- }
- #pragma warning restore 1591
- /// <summary>
- /// Creates a sequence by concatenating source sequences until a source sequence completes successfully.
- /// </summary>
- /// <typeparam name="TSource">Source sequence element type.</typeparam>
- /// <param name="sources">Source sequences.</param>
- /// <returns>Sequence that continues to concatenate source sequences while errors occur.</returns>
- public static IQueryable<TSource> Catch<TSource>(this IQueryable<IEnumerable<TSource>> sources)
- {
- if (sources == null)
- throw new ArgumentNullException(nameof(sources));
- return sources.Provider.CreateQuery<TSource>(
- Expression.Call(
- null,
- #if CRIPPLED_REFLECTION
- InfoOf(() => QueryableEx.Catch<TSource>(default(IQueryable<IEnumerable<TSource>>))),
- #else
- ((MethodInfo)MethodInfo.GetCurrentMethod()).MakeGenericMethod(typeof(TSource)),
- #endif
- sources.Expression
- )
- );
- }
- #pragma warning disable 1591
- [EditorBrowsable(EditorBrowsableState.Never)]
- public static IEnumerable<TSource> Catch<TSource>(IEnumerable<IEnumerable<TSource>> sources)
- {
- return EnumerableEx.Catch(sources);
- }
- #pragma warning restore 1591
- /// <summary>
- /// Creates a sequence by concatenating source sequences until a source sequence completes successfully.
- /// </summary>
- /// <typeparam name="TSource">Source sequence element type.</typeparam>
- /// <param name="provider">Query provider.</param>
- /// <param name="sources">Source sequences.</param>
- /// <returns>Sequence that continues to concatenate source sequences while errors occur.</returns>
- public static IQueryable<TSource> Catch<TSource>(this IQueryProvider provider, params IEnumerable<TSource>[] sources)
- {
- if (provider == null)
- throw new ArgumentNullException(nameof(provider));
- if (sources == null)
- throw new ArgumentNullException(nameof(sources));
- return provider.CreateQuery<TSource>(
- Expression.Call(
- null,
- #if CRIPPLED_REFLECTION
- InfoOf(() => QueryableEx.Catch<TSource>(default(IQueryProvider), default(IEnumerable<TSource>[]))),
- #else
- ((MethodInfo)MethodInfo.GetCurrentMethod()).MakeGenericMethod(typeof(TSource)),
- #endif
- Expression.Constant(provider, typeof(IQueryProvider)),
- GetSourceExpression(sources)
- )
- );
- }
- #pragma warning disable 1591
- [EditorBrowsable(EditorBrowsableState.Never)]
- public static /*!*/IQueryable<TSource> Catch<TSource>(params IEnumerable<TSource>[] sources)
- {
- return EnumerableEx.Catch(sources).AsQueryable();
- }
- #pragma warning restore 1591
- /// <summary>
- /// Creates a sequence that returns the elements of the first sequence, switching to the second in case of an error.
- /// </summary>
- /// <typeparam name="TSource">Source sequence element type.</typeparam>
- /// <param name="first">First sequence.</param>
- /// <param name="second">Second sequence, concatenated to the result in case the first sequence completes exceptionally.</param>
- /// <returns>The first sequence, followed by the second sequence in case an error is produced.</returns>
- public static IQueryable<TSource> Catch<TSource>(this IQueryable<TSource> first, IEnumerable<TSource> second)
- {
- if (first == null)
- throw new ArgumentNullException(nameof(first));
- if (second == null)
- throw new ArgumentNullException(nameof(second));
- return first.Provider.CreateQuery<TSource>(
- Expression.Call(
- null,
- #if CRIPPLED_REFLECTION
- InfoOf(() => QueryableEx.Catch<TSource>(default(IQueryable<TSource>), default(IEnumerable<TSource>))),
- #else
- ((MethodInfo)MethodInfo.GetCurrentMethod()).MakeGenericMethod(typeof(TSource)),
- #endif
- first.Expression,
- GetSourceExpression(second)
- )
- );
- }
- #pragma warning disable 1591
- [EditorBrowsable(EditorBrowsableState.Never)]
- public static IEnumerable<TSource> Catch<TSource>(IEnumerable<TSource> first, IEnumerable<TSource> second)
- {
- return EnumerableEx.Catch(first, second);
- }
- #pragma warning restore 1591
- /// <summary>
- /// Creates a sequence whose termination or disposal of an enumerator causes a finally action to be executed.
- /// </summary>
- /// <typeparam name="TSource">Source sequence element type.</typeparam>
- /// <param name="source">Source sequence.</param>
- /// <param name="finallyAction">Action to run upon termination of the sequence, or when an enumerator is disposed.</param>
- /// <returns>Source sequence with guarantees on the invocation of the finally action.</returns>
- public static IQueryable<TSource> Finally<TSource>(this IQueryable<TSource> source, Expression<Action> finallyAction)
- {
- if (source == null)
- throw new ArgumentNullException(nameof(source));
- if (finallyAction == null)
- throw new ArgumentNullException(nameof(finallyAction));
- return source.Provider.CreateQuery<TSource>(
- Expression.Call(
- null,
- #if CRIPPLED_REFLECTION
- InfoOf(() => QueryableEx.Finally<TSource>(default(IQueryable<TSource>), default(Expression<Action>))),
- #else
- ((MethodInfo)MethodInfo.GetCurrentMethod()).MakeGenericMethod(typeof(TSource)),
- #endif
- source.Expression,
- finallyAction
- )
- );
- }
- #pragma warning disable 1591
- [EditorBrowsable(EditorBrowsableState.Never)]
- public static IEnumerable<TSource> Finally<TSource>(IEnumerable<TSource> source, Action finallyAction)
- {
- return EnumerableEx.Finally(source, finallyAction);
- }
- #pragma warning restore 1591
- /// <summary>
- /// Creates a sequence that concatenates both given sequences, regardless of whether an error occurs.
- /// </summary>
- /// <typeparam name="TSource">Source sequence element type.</typeparam>
- /// <param name="first">First sequence.</param>
- /// <param name="second">Second sequence.</param>
- /// <returns>Sequence concatenating the elements of both sequences, ignoring errors.</returns>
- public static IQueryable<TSource> OnErrorResumeNext<TSource>(this IQueryable<TSource> first, IEnumerable<TSource> second)
- {
- if (first == null)
- throw new ArgumentNullException(nameof(first));
- if (second == null)
- throw new ArgumentNullException(nameof(second));
- return first.Provider.CreateQuery<TSource>(
- Expression.Call(
- null,
- #if CRIPPLED_REFLECTION
- InfoOf(() => QueryableEx.OnErrorResumeNext<TSource>(default(IQueryable<TSource>), default(IEnumerable<TSource>))),
- #else
- ((MethodInfo)MethodInfo.GetCurrentMethod()).MakeGenericMethod(typeof(TSource)),
- #endif
- first.Expression,
- GetSourceExpression(second)
- )
- );
- }
- #pragma warning disable 1591
- [EditorBrowsable(EditorBrowsableState.Never)]
- public static IEnumerable<TSource> OnErrorResumeNext<TSource>(IEnumerable<TSource> first, IEnumerable<TSource> second)
- {
- return EnumerableEx.OnErrorResumeNext(first, second);
- }
- #pragma warning restore 1591
- /// <summary>
- /// Creates a sequence that concatenates the given sequences, regardless of whether an error occurs in any of the sequences.
- /// </summary>
- /// <typeparam name="TSource">Source sequence element type.</typeparam>
- /// <param name="provider">Query provider.</param>
- /// <param name="sources">Source sequences.</param>
- /// <returns>Sequence concatenating the elements of the given sequences, ignoring errors.</returns>
- public static IEnumerable<TSource> OnErrorResumeNext<TSource>(this IQueryProvider provider, params IEnumerable<TSource>[] sources)
- {
- if (provider == null)
- throw new ArgumentNullException(nameof(provider));
- if (sources == null)
- throw new ArgumentNullException(nameof(sources));
- return provider.CreateQuery<TSource>(
- Expression.Call(
- null,
- #if CRIPPLED_REFLECTION
- InfoOf(() => QueryableEx.OnErrorResumeNext<TSource>(default(IQueryProvider), default(IEnumerable<TSource>[]))),
- #else
- ((MethodInfo)MethodInfo.GetCurrentMethod()).MakeGenericMethod(typeof(TSource)),
- #endif
- Expression.Constant(provider, typeof(IQueryProvider)),
- GetSourceExpression(sources)
- )
- );
- }
- #pragma warning disable 1591
- [EditorBrowsable(EditorBrowsableState.Never)]
- public static /*!*/IQueryable<TSource> OnErrorResumeNext<TSource>(params IEnumerable<TSource>[] sources)
- {
- return EnumerableEx.OnErrorResumeNext(sources).AsQueryable();
- }
- #pragma warning restore 1591
- /// <summary>
- /// Creates a sequence that concatenates the given sequences, regardless of whether an error occurs in any of the sequences.
- /// </summary>
- /// <typeparam name="TSource">Source sequence element type.</typeparam>
- /// <param name="sources">Source sequences.</param>
- /// <returns>Sequence concatenating the elements of the given sequences, ignoring errors.</returns>
- public static IQueryable<TSource> OnErrorResumeNext<TSource>(this IQueryable<IEnumerable<TSource>> sources)
- {
- if (sources == null)
- throw new ArgumentNullException(nameof(sources));
- return sources.Provider.CreateQuery<TSource>(
- Expression.Call(
- null,
- #if CRIPPLED_REFLECTION
- InfoOf(() => QueryableEx.OnErrorResumeNext<TSource>(default(IQueryable<IEnumerable<TSource>>))),
- #else
- ((MethodInfo)MethodInfo.GetCurrentMethod()).MakeGenericMethod(typeof(TSource)),
- #endif
- sources.Expression
- )
- );
- }
- #pragma warning disable 1591
- [EditorBrowsable(EditorBrowsableState.Never)]
- public static IEnumerable<TSource> OnErrorResumeNext<TSource>(IEnumerable<IEnumerable<TSource>> sources)
- {
- return EnumerableEx.OnErrorResumeNext(sources);
- }
- #pragma warning restore 1591
- /// <summary>
- /// Creates a sequence that retries enumerating the source sequence as long as an error occurs.
- /// </summary>
- /// <typeparam name="TSource">Source sequence element type.</typeparam>
- /// <param name="source">Source sequence.</param>
- /// <returns>Sequence concatenating the results of the source sequence as long as an error occurs.</returns>
- public static IQueryable<TSource> Retry<TSource>(this IQueryable<TSource> source)
- {
- if (source == null)
- throw new ArgumentNullException(nameof(source));
- return source.Provider.CreateQuery<TSource>(
- Expression.Call(
- null,
- #if CRIPPLED_REFLECTION
- InfoOf(() => QueryableEx.Retry<TSource>(default(IQueryable<TSource>))),
- #else
- ((MethodInfo)MethodInfo.GetCurrentMethod()).MakeGenericMethod(typeof(TSource)),
- #endif
- source.Expression
- )
- );
- }
- #pragma warning disable 1591
- [EditorBrowsable(EditorBrowsableState.Never)]
- public static IEnumerable<TSource> Retry<TSource>(IEnumerable<TSource> source)
- {
- return EnumerableEx.Retry(source);
- }
- #pragma warning restore 1591
- /// <summary>
- /// Creates a sequence that retries enumerating the source sequence as long as an error occurs, with the specified maximum number of retries.
- /// </summary>
- /// <typeparam name="TSource">Source sequence element type.</typeparam>
- /// <param name="source">Source sequence.</param>
- /// <param name="retryCount">Maximum number of retries.</param>
- /// <returns>Sequence concatenating the results of the source sequence as long as an error occurs.</returns>
- public static IQueryable<TSource> Retry<TSource>(this IQueryable<TSource> source, int retryCount)
- {
- if (source == null)
- throw new ArgumentNullException(nameof(source));
- return source.Provider.CreateQuery<TSource>(
- Expression.Call(
- null,
- #if CRIPPLED_REFLECTION
- InfoOf(() => QueryableEx.Retry<TSource>(default(IQueryable<TSource>), default(int))),
- #else
- ((MethodInfo)MethodInfo.GetCurrentMethod()).MakeGenericMethod(typeof(TSource)),
- #endif
- source.Expression,
- Expression.Constant(retryCount, typeof(int))
- )
- );
- }
- #pragma warning disable 1591
- [EditorBrowsable(EditorBrowsableState.Never)]
- public static IEnumerable<TSource> Retry<TSource>(IEnumerable<TSource> source, int retryCount)
- {
- return EnumerableEx.Retry(source, retryCount);
- }
- #pragma warning restore 1591
- /// <summary>
- /// Generates an enumerable sequence by repeating a source sequence as long as the given loop condition holds.
- /// </summary>
- /// <typeparam name="TResult">Result sequence element type.</typeparam>
- /// <param name="provider">Query provider.</param>
- /// <param name="condition">Loop condition.</param>
- /// <param name="source">Sequence to repeat while the condition evaluates true.</param>
- /// <returns>Sequence generated by repeating the given sequence while the condition evaluates to true.</returns>
- public static IQueryable<TResult> While<TResult>(this IQueryProvider provider, Expression<Func<bool>> condition, IEnumerable<TResult> source)
- {
- if (provider == null)
- throw new ArgumentNullException(nameof(provider));
- if (condition == null)
- throw new ArgumentNullException(nameof(condition));
- if (source == null)
- throw new ArgumentNullException(nameof(source));
- return provider.CreateQuery<TResult>(
- Expression.Call(
- null,
- #if CRIPPLED_REFLECTION
- InfoOf(() => QueryableEx.While<TResult>(default(IQueryProvider), default(Expression<Func<bool>>), default(IEnumerable<TResult>))),
- #else
- ((MethodInfo)MethodInfo.GetCurrentMethod()).MakeGenericMethod(typeof(TResult)),
- #endif
- Expression.Constant(provider, typeof(IQueryProvider)),
- condition,
- GetSourceExpression(source)
- )
- );
- }
- #pragma warning disable 1591
- [EditorBrowsable(EditorBrowsableState.Never)]
- public static /*!*/IQueryable<TResult> While<TResult>(Func<bool> condition, IEnumerable<TResult> source)
- {
- return EnumerableEx.While(condition, source).AsQueryable();
- }
- #pragma warning restore 1591
- /// <summary>
- /// Returns an enumerable sequence based on the evaluation result of the given condition.
- /// </summary>
- /// <typeparam name="TResult">Result sequence element type.</typeparam>
- /// <param name="provider">Query provider.</param>
- /// <param name="condition">Condition to evaluate.</param>
- /// <param name="thenSource">Sequence to return in case the condition evaluates true.</param>
- /// <param name="elseSource">Sequence to return in case the condition evaluates false.</param>
- /// <returns>Either of the two input sequences based on the result of evaluating the condition.</returns>
- public static IQueryable<TResult> If<TResult>(this IQueryProvider provider, Expression<Func<bool>> condition, IEnumerable<TResult> thenSource, IEnumerable<TResult> elseSource)
- {
- if (provider == null)
- throw new ArgumentNullException(nameof(provider));
- if (condition == null)
- throw new ArgumentNullException(nameof(condition));
- if (thenSource == null)
- throw new ArgumentNullException(nameof(thenSource));
- if (elseSource == null)
- throw new ArgumentNullException(nameof(elseSource));
- return provider.CreateQuery<TResult>(
- Expression.Call(
- null,
- #if CRIPPLED_REFLECTION
- InfoOf(() => QueryableEx.If<TResult>(default(IQueryProvider), default(Expression<Func<bool>>), default(IEnumerable<TResult>), default(IEnumerable<TResult>))),
- #else
- ((MethodInfo)MethodInfo.GetCurrentMethod()).MakeGenericMethod(typeof(TResult)),
- #endif
- Expression.Constant(provider, typeof(IQueryProvider)),
- condition,
- GetSourceExpression(thenSource),
- GetSourceExpression(elseSource)
- )
- );
- }
- #pragma warning disable 1591
- [EditorBrowsable(EditorBrowsableState.Never)]
- public static /*!*/IQueryable<TResult> If<TResult>(Func<bool> condition, IEnumerable<TResult> thenSource, IEnumerable<TResult> elseSource)
- {
- return EnumerableEx.If(condition, thenSource, elseSource).AsQueryable();
- }
- #pragma warning restore 1591
- /// <summary>
- /// Returns an enumerable sequence if the evaluation result of the given condition is true, otherwise returns an empty sequence.
- /// </summary>
- /// <typeparam name="TResult">Result sequence element type.</typeparam>
- /// <param name="provider">Query provider.</param>
- /// <param name="condition">Condition to evaluate.</param>
- /// <param name="thenSource">Sequence to return in case the condition evaluates true.</param>
- /// <returns>The given input sequence if the condition evaluates true; otherwise, an empty sequence.</returns>
- public static IQueryable<TResult> If<TResult>(this IQueryProvider provider, Expression<Func<bool>> condition, IEnumerable<TResult> thenSource)
- {
- if (provider == null)
- throw new ArgumentNullException(nameof(provider));
- if (condition == null)
- throw new ArgumentNullException(nameof(condition));
- if (thenSource == null)
- throw new ArgumentNullException(nameof(thenSource));
- return provider.CreateQuery<TResult>(
- Expression.Call(
- null,
- #if CRIPPLED_REFLECTION
- InfoOf(() => QueryableEx.If<TResult>(default(IQueryProvider), default(Expression<Func<bool>>), default(IEnumerable<TResult>))),
- #else
- ((MethodInfo)MethodInfo.GetCurrentMethod()).MakeGenericMethod(typeof(TResult)),
- #endif
- Expression.Constant(provider, typeof(IQueryProvider)),
- condition,
- GetSourceExpression(thenSource)
- )
- );
- }
- #pragma warning disable 1591
- [EditorBrowsable(EditorBrowsableState.Never)]
- public static /*!*/IQueryable<TResult> If<TResult>(Func<bool> condition, IEnumerable<TResult> thenSource)
- {
- return EnumerableEx.If(condition, thenSource).AsQueryable();
- }
- #pragma warning restore 1591
- /// <summary>
- /// Generates an enumerable sequence by repeating a source sequence as long as the given loop postcondition holds.
- /// </summary>
- /// <typeparam name="TResult">Result sequence element type.</typeparam>
- /// <param name="source">Source sequence to repeat while the condition evaluates true.</param>
- /// <param name="condition">Loop condition.</param>
- /// <returns>Sequence generated by repeating the given sequence until the condition evaluates to false.</returns>
- public static IQueryable<TResult> DoWhile<TResult>(this IQueryable<TResult> source, Expression<Func<bool>> condition)
- {
- if (source == null)
- throw new ArgumentNullException(nameof(source));
- if (condition == null)
- throw new ArgumentNullException(nameof(condition));
- return source.Provider.CreateQuery<TResult>(
- Expression.Call(
- null,
- #if CRIPPLED_REFLECTION
- InfoOf(() => QueryableEx.DoWhile<TResult>(default(IQueryable<TResult>), default(Expression<Func<bool>>))),
- #else
- ((MethodInfo)MethodInfo.GetCurrentMethod()).MakeGenericMethod(typeof(TResult)),
- #endif
- source.Expression,
- condition
- )
- );
- }
- #pragma warning disable 1591
- [EditorBrowsable(EditorBrowsableState.Never)]
- public static IEnumerable<TResult> DoWhile<TResult>(IEnumerable<TResult> source, Func<bool> condition)
- {
- return EnumerableEx.DoWhile(source, condition);
- }
- #pragma warning restore 1591
- /// <summary>
- /// Returns a sequence from a dictionary based on the result of evaluating a selector function.
- /// </summary>
- /// <typeparam name="TValue">Type of the selector value.</typeparam>
- /// <typeparam name="TResult">Result sequence element type.</typeparam>
- /// <param name="provider">Query provider.</param>
- /// <param name="selector">Selector function used to pick a sequence from the given sources.</param>
- /// <param name="sources">Dictionary mapping selector values onto resulting sequences.</param>
- /// <returns>The source sequence corresponding with the evaluated selector value; otherwise, an empty sequence.</returns>
- public static IQueryable<TResult> Case<TValue, TResult>(this IQueryProvider provider, Expression<Func<TValue>> selector, IDictionary<TValue, IEnumerable<TResult>> sources)
- {
- if (provider == null)
- throw new ArgumentNullException(nameof(provider));
- if (selector == null)
- throw new ArgumentNullException(nameof(selector));
- if (sources == null)
- throw new ArgumentNullException(nameof(sources));
- return provider.CreateQuery<TResult>(
- Expression.Call(
- null,
- #if CRIPPLED_REFLECTION
- InfoOf(() => QueryableEx.Case<TValue, TResult>(default(IQueryProvider), default(Expression<Func<TValue>>), default(IDictionary<TValue, IEnumerable<TResult>>))),
- #else
- ((MethodInfo)MethodInfo.GetCurrentMethod()).MakeGenericMethod(typeof(TValue), typeof(TResult)),
- #endif
- selector,
- Expression.Constant(sources, typeof(IDictionary<TValue, IEnumerable<TResult>>))
- )
- );
- }
- #pragma warning disable 1591
- [EditorBrowsable(EditorBrowsableState.Never)]
- public static /*!*/IQueryable<TResult> Case<TValue, TResult>(Func<TValue> selector, IDictionary<TValue, IEnumerable<TResult>> sources)
- {
- return EnumerableEx.Case(selector, sources).AsQueryable();
- }
- #pragma warning restore 1591
- /// <summary>
- /// Returns a sequence from a dictionary based on the result of evaluating a selector function, also specifying a default sequence.
- /// </summary>
- /// <typeparam name="TValue">Type of the selector value.</typeparam>
- /// <typeparam name="TResult">Result sequence element type.</typeparam>
- /// <param name="provider">Query provider.</param>
- /// <param name="selector">Selector function used to pick a sequence from the given sources.</param>
- /// <param name="sources">Dictionary mapping selector values onto resulting sequences.</param>
- /// <param name="defaultSource">Default sequence to return in case there's no corresponding source for the computed selector value.</param>
- /// <returns>The source sequence corresponding with the evaluated selector value; otherwise, the default source.</returns>
- public static IQueryable<TResult> Case<TValue, TResult>(this IQueryProvider provider, Expression<Func<TValue>> selector, IDictionary<TValue, IEnumerable<TResult>> sources, IEnumerable<TResult> defaultSource)
- {
- if (provider == null)
- throw new ArgumentNullException(nameof(provider));
- if (selector == null)
- throw new ArgumentNullException(nameof(selector));
- if (sources == null)
- throw new ArgumentNullException(nameof(sources));
- if (defaultSource == null)
- throw new ArgumentNullException(nameof(defaultSource));
- return provider.CreateQuery<TResult>(
- Expression.Call(
- null,
- #if CRIPPLED_REFLECTION
- InfoOf(() => QueryableEx.Case<TValue, TResult>(default(IQueryProvider), default(Expression<Func<TValue>>), default(IDictionary<TValue, IEnumerable<TResult>>), default(IEnumerable<TResult>))),
- #else
- ((MethodInfo)MethodInfo.GetCurrentMethod()).MakeGenericMethod(typeof(TValue), typeof(TResult)),
- #endif
- selector,
- Expression.Constant(sources, typeof(IDictionary<TValue, IEnumerable<TResult>>))
- )
- );
- }
- #pragma warning disable 1591
- [EditorBrowsable(EditorBrowsableState.Never)]
- public static /*!*/IQueryable<TResult> Case<TValue, TResult>(Func<TValue> selector, IDictionary<TValue, IEnumerable<TResult>> sources, IEnumerable<TResult> defaultSource)
- {
- return EnumerableEx.Case(selector, sources, defaultSource).AsQueryable();
- }
- #pragma warning restore 1591
- /// <summary>
- /// Generates a sequence by enumerating a source sequence, mapping its elements on result sequences, and concatenating those sequences.
- /// </summary>
- /// <typeparam name="TSource">Source sequence element type.</typeparam>
- /// <typeparam name="TResult">Result sequence element type.</typeparam>
- /// <param name="provider">Query provider.</param>
- /// <param name="source">Source sequence.</param>
- /// <param name="resultSelector">Result selector to evaluate for each iteration over the source.</param>
- /// <returns>Sequence concatenating the inner sequences that result from evaluating the result selector on elements from the source.</returns>
- public static IQueryable<TResult> For<TSource, TResult>(this IQueryProvider provider, IEnumerable<TSource> source, Expression<Func<TSource, IEnumerable<TResult>>> resultSelector)
- {
- if (provider == null)
- throw new ArgumentNullException(nameof(provider));
- if (source == null)
- throw new ArgumentNullException(nameof(source));
- if (resultSelector == null)
- throw new ArgumentNullException(nameof(resultSelector));
- return provider.CreateQuery<TResult>(
- Expression.Call(
- null,
- #if CRIPPLED_REFLECTION
- InfoOf(() => QueryableEx.For<TSource, TResult>(default(IQueryProvider), default(IEnumerable<TSource>), default(Expression<Func<TSource, IEnumerable<TResult>>>))),
- #else
- ((MethodInfo)MethodInfo.GetCurrentMethod()).MakeGenericMethod(typeof(TSource), typeof(TResult)),
- #endif
- GetSourceExpression(source),
- resultSelector
- )
- );
- }
- #pragma warning disable 1591
- [EditorBrowsable(EditorBrowsableState.Never)]
- public static /*!*/IQueryable<TResult> For<TSource, TResult>(IEnumerable<TSource> source, Func<TSource, IEnumerable<TResult>> resultSelector)
- {
- return EnumerableEx.For(source, resultSelector).AsQueryable();
- }
- #pragma warning restore 1591
- /// <summary>
- /// Concatenates the input sequences.
- /// </summary>
- /// <typeparam name="TSource">Source sequence element type.</typeparam>
- /// <param name="sources">Source sequences.</param>
- /// <returns>Sequence with the elements of the source sequences concatenated.</returns>
- public static IQueryable<TSource> Concat<TSource>(this IQueryable<IEnumerable<TSource>> sources)
- {
- if (sources == null)
- throw new ArgumentNullException(nameof(sources));
- return sources.Provider.CreateQuery<TSource>(
- Expression.Call(
- null,
- #if CRIPPLED_REFLECTION
- InfoOf(() => QueryableEx.Concat<TSource>(default(IQueryable<IEnumerable<TSource>>))),
- #else
- ((MethodInfo)MethodInfo.GetCurrentMethod()).MakeGenericMethod(typeof(TSource)),
- #endif
- GetSourceExpression(sources)
- )
- );
- }
- #pragma warning disable 1591
- [EditorBrowsable(EditorBrowsableState.Never)]
- public static IEnumerable<TSource> Concat<TSource>(IEnumerable<IEnumerable<TSource>> sources)
- {
- return EnumerableEx.Concat(sources);
- }
- #pragma warning restore 1591
- /// <summary>
- /// Concatenates the input sequences.
- /// </summary>
- /// <typeparam name="TSource">Source sequence element type.</typeparam>
- /// <param name="provider">Query provider.</param>
- /// <param name="sources">Source sequences.</param>
- /// <returns>Sequence with the elements of the source sequences concatenated.</returns>
- public static IQueryable<TSource> Concat<TSource>(this IQueryProvider provider, params IEnumerable<TSource>[] sources)
- {
- if (provider == null)
- throw new ArgumentNullException(nameof(provider));
- if (sources == null)
- throw new ArgumentNullException(nameof(sources));
- return provider.CreateQuery<TSource>(
- Expression.Call(
- null,
- #if CRIPPLED_REFLECTION
- InfoOf(() => QueryableEx.Concat<TSource>(default(IQueryProvider), default(IEnumerable<TSource>[]))),
- #else
- ((MethodInfo)MethodInfo.GetCurrentMethod()).MakeGenericMethod(typeof(TSource)),
- #endif
- Expression.Constant(provider, typeof(IQueryProvider)),
- GetSourceExpression(sources)
- )
- );
- }
- #pragma warning disable 1591
- [EditorBrowsable(EditorBrowsableState.Never)]
- public static /*!*/IQueryable<TSource> Concat<TSource>(params IEnumerable<TSource>[] sources)
- {
- return EnumerableEx.Concat(sources).AsQueryable();
- }
- #pragma warning restore 1591
- /// <summary>
- /// Projects each element of a sequence to an given sequence and flattens the resulting sequences into one sequence.
- /// </summary>
- /// <typeparam name="TSource">First source sequence element type.</typeparam>
- /// <typeparam name="TOther">Second source sequence element type.</typeparam>
- /// <param name="source">A sequence of values to project.</param>
- /// <param name="other">Inner sequence each source sequenec element is projected onto.</param>
- /// <returns>Sequence flattening the sequences that result from projecting elements in the source sequence.</returns>
- public static IQueryable<TOther> SelectMany<TSource, TOther>(this IQueryable<TSource> source, IEnumerable<TOther> other)
- {
- if (source == null)
- throw new ArgumentNullException(nameof(source));
- if (other == null)
- throw new ArgumentNullException(nameof(other));
- return source.Provider.CreateQuery<TOther>(
- Expression.Call(
- null,
- #if CRIPPLED_REFLECTION
- InfoOf(() => QueryableEx.SelectMany<TSource, TOther>(default(IQueryable<TSource>), default(IEnumerable<TOther>))),
- #else
- ((MethodInfo)MethodInfo.GetCurrentMethod()).MakeGenericMethod(typeof(TSource), typeof(TOther)),
- #endif
- source.Expression,
- GetSourceExpression(other)
- )
- );
- }
- #pragma warning disable 1591
- [EditorBrowsable(EditorBrowsableState.Never)]
- public static IEnumerable<TOther> SelectMany<TSource, TOther>(IEnumerable<TSource> source, IEnumerable<TOther> other)
- {
- return EnumerableEx.SelectMany(source, other);
- }
- #pragma warning restore 1591
- /// <summary>
- /// Hides the enumerable sequence object identity.
- /// </summary>
- /// <typeparam name="TSource">Source sequence element type.</typeparam>
- /// <param name="source">Source sequence.</param>
- /// <returns>Enumerable sequence with the same behavior as the original, but hiding the source object identity.</returns>
- /// <remarks>AsQueryable doesn't hide the object identity, and simply acts as a cast to the IQueryable<TSource> interface.</remarks>
- public static IQueryable<TSource> Hide<TSource>(this IQueryable<TSource> source)
- {
- if (source == null)
- throw new ArgumentNullException(nameof(source));
- return source.Provider.CreateQuery<TSource>(
- Expression.Call(
- null,
- #if CRIPPLED_REFLECTION
- InfoOf(() => QueryableEx.Hide<TSource>(default(IQueryable<TSource>))),
- #else
- ((MethodInfo)MethodInfo.GetCurrentMethod()).MakeGenericMethod(typeof(TSource)),
- #endif
- source.Expression
- )
- );
- }
- #pragma warning disable 1591
- [EditorBrowsable(EditorBrowsableState.Never)]
- public static IEnumerable<TSource> Hide<TSource>(IEnumerable<TSource> source)
- {
- return EnumerableEx.Hide(source);
- }
- #pragma warning restore 1591
- /// <summary>
- /// Lazily invokes an action for each value in the sequence.
- /// </summary>
- /// <typeparam name="TSource">Source sequence element type.</typeparam>
- /// <param name="source">Source sequence.</param>
- /// <param name="onNext">Action to invoke for each element.</param>
- /// <returns>Sequence exhibiting the specified side-effects upon enumeration.</returns>
- public static IQueryable<TSource> Do<TSource>(this IQueryable<TSource> source, Expression<Action<TSource>> onNext)
- {
- if (source == null)
- throw new ArgumentNullException(nameof(source));
- if (onNext == null)
- throw new ArgumentNullException(nameof(onNext));
- return source.Provider.CreateQuery<TSource>(
- Expression.Call(
- null,
- #if CRIPPLED_REFLECTION
- InfoOf(() => QueryableEx.Do<TSource>(default(IQueryable<TSource>), default(Expression<Action<TSource>>))),
- #else
- ((MethodInfo)MethodInfo.GetCurrentMethod()).MakeGenericMethod(typeof(TSource)),
- #endif
- source.Expression,
- onNext
- )
- );
- }
- #pragma warning disable 1591
- [EditorBrowsable(EditorBrowsableState.Never)]
- public static IEnumerable<TSource> Do<TSource>(IEnumerable<TSource> source, Action<TSource> onNext)
- {
- return EnumerableEx.Do(source, onNext);
- }
- #pragma warning restore 1591
- /// <summary>
- /// Lazily invokes an action for each value in the sequence, and executes an action for successful termination.
- /// </summary>
- /// <typeparam name="TSource">Source sequence element type.</typeparam>
- /// <param name="source">Source sequence.</param>
- /// <param name="onNext">Action to invoke for each element.</param>
- /// <param name="onCompleted">Action to invoke on successful termination of the sequence.</param>
- /// <returns>Sequence exhibiting the specified side-effects upon enumeration.</returns>
- public static IQueryable<TSource> Do<TSource>(this IQueryable<TSource> source, Expression<Action<TSource>> onNext, Expression<Action> onCompleted)
- {
- if (source == null)
- throw new ArgumentNullException(nameof(source));
- if (onNext == null)
- throw new ArgumentNullException(nameof(onNext));
- if (onCompleted == null)
- throw new ArgumentNullException(nameof(onCompleted));
- return source.Provider.CreateQuery<TSource>(
- Expression.Call(
- null,
- #if CRIPPLED_REFLECTION
- InfoOf(() => QueryableEx.Do<TSource>(default(IQueryable<TSource>), default(Expression<Action<TSource>>), default(Expression<Action>))),
- #else
- ((MethodInfo)MethodInfo.GetCurrentMethod()).MakeGenericMethod(typeof(TSource)),
- #endif
- source.Expression,
- onNext,
- onCompleted
- )
- );
- }
- #pragma warning disable 1591
- [EditorBrowsable(EditorBrowsableState.Never)]
- public static IEnumerable<TSource> Do<TSource>(IEnumerable<TSource> source, Action<TSource> onNext, Action onCompleted)
- {
- return EnumerableEx.Do(source, onNext, onCompleted);
- }
- #pragma warning restore 1591
- /// <summary>
- /// Lazily invokes an action for each value in the sequence, and executes an action upon exceptional termination.
- /// </summary>
- /// <typeparam name="TSource">Source sequence element type.</typeparam>
- /// <param name="source">Source sequence.</param>
- /// <param name="onNext">Action to invoke for each element.</param>
- /// <param name="onError">Action to invoke on exceptional termination of the sequence.</param>
- /// <returns>Sequence exhibiting the specified side-effects upon enumeration.</returns>
- public static IQueryable<TSource> Do<TSource>(this IQueryable<TSource> source, Expression<Action<TSource>> onNext, Expression<Action<Exception>> onError)
- {
- if (source == null)
- throw new ArgumentNullException(nameof(source));
- if (onNext == null)
- throw new ArgumentNullException(nameof(onNext));
- if (onError == null)
- throw new ArgumentNullException(nameof(onError));
- return source.Provider.CreateQuery<TSource>(
- Expression.Call(
- null,
- #if CRIPPLED_REFLECTION
- InfoOf(() => QueryableEx.Do<TSource>(default(IQueryable<TSource>), default(Expression<Action<TSource>>), default(Expression<Action<Exception>>))),
- #else
- ((MethodInfo)MethodInfo.GetCurrentMethod()).MakeGenericMethod(typeof(TSource)),
- #endif
- source.Expression,
- onNext,
- onError
- )
- );
- }
- #pragma warning disable 1591
- [EditorBrowsable(EditorBrowsableState.Never)]
- public static IEnumerable<TSource> Do<TSource>(IEnumerable<TSource> source, Action<TSource> onNext, Action<Exception> onError)
- {
- return EnumerableEx.Do(source, onNext, onError);
- }
- #pragma warning restore 1591
- /// <summary>
- /// Lazily invokes an action for each value in the sequence, and executes an action upon successful or exceptional termination.
- /// </summary>
- /// <typeparam name="TSource">Source sequence element type.</typeparam>
- /// <param name="source">Source sequence.</param>
- /// <param name="onNext">Action to invoke for each element.</param>
- /// <param name="onError">Action to invoke on exceptional termination of the sequence.</param>
- /// <param name="onCompleted">Action to invoke on successful termination of the sequence.</param>
- /// <returns>Sequence exhibiting the specified side-effects upon enumeration.</returns>
- public static IQueryable<TSource> Do<TSource>(this IQueryable<TSource> source, Expression<Action<TSource>> onNext, Expression<Action<Exception>> onError, Expression<Action> onCompleted)
- {
- if (source == null)
- throw new ArgumentNullException(nameof(source));
- if (onNext == null)
- throw new ArgumentNullException(nameof(onNext));
- if (onError == null)
- throw new ArgumentNullException(nameof(onError));
- if (onCompleted == null)
- throw new ArgumentNullException(nameof(onCompleted));
- return source.Provider.CreateQuery<TSource>(
- Expression.Call(
- null,
- #if CRIPPLED_REFLECTION
- InfoOf(() => QueryableEx.Do<TSource>(default(IQueryable<TSource>), default(Expression<Action<TSource>>), default(Expression<Action<Exception>>), default(Expression<Action>))),
- #else
- ((MethodInfo)MethodInfo.GetCurrentMethod()).MakeGenericMethod(typeof(TSource)),
- #endif
- source.Expression,
- onNext,
- onError,
- onCompleted
- )
- );
- }
- #pragma warning disable 1591
- [EditorBrowsable(EditorBrowsableState.Never)]
- public static IEnumerable<TSource> Do<TSource>(IEnumerable<TSource> source, Action<TSource> onNext, Action<Exception> onError, Action onCompleted)
- {
- return EnumerableEx.Do(source, onNext, onError, onCompleted);
- }
- #pragma warning restore 1591
- /// <summary>
- /// Lazily invokes observer methods for each value in the sequence, and upon successful or exceptional termination.
- /// </summary>
- /// <typeparam name="TSource">Source sequence element type.</typeparam>
- /// <param name="source">Source sequence.</param>
- /// <param name="observer">Observer to invoke notification calls on.</param>
- /// <returns>Sequence exhibiting the side-effects of observer method invocation upon enumeration.</returns>
- public static IQueryable<TSource> Do<TSource>(this IQueryable<TSource> source, IObserver<TSource> observer)
- {
- if (source == null)
- throw new ArgumentNullException(nameof(source));
- if (observer == null)
- throw new ArgumentNullException(nameof(observer));
- return source.Provider.CreateQuery<TSource>(
- Expression.Call(
- null,
- #if CRIPPLED_REFLECTION
- InfoOf(() => QueryableEx.Do<TSource>(default(IQueryable<TSource>), default(IObserver<TSource>))),
- #else
- ((MethodInfo)MethodInfo.GetCurrentMethod()).MakeGenericMethod(typeof(TSource)),
- #endif
- source.Expression,
- Expression.Constant(observer, typeof(IObserver<TSource>))
- )
- );
- }
- #pragma warning disable 1591
- [EditorBrowsable(EditorBrowsableState.Never)]
- public static IEnumerable<TSource> Do<TSource>(IEnumerable<TSource> source, IObserver<TSource> observer)
- {
- return EnumerableEx.Do(source, observer);
- }
- #pragma warning restore 1591
- /// <summary>
- /// Generates a sequence of non-overlapping adjacent buffers over the source sequence.
- /// </summary>
- /// <typeparam name="TSource">Source sequence element type.</typeparam>
- /// <param name="source">Source sequence.</param>
- /// <param name="count">Number of elements for allocated buffers.</param>
- /// <returns>Sequence of buffers containing source sequence elements.</returns>
- public static IQueryable<IList<TSource>> Buffer<TSource>(this IQueryable<TSource> source, int count)
- {
- if (source == null)
- throw new ArgumentNullException(nameof(source));
- return source.Provider.CreateQuery<IList<TSource>>(
- Expression.Call(
- null,
- #if CRIPPLED_REFLECTION
- InfoOf(() => QueryableEx.Buffer<TSource>(default(IQueryable<TSource>), default(int))),
- #else
- ((MethodInfo)MethodInfo.GetCurrentMethod()).MakeGenericMethod(typeof(TSource)),
- #endif
- source.Expression,
- Expression.Constant(count, typeof(int))
- )
- );
- }
- #pragma warning disable 1591
- [EditorBrowsable(EditorBrowsableState.Never)]
- public static IEnumerable<IList<TSource>> Buffer<TSource>(IEnumerable<TSource> source, int count)
- {
- return EnumerableEx.Buffer(source, count);
- }
- #pragma warning restore 1591
- /// <summary>
- /// Generates a sequence of buffers over the source sequence, with specified length and possible overlap.
- /// </summary>
- /// <typeparam name="TSource">Source sequence element type.</typeparam>
- /// <param name="source">Source sequence.</param>
- /// <param name="count">Number of elements for allocated buffers.</param>
- /// <param name="skip">Number of elements to skip between the start of consecutive buffers.</param>
- /// <returns>Sequence of buffers containing source sequence elements.</returns>
- public static IQueryable<IList<TSource>> Buffer<TSource>(this IQueryable<TSource> source, int count, int skip)
- {
- if (source == null)
- throw new ArgumentNullException(nameof(source));
- return source.Provider.CreateQuery<IList<TSource>>(
- Expression.Call(
- null,
- #if CRIPPLED_REFLECTION
- InfoOf(() => QueryableEx.Buffer<TSource>(default(IQueryable<TSource>), default(int), default(int))),
- #else
- ((MethodInfo)MethodInfo.GetCurrentMethod()).MakeGenericMethod(typeof(TSource)),
- #endif
- source.Expression,
- Expression.Constant(count, typeof(int)),
- Expression.Constant(skip, typeof(int))
- )
- );
- }
- #pragma warning disable 1591
- [EditorBrowsable(EditorBrowsableState.Never)]
- public static IEnumerable<IList<TSource>> Buffer<TSource>(IEnumerable<TSource> source, int count, int skip)
- {
- return EnumerableEx.Buffer(source, count, skip);
- }
- #pragma warning restore 1591
- /// <summary>
- /// Ignores all elements in the source sequence.
- /// </summary>
- /// <typeparam name="TSource">Source sequence element type.</typeparam>
- /// <param name="source">Source sequence.</param>
- /// <returns>Source sequence without its elements.</returns>
- public static IQueryable<TSource> IgnoreElements<TSource>(this IQueryable<TSource> source)
- {
- if (source == null)
- throw new ArgumentNullException(nameof(source));
- return source.Provider.CreateQuery<TSource>(
- Expression.Call(
- null,
- #if CRIPPLED_REFLECTION
- InfoOf(() => QueryableEx.IgnoreElements<TSource>(default(IQueryable<TSource>))),
- #else
- ((MethodInfo)MethodInfo.GetCurrentMethod()).MakeGenericMethod(typeof(TSource)),
- #endif
- source.Expression
- )
- );
- }
- #pragma warning disable 1591
- [EditorBrowsable(EditorBrowsableState.Never)]
- public static IEnumerable<TSource> IgnoreElements<TSource>(IEnumerable<TSource> source)
- {
- return EnumerableEx.IgnoreElements(source);
- }
- #pragma warning restore 1591
- /// <summary>
- /// Returns elements with a distinct key value by using the default equality comparer to compare key values.
- /// </summary>
- /// <typeparam name="TSource">Source sequence element type.</typeparam>
- /// <typeparam name="TKey">Key type.</typeparam>
- /// <param name="source">Source sequence.</param>
- /// <param name="keySelector">Key selector.</param>
- /// <returns>Sequence that contains the elements from the source sequence with distinct key values.</returns>
- public static IQueryable<TSource> Distinct<TSource, TKey>(this IQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector)
- {
- if (source == null)
- throw new ArgumentNullException(nameof(source));
- if (keySelector == null)
- throw new ArgumentNullException(nameof(keySelector));
- return source.Provider.CreateQuery<TSource>(
- Expression.Call(
- null,
- #if CRIPPLED_REFLECTION
- InfoOf(() => QueryableEx.Distinct<TSource, TKey>(default(IQueryable<TSource>), default(Expression<Func<TSource, TKey>>))),
- #else
- ((MethodInfo)MethodInfo.GetCurrentMethod()).MakeGenericMethod(typeof(TSource), typeof(TKey)),
- #endif
- source.Expression,
- keySelector
- )
- );
- }
- #pragma warning disable 1591
- [EditorBrowsable(EditorBrowsableState.Never)]
- public static IEnumerable<TSource> Distinct<TSource, TKey>(IEnumerable<TSource> source, Func<TSource, TKey> keySelector)
- {
- return EnumerableEx.Distinct(source, keySelector);
- }
- #pragma warning restore 1591
- /// <summary>
- /// Returns elements with a distinct key value by using the specified equality comparer to compare key values.
- /// </summary>
- /// <typeparam name="TSource">Source sequence element type.</typeparam>
- /// <typeparam name="TKey">Key type.</typeparam>
- /// <param name="source">Source sequence.</param>
- /// <param name="keySelector">Key selector.</param>
- /// <param name="comparer">Comparer used to compare key values.</param>
- /// <returns>Sequence that contains the elements from the source sequence with distinct key values.</returns>
- public static IQueryable<TSource> Distinct<TSource, TKey>(this IQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector, IEqualityComparer<TKey> comparer)
- {
- if (source == null)
- throw new ArgumentNullException(nameof(source));
- if (keySelector == null)
- throw new ArgumentNullException(nameof(keySelector));
- if (comparer == null)
- throw new ArgumentNullException(nameof(comparer));
- return source.Provider.CreateQuery<TSource>(
- Expression.Call(
- null,
- #if CRIPPLED_REFLECTION
- InfoOf(() => QueryableEx.Distinct<TSource, TKey>(default(IQueryable<TSource>), default(Expression<Func<TSource, TKey>>), default(IEqualityComparer<TKey>))),
- #else
- ((MethodInfo)MethodInfo.GetCurrentMethod()).MakeGenericMethod(typeof(TSource), typeof(TKey)),
- #endif
- source.Expression,
- keySelector,
- Expression.Constant(comparer, typeof(IEqualityComparer<TKey>))
- )
- );
- }
- #pragma warning disable 1591
- [EditorBrowsable(EditorBrowsableState.Never)]
- public static IEnumerable<TSource> Distinct<TSource, TKey>(IEnumerable<TSource> source, Func<TSource, TKey> keySelector, IEqualityComparer<TKey> comparer)
- {
- return EnumerableEx.Distinct(source, keySelector, comparer);
- }
- #pragma warning restore 1591
- /// <summary>
- /// Returns consecutive distinct elements by using the default equality comparer to compare values.
- /// </summary>
- /// <typeparam name="TSource">Source sequence element type.</typeparam>
- /// <param name="source">Source sequence.</param>
- /// <returns>Sequence without adjacent non-distinct elements.</returns>
- public static IQueryable<TSource> DistinctUntilChanged<TSource>(this IQueryable<TSource> source)
- {
- if (source == null)
- throw new ArgumentNullException(nameof(source));
- return source.Provider.CreateQuery<TSource>(
- Expression.Call(
- null,
- #if CRIPPLED_REFLECTION
- InfoOf(() => QueryableEx.DistinctUntilChanged<TSource>(default(IQueryable<TSource>))),
- #else
- ((MethodInfo)MethodInfo.GetCurrentMethod()).MakeGenericMethod(typeof(TSource)),
- #endif
- source.Expression
- )
- );
- }
- #pragma warning disable 1591
- [EditorBrowsable(EditorBrowsableState.Never)]
- public static IEnumerable<TSource> DistinctUntilChanged<TSource>(IEnumerable<TSource> source)
- {
- return EnumerableEx.DistinctUntilChanged(source);
- }
- #pragma warning restore 1591
- /// <summary>
- /// Returns consecutive distinct elements by using the specified equality comparer to compare values.
- /// </summary>
- /// <typeparam name="TSource">Source sequence element type.</typeparam>
- /// <param name="source">Source sequence.</param>
- /// <param name="comparer">Comparer used to compare values.</param>
- /// <returns>Sequence without adjacent non-distinct elements.</returns>
- public static IQueryable<TSource> DistinctUntilChanged<TSource>(this IQueryable<TSource> source, IEqualityComparer<TSource> comparer)
- {
- if (source == null)
- throw new ArgumentNullException(nameof(source));
- if (comparer == null)
- throw new ArgumentNullException(nameof(comparer));
- return source.Provider.CreateQuery<TSource>(
- Expression.Call(
- null,
- #if CRIPPLED_REFLECTION
- InfoOf(() => QueryableEx.DistinctUntilChanged<TSource>(default(IQueryable<TSource>), default(IEqualityComparer<TSource>))),
- #else
- ((MethodInfo)MethodInfo.GetCurrentMethod()).MakeGenericMethod(typeof(TSource)),
- #endif
- source.Expression,
- Expression.Constant(comparer, typeof(IEqualityComparer<TSource>))
- )
- );
- }
- #pragma warning disable 1591
- [EditorBrowsable(EditorBrowsableState.Never)]
- public static IEnumerable<TSource> DistinctUntilChanged<TSource>(IEnumerable<TSource> source, IEqualityComparer<TSource> comparer)
- {
- return EnumerableEx.DistinctUntilChanged(source, comparer);
- }
- #pragma warning restore 1591
- /// <summary>
- /// Returns consecutive distinct elements based on a key value by using the specified equality comparer to compare key values.
- /// </summary>
- /// <typeparam name="TSource">Source sequence element type.</typeparam>
- /// <typeparam name="TKey">Key type.</typeparam>
- /// <param name="source">Source sequence.</param>
- /// <param name="keySelector">Key selector.</param>
- /// <returns>Sequence without adjacent non-distinct elements.</returns>
- public static IQueryable<TSource> DistinctUntilChanged<TSource, TKey>(this IQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector)
- {
- if (source == null)
- throw new ArgumentNullException(nameof(source));
- if (keySelector == null)
- throw new ArgumentNullException(nameof(keySelector));
- return source.Provider.CreateQuery<TSource>(
- Expression.Call(
- null,
- #if CRIPPLED_REFLECTION
- InfoOf(() => QueryableEx.DistinctUntilChanged<TSource, TKey>(default(IQueryable<TSource>), default(Expression<Func<TSource, TKey>>))),
- #else
- ((MethodInfo)MethodInfo.GetCurrentMethod()).MakeGenericMethod(typeof(TSource), typeof(TKey)),
- #endif
- source.Expression,
- keySelector
- )
- );
- }
- #pragma warning disable 1591
- [EditorBrowsable(EditorBrowsableState.Never)]
- public static IEnumerable<TSource> DistinctUntilChanged<TSource, TKey>(IEnumerable<TSource> source, Func<TSource, TKey> keySelector)
- {
- return EnumerableEx.DistinctUntilChanged(source, keySelector);
- }
- #pragma warning restore 1591
- /// <summary>
- /// Returns consecutive distinct elements based on a key value by using the specified equality comparer to compare key values.
- /// </summary>
- /// <typeparam name="TSource">Source sequence element type.</typeparam>
- /// <typeparam name="TKey">Key type.</typeparam>
- /// <param name="source">Source sequence.</param>
- /// <param name="keySelector">Key selector.</param>
- /// <param name="comparer">Comparer used to compare key values.</param>
- /// <returns>Sequence without adjacent non-distinct elements.</returns>
- public static IQueryable<TSource> DistinctUntilChanged<TSource, TKey>(this IQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector, IEqualityComparer<TKey> comparer)
- {
- if (source == null)
- throw new ArgumentNullException(nameof(source));
- if (keySelector == null)
- throw new ArgumentNullException(nameof(keySelector));
- if (comparer == null)
- throw new ArgumentNullException(nameof(comparer));
- return source.Provider.CreateQuery<TSource>(
- Expression.Call(
- null,
- #if CRIPPLED_REFLECTION
- InfoOf(() => QueryableEx.DistinctUntilChanged<TSource, TKey>(default(IQueryable<TSource>), default(Expression<Func<TSource, TKey>>), default(IEqualityComparer<TKey>))),
- #else
- ((MethodInfo)MethodInfo.GetCurrentMethod()).MakeGenericMethod(typeof(TSource), typeof(TKey)),
- #endif
- source.Expression,
- keySelector,
- Expression.Constant(comparer, typeof(IEqualityComparer<TKey>))
- )
- );
- }
- #pragma warning disable 1591
- [EditorBrowsable(EditorBrowsableState.Never)]
- public static IEnumerable<TSource> DistinctUntilChanged<TSource, TKey>(IEnumerable<TSource> source, Func<TSource, TKey> keySelector, IEqualityComparer<TKey> comparer)
- {
- return EnumerableEx.DistinctUntilChanged(source, keySelector, comparer);
- }
- #pragma warning restore 1591
- /// <summary>
- /// Expands the sequence by recursively applying a selector function.
- /// </summary>
- /// <typeparam name="TSource">Source sequence element type.</typeparam>
- /// <param name="source">Source sequence.</param>
- /// <param name="selector">Selector function to retrieve the next sequence to expand.</param>
- /// <returns>Sequence with results from the recursive expansion of the source sequence.</returns>
- public static IQueryable<TSource> Expand<TSource>(this IQueryable<TSource> source, Expression<Func<TSource, IEnumerable<TSource>>> selector)
- {
- if (source == null)
- throw new ArgumentNullException(nameof(source));
- if (selector == null)
- throw new ArgumentNullException(nameof(selector));
- return source.Provider.CreateQuery<TSource>(
- Expression.Call(
- null,
- #if CRIPPLED_REFLECTION
- InfoOf(() => QueryableEx.Expand<TSource>(default(IQueryable<TSource>), default(Expression<Func<TSource, IEnumerable<TSource>>>))),
- #else
- ((MethodInfo)MethodInfo.GetCurrentMethod()).MakeGenericMethod(typeof(TSource)),
- #endif
- source.Expression,
- selector
- )
- );
- }
- #pragma warning disable 1591
- [EditorBrowsable(EditorBrowsableState.Never)]
- public static IEnumerable<TSource> Expand<TSource>(IEnumerable<TSource> source, Func<TSource, IEnumerable<TSource>> selector)
- {
- return EnumerableEx.Expand(source, selector);
- }
- #pragma warning restore 1591
- /// <summary>
- /// Returns the source sequence prefixed with the specified value.
- /// </summary>
- /// <typeparam name="TSource">Source sequence element type.</typeparam>
- /// <param name="source">Source sequence.</param>
- /// <param name="values">Values to prefix the sequence with.</param>
- /// <returns>Sequence starting with the specified prefix value, followed by the source sequence.</returns>
- public static IQueryable<TSource> StartWith<TSource>(this IQueryable<TSource> source, params TSource[] values)
- {
- if (source == null)
- throw new ArgumentNullException(nameof(source));
- return source.Provider.CreateQuery<TSource>(
- Expression.Call(
- null,
- #if CRIPPLED_REFLECTION
- InfoOf(() => QueryableEx.StartWith<TSource>(default(IQueryable<TSource>), default(TSource[]))),
- #else
- ((MethodInfo)MethodInfo.GetCurrentMethod()).MakeGenericMethod(typeof(TSource)),
- #endif
- source.Expression,
- Expression.Constant(values, typeof(TSource[]))
- )
- );
- }
- #pragma warning disable 1591
- [EditorBrowsable(EditorBrowsableState.Never)]
- public static IEnumerable<TSource> StartWith<TSource>(IEnumerable<TSource> source, params TSource[] values)
- {
- return EnumerableEx.StartWith(source, values);
- }
- #pragma warning restore 1591
- /// <summary>
- /// Generates a sequence of accumulated values by scanning the source sequence and applying an accumulator function.
- /// </summary>
- /// <typeparam name="TSource">Source sequence element type.</typeparam>
- /// <typeparam name="TAccumulate">Accumulation type.</typeparam>
- /// <param name="source">Source sequence.</param>
- /// <param name="seed">Accumulator seed value.</param>
- /// <param name="accumulator">Accumulation function to apply to the current accumulation value and each element of the sequence.</param>
- /// <returns>Sequence with all intermediate accumulation values resulting from scanning the sequence.</returns>
- public static IQueryable<TAccumulate> Scan<TSource, TAccumulate>(this IQueryable<TSource> source, TAccumulate seed, Expression<Func<TAccumulate, TSource, TAccumulate>> accumulator)
- {
- if (source == null)
- throw new ArgumentNullException(nameof(source));
- if (accumulator == null)
- throw new ArgumentNullException(nameof(accumulator));
- return source.Provider.CreateQuery<TAccumulate>(
- Expression.Call(
- null,
- #if CRIPPLED_REFLECTION
- InfoOf(() => QueryableEx.Scan<TSource, TAccumulate>(default(IQueryable<TSource>), default(TAccumulate), default(Expression<Func<TAccumulate, TSource, TAccumulate>>))),
- #else
- ((MethodInfo)MethodInfo.GetCurrentMethod()).MakeGenericMethod(typeof(TSource), typeof(TAccumulate)),
- #endif
- source.Expression,
- Expression.Constant(seed, typeof(TAccumulate)),
- accumulator
- )
- );
- }
- #pragma warning disable 1591
- [EditorBrowsable(EditorBrowsableState.Never)]
- public static IEnumerable<TAccumulate> Scan<TSource, TAccumulate>(IEnumerable<TSource> source, TAccumulate seed, Func<TAccumulate, TSource, TAccumulate> accumulator)
- {
- return EnumerableEx.Scan(source, seed, accumulator);
- }
- #pragma warning restore 1591
- /// <summary>
- /// Generates a sequence of accumulated values by scanning the source sequence and applying an accumulator function.
- /// </summary>
- /// <typeparam name="TSource">Source sequence element type.</typeparam>
- /// <param name="source">Source sequence.</param>
- /// <param name="accumulator">Accumulation function to apply to the current accumulation value and each element of the sequence.</param>
- /// <returns>Sequence with all intermediate accumulation values resulting from scanning the sequence.</returns>
- public static IQueryable<TSource> Scan<TSource>(this IQueryable<TSource> source, Expression<Func<TSource, TSource, TSource>> accumulator)
- {
- if (source == null)
- throw new ArgumentNullException(nameof(source));
- if (accumulator == null)
- throw new ArgumentNullException(nameof(accumulator));
- return source.Provider.CreateQuery<TSource>(
- Expression.Call(
- null,
- #if CRIPPLED_REFLECTION
- InfoOf(() => QueryableEx.Scan<TSource>(default(IQueryable<TSource>), default(Expression<Func<TSource, TSource, TSource>>))),
- #else
- ((MethodInfo)MethodInfo.GetCurrentMethod()).MakeGenericMethod(typeof(TSource)),
- #endif
- source.Expression,
- accumulator
- )
- );
- }
- #pragma warning disable 1591
- [EditorBrowsable(EditorBrowsableState.Never)]
- public static IEnumerable<TSource> Scan<TSource>(IEnumerable<TSource> source, Func<TSource, TSource, TSource> accumulator)
- {
- return EnumerableEx.Scan(source, accumulator);
- }
- #pragma warning restore 1591
- /// <summary>
- /// Returns a specified number of contiguous elements from the end of the sequence.
- /// </summary>
- /// <typeparam name="TSource">Source sequence element type.</typeparam>
- /// <param name="source">Source sequence.</param>
- /// <param name="count">The number of elements to take from the end of the sequence.</param>
- /// <returns>Sequence with the specified number of elements counting from the end of the source sequence.</returns>
- public static IQueryable<TSource> TakeLast<TSource>(this IQueryable<TSource> source, int count)
- {
- if (source == null)
- throw new ArgumentNullException(nameof(source));
- return source.Provider.CreateQuery<TSource>(
- Expression.Call(
- null,
- #if CRIPPLED_REFLECTION
- InfoOf(() => QueryableEx.TakeLast<TSource>(default(IQueryable<TSource>), default(int))),
- #else
- ((MethodInfo)MethodInfo.GetCurrentMethod()).MakeGenericMethod(typeof(TSource)),
- #endif
- source.Expression,
- Expression.Constant(count, typeof(int))
- )
- );
- }
- #pragma warning disable 1591
- [EditorBrowsable(EditorBrowsableState.Never)]
- public static IEnumerable<TSource> TakeLast<TSource>(IEnumerable<TSource> source, int count)
- {
- return EnumerableEx.TakeLast(source, count);
- }
- #pragma warning restore 1591
- /// <summary>
- /// Bypasses a specified number of contiguous elements from the end of the sequence and returns the remaining elements.
- /// </summary>
- /// <typeparam name="TSource">Source sequence element type.</typeparam>
- /// <param name="source">Source sequence.</param>
- /// <param name="count">The number of elements to skip from the end of the sequence before returning the remaining elements.</param>
- /// <returns>Sequence bypassing the specified number of elements counting from the end of the source sequence.</returns>
- public static IQueryable<TSource> SkipLast<TSource>(this IQueryable<TSource> source, int count)
- {
- if (source == null)
- throw new ArgumentNullException(nameof(source));
- return source.Provider.CreateQuery<TSource>(
- Expression.Call(
- null,
- #if CRIPPLED_REFLECTION
- InfoOf(() => QueryableEx.SkipLast<TSource>(default(IQueryable<TSource>), default(int))),
- #else
- ((MethodInfo)MethodInfo.GetCurrentMethod()).MakeGenericMethod(typeof(TSource)),
- #endif
- source.Expression,
- Expression.Constant(count, typeof(int))
- )
- );
- }
- #pragma warning disable 1591
- [EditorBrowsable(EditorBrowsableState.Never)]
- public static IEnumerable<TSource> SkipLast<TSource>(IEnumerable<TSource> source, int count)
- {
- return EnumerableEx.SkipLast(source, count);
- }
- #pragma warning restore 1591
- /// <summary>
- /// Repeats and concatenates the source sequence infinitely.
- /// </summary>
- /// <typeparam name="TSource">Source sequence element type.</typeparam>
- /// <param name="source">Source sequence.</param>
- /// <returns>Sequence obtained by concatenating the source sequence to itself infinitely.</returns>
- public static IQueryable<TSource> Repeat<TSource>(this IQueryable<TSource> source)
- {
- if (source == null)
- throw new ArgumentNullException(nameof(source));
- return source.Provider.CreateQuery<TSource>(
- Expression.Call(
- null,
- #if CRIPPLED_REFLECTION
- InfoOf(() => QueryableEx.Repeat<TSource>(default(IQueryable<TSource>))),
- #else
- ((MethodInfo)MethodInfo.GetCurrentMethod()).MakeGenericMethod(typeof(TSource)),
- #endif
- source.Expression
- )
- );
- }
- #pragma warning disable 1591
- [EditorBrowsable(EditorBrowsableState.Never)]
- public static IEnumerable<TSource> Repeat<TSource>(IEnumerable<TSource> source)
- {
- return EnumerableEx.Repeat(source);
- }
- #pragma warning restore 1591
- /// <summary>
- /// Repeats and concatenates the source sequence the given number of times.
- /// </summary>
- /// <typeparam name="TSource">Source sequence element type.</typeparam>
- /// <param name="source">Source sequence.</param>
- /// <param name="count">Number of times to repeat the source sequence.</param>
- /// <returns>Sequence obtained by concatenating the source sequence to itself the specified number of times.</returns>
- public static IQueryable<TSource> Repeat<TSource>(this IQueryable<TSource> source, int count)
- {
- if (source == null)
- throw new ArgumentNullException(nameof(source));
- return source.Provider.CreateQuery<TSource>(
- Expression.Call(
- null,
- #if CRIPPLED_REFLECTION
- InfoOf(() => QueryableEx.Repeat<TSource>(default(IQueryable<TSource>), default(int))),
- #else
- ((MethodInfo)MethodInfo.GetCurrentMethod()).MakeGenericMethod(typeof(TSource)),
- #endif
- source.Expression,
- Expression.Constant(count, typeof(int))
- )
- );
- }
- #pragma warning disable 1591
- [EditorBrowsable(EditorBrowsableState.Never)]
- public static IEnumerable<TSource> Repeat<TSource>(IEnumerable<TSource> source, int count)
- {
- return EnumerableEx.Repeat(source, count);
- }
- #pragma warning restore 1591
- /// <summary>
- /// Returns a sequence with no elements.
- /// </summary>
- /// <typeparam name="TResult">Result sequence element type.</typeparam>
- /// <param name="provider">Query provider.</param>
- /// <returns>Sequence with no elements.</returns>
- public static IQueryable<TResult> Empty<TResult>(this IQueryProvider provider)
- {
- if (provider == null)
- throw new ArgumentNullException(nameof(provider));
- return provider.CreateQuery<TResult>(
- Expression.Call(
- null,
- #if CRIPPLED_REFLECTION
- InfoOf(() => QueryableEx.Empty<TResult>(default(IQueryProvider))),
- #else
- ((MethodInfo)MethodInfo.GetCurrentMethod()).MakeGenericMethod(typeof(TResult)),
- #endif
- Expression.Constant(provider, typeof(IQueryProvider))
- )
- );
- }
- #pragma warning disable 1591
- [EditorBrowsable(EditorBrowsableState.Never)]
- public static /*!*/IQueryable<TResult> Empty<TResult>()
- {
- return Enumerable.Empty<TResult>().AsQueryable();
- }
- #pragma warning restore 1591
- /// <summary>
- /// Generates a sequence of integral numbers within a specified range.
- /// </summary>
- /// <param name="provider">Query provider.</param>
- /// <param name="start">The value of the first integer in the sequence.</param>
- /// <param name="count">The number of sequential integers to generate.</param>
- /// <returns>Sequence that contains a range of sequential integral numbers.</returns>
- public static IQueryable<int> Range(this IQueryProvider provider, int start, int count)
- {
- if (provider == null)
- throw new ArgumentNullException(nameof(provider));
- return provider.CreateQuery<int>(
- Expression.Call(
- null,
- #if CRIPPLED_REFLECTION
- InfoOf(() => QueryableEx.Range(default(IQueryProvider), default(int), default(int))),
- #else
- (MethodInfo)MethodInfo.GetCurrentMethod(),
- #endif
- Expression.Constant(provider, typeof(IQueryProvider)),
- Expression.Constant(start, typeof(int)),
- Expression.Constant(count, typeof(int))
- )
- );
- }
- #pragma warning disable 1591
- [EditorBrowsable(EditorBrowsableState.Never)]
- public static /*!*/IQueryable<int> Range(int start, int count)
- {
- return Enumerable.Range(start, count).AsQueryable();
- }
- #pragma warning restore 1591
- /// <summary>
- /// Generates a sequence that contains one repeated value.
- /// </summary>
- /// <typeparam name="TResult">Result sequence element type.</typeparam>
- /// <param name="provider">Query provider.</param>
- /// <param name="element">The value to be repeated.</param>
- /// <param name="count">The number of times to repeat the value in the generated sequence.</param>
- /// <returns>Sequence that contains a repeated value.</returns>
- public static IQueryable<TResult> Repeat<TResult>(this IQueryProvider provider, TResult element, int count)
- {
- if (provider == null)
- throw new ArgumentNullException(nameof(provider));
- return provider.CreateQuery<TResult>(
- Expression.Call(
- null,
- #if CRIPPLED_REFLECTION
- InfoOf(() => QueryableEx.Repeat<TResult>(default(IQueryProvider), default(TResult), default(int))),
- #else
- ((MethodInfo)MethodInfo.GetCurrentMethod()).MakeGenericMethod(typeof(TResult)),
- #endif
- Expression.Constant(provider, typeof(IQueryProvider)),
- Expression.Constant(element, typeof(TResult)),
- Expression.Constant(count, typeof(int))
- )
- );
- }
- #pragma warning disable 1591
- [EditorBrowsable(EditorBrowsableState.Never)]
- public static /*!*/IQueryable<TResult> Repeat<TResult>(TResult element, int count)
- {
- return EnumerableEx.Repeat<TResult>(element, count).AsQueryable();
- }
- #pragma warning restore 1591
- /// <summary>
- /// Gets the local Queryable provider.
- /// </summary>
- public static IQueryProvider Provider
- {
- get
- {
- return new QueryProviderShim();
- }
- }
- class QueryProviderShim : IQueryProvider
- {
- public IQueryable<TElement> CreateQuery<TElement>(Expression expression)
- {
- var provider = new TElement[0].AsQueryable().Provider;
- var res = Redir(expression);
- return provider.CreateQuery<TElement>(res);
- }
- public IQueryable CreateQuery(Expression expression)
- {
- return CreateQuery<object>(expression);
- }
- public TResult Execute<TResult>(Expression expression)
- {
- var provider = new TResult[0].AsQueryable().Provider;
- var res = Redir(expression);
- return provider.Execute<TResult>(res);
- }
- public object Execute(Expression expression)
- {
- return Execute<object>(expression);
- }
- private static Expression Redir(Expression expression)
- {
- if (expression is MethodCallExpression mce && mce.Method.DeclaringType == typeof(QueryableEx))
- {
- if (mce.Arguments.Count >= 1 && typeof(IQueryProvider).IsAssignableFrom(mce.Arguments[0].Type))
- {
- if (mce.Arguments[0] is ConstantExpression ce)
- {
- if (ce.Value is QueryProviderShim)
- {
- var targetType = typeof(QueryableEx);
- var method = mce.Method;
- var methods = GetMethods(targetType);
- var arguments = mce.Arguments.Skip(1).ToList();
- //
- // From all the operators with the method's name, find the one that matches all arguments.
- //
- var typeArgs = method.IsGenericMethod ? method.GetGenericArguments() : null;
- var targetMethod = methods[method.Name].FirstOrDefault(candidateMethod => ArgsMatch(candidateMethod, arguments, typeArgs));
- if (targetMethod == null)
- throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, "There is no method '{0}' on type '{1}' that matches the specified arguments", method.Name, targetType.Name));
- //
- // Restore generic arguments.
- //
- if (typeArgs != null)
- targetMethod = targetMethod.MakeGenericMethod(typeArgs);
- //
- // Finally, we need to deal with mismatches on Expression<Func<...>> versus Func<...>.
- //
- var parameters = targetMethod.GetParameters();
- for (int i = 0, n = parameters.Length; i < n; i++)
- {
- arguments[i] = Unquote(arguments[i]);
- }
- //
- // Emit a new call to the discovered target method.
- //
- return Expression.Call(null, targetMethod, arguments);
- }
- }
- }
- }
- return expression;
- }
- private static ILookup<string, MethodInfo> GetMethods(Type type)
- {
- return type.GetMethods(BindingFlags.Static | BindingFlags.Public).ToLookup(m => m.Name);
- }
- private static bool ArgsMatch(MethodInfo method, IList<Expression> arguments, Type[] typeArgs)
- {
- //
- // Number of parameters should match. Notice we've sanitized IQueryProvider "this"
- // parameters first (see Redir).
- //
- var parameters = method.GetParameters();
- if (parameters.Length != arguments.Count)
- return false;
- //
- // Genericity should match too.
- //
- if (!method.IsGenericMethod && typeArgs != null && typeArgs.Length > 0)
- return false;
- //
- // Reconstruct the generic method if needed.
- //
- if (method.IsGenericMethodDefinition)
- {
- if (typeArgs == null)
- return false;
- if (method.GetGenericArguments().Length != typeArgs.Length)
- return false;
- var result = method.MakeGenericMethod(typeArgs);
- parameters = result.GetParameters();
- }
- //
- // Check compatibility for the parameter types.
- //
- for (int i = 0, n = arguments.Count; i < n; i++)
- {
- var parameterType = parameters[i].ParameterType;
- var argument = arguments[i];
- //
- // For operators that take a function (like Where, Select), we'll be faced
- // with a quoted argument and a discrepancy between Expression<Func<...>>
- // and the underlying Func<...>.
- //
- if (!parameterType.IsAssignableFrom(argument.Type))
- {
- argument = Unquote(argument);
- if (!parameterType.IsAssignableFrom(argument.Type))
- return false;
- }
- }
- return true;
- }
- private static Expression Unquote(Expression expression)
- {
- //
- // Get rid of all outer quotes around an expression.
- //
- while (expression.NodeType == ExpressionType.Quote)
- expression = ((UnaryExpression)expression).Operand;
- return expression;
- }
- }
- internal static Expression GetSourceExpression<TSource>(IEnumerable<TSource> source)
- {
- if (source is IQueryable<TSource> q)
- return q.Expression;
- return Expression.Constant(source, typeof(IEnumerable<TSource>));
- }
- internal static Expression GetSourceExpression<TSource>(IEnumerable<TSource>[] sources)
- {
- return Expression.NewArrayInit(
- typeof(IEnumerable<TSource>),
- sources.Select(source => GetSourceExpression(source))
- );
- }
- internal static MethodInfo InfoOf<R>(Expression<Func<R>> f)
- {
- return ((MethodCallExpression)f.Body).Method;
- }
- }
- }
|