|
| 1 | +using HotChocolate.Data.Filters; |
| 2 | +using HotChocolate.Execution.Processing; |
| 3 | + |
| 4 | +// ReSharper disable once CheckNamespace |
| 5 | +namespace System.Linq; |
| 6 | + |
| 7 | +/// <summary> |
| 8 | +/// Provides extension methods to integrate <see cref="IQueryable{T}"/> |
| 9 | +/// with <see cref="ISelection"/> and <see cref="IFilterContext"/>. |
| 10 | +/// </summary> |
| 11 | +public static class QueryableExtensions |
| 12 | +{ |
| 13 | + /// <summary> |
| 14 | + /// Applies a selection to the queryable. |
| 15 | + /// </summary> |
| 16 | + /// <param name="queryable"> |
| 17 | + /// The queryable that shall be projected. |
| 18 | + /// </param> |
| 19 | + /// <param name="selection"> |
| 20 | + /// The selection that shall be applied to the queryable. |
| 21 | + /// </param> |
| 22 | + /// <typeparam name="T"> |
| 23 | + /// The type of the queryable. |
| 24 | + /// </typeparam> |
| 25 | + /// <returns> |
| 26 | + /// Returns a queryable that has the selection applied. |
| 27 | + /// </returns> |
| 28 | + /// <exception cref="ArgumentNullException"> |
| 29 | + /// Throws if <paramref name="queryable"/> is <c>null</c> or if <paramref name="selection"/> is <c>null</c>. |
| 30 | + /// </exception> |
| 31 | + public static IQueryable<T> Select<T>(this IQueryable<T> queryable, ISelection selection) |
| 32 | + { |
| 33 | + if (queryable is null) |
| 34 | + { |
| 35 | + throw new ArgumentNullException(nameof(queryable)); |
| 36 | + } |
| 37 | + |
| 38 | + if (selection is null) |
| 39 | + { |
| 40 | + throw new ArgumentNullException(nameof(selection)); |
| 41 | + } |
| 42 | + |
| 43 | + return queryable.Select(selection.AsSelector<T>()); |
| 44 | + } |
| 45 | + |
| 46 | + /// <summary> |
| 47 | + /// Applies a filter context to the queryable. |
| 48 | + /// </summary> |
| 49 | + /// <param name="queryable"> |
| 50 | + /// The queryable that shall be filtered. |
| 51 | + /// </param> |
| 52 | + /// <param name="filter"> |
| 53 | + /// The filter context that shall be applied to the queryable. |
| 54 | + /// </param> |
| 55 | + /// <typeparam name="T"> |
| 56 | + /// The type of the queryable. |
| 57 | + /// </typeparam> |
| 58 | + /// <returns> |
| 59 | + /// Returns a queryable that has the filter applied. |
| 60 | + /// </returns> |
| 61 | + /// <exception cref="ArgumentNullException"> |
| 62 | + /// Throws if <paramref name="queryable"/> is <c>null</c> or if <paramref name="filter"/> is <c>null</c>. |
| 63 | + /// </exception> |
| 64 | + public static IQueryable<T> Where<T>(this IQueryable<T> queryable, IFilterContext filter) |
| 65 | + { |
| 66 | + if (queryable is null) |
| 67 | + { |
| 68 | + throw new ArgumentNullException(nameof(queryable)); |
| 69 | + } |
| 70 | + |
| 71 | + if (filter is null) |
| 72 | + { |
| 73 | + throw new ArgumentNullException(nameof(filter)); |
| 74 | + } |
| 75 | + |
| 76 | + var predicate = filter.AsPredicate<T>(); |
| 77 | + return predicate is null ? queryable : queryable.Where(predicate); |
| 78 | + } |
| 79 | +} |
0 commit comments