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