| | | 1 | | using System.Collections; |
| | | 2 | | using System.Reflection; |
| | | 3 | | using LinqKit; |
| | | 4 | | |
| | | 5 | | // ReSharper disable once CheckNamespace |
| | | 6 | | namespace Microsoft.EntityFrameworkCore; |
| | | 7 | | |
| | | 8 | | /// <summary>Optional-filter helpers for <see cref="IQueryable{T}"/>: a filter is skipped when its value is empty.</summ |
| | | 9 | | public static class QueryableExtensions |
| | | 10 | | { |
| | 0 | 11 | | private static readonly MethodInfo _stringContains = typeof( string ).GetMethod( nameof( string.Contains ), [ typeof |
| | | 12 | | |
| | 0 | 13 | | private static readonly MethodInfo _enumerableContains = typeof( Enumerable ).GetMethods() |
| | 0 | 14 | | .First( m => m.Name == nameof( Enumerab |
| | | 15 | | |
| | | 16 | | |
| | | 17 | | /// <summary>Keeps rows whose <paramref name="selector"/> value is in <paramref name="values"/>; no filter when <par |
| | | 18 | | /// <typeparam name="TEntity">The row type.</typeparam> |
| | | 19 | | /// <typeparam name="TValue">The value type.</typeparam> |
| | | 20 | | /// <param name="source">The query.</param> |
| | | 21 | | /// <param name="values">The allowed values.</param> |
| | | 22 | | /// <param name="selector">The property to test.</param> |
| | | 23 | | /// <returns>The filtered query.</returns> |
| | | 24 | | public static IQueryable<TEntity> FilterIfAny< TEntity, TValue >( this IQueryable<TEntity> source, ICollection<TValu |
| | | 25 | | { |
| | 0 | 26 | | ArgumentNullException.ThrowIfNull( selector ); |
| | | 27 | | |
| | 0 | 28 | | if ( values is not { Count: > 0 } ) { return source; } |
| | | 29 | | |
| | | 30 | | |
| | 0 | 31 | | var contains = Expression.Call( Expression.Constant( values ), typeof( ICollection<TValue> ).GetMethod( nameof( |
| | | 32 | | |
| | | 33 | | |
| | 0 | 34 | | return source.Where( Expression.Lambda<Func<TEntity, bool>>( contains, selector.Parameters ) ); |
| | | 35 | | } |
| | | 36 | | |
| | | 37 | | /// <summary>Keeps rows whose <paramref name="selector"/> value equals <paramref name="value"/>; no filter when it i |
| | | 38 | | /// <typeparam name="TEntity">The row type.</typeparam> |
| | | 39 | | /// <typeparam name="TValue">The value type.</typeparam> |
| | | 40 | | /// <param name="source">The query.</param> |
| | | 41 | | /// <param name="value">The value.</param> |
| | | 42 | | /// <param name="selector">The property to test.</param> |
| | | 43 | | /// <returns>The filtered query.</returns> |
| | | 44 | | public static IQueryable<TEntity> FilterIfAny< TEntity, TValue >( this IQueryable<TEntity> source, TValue? value, Ex |
| | | 45 | | { |
| | 0 | 46 | | ArgumentNullException.ThrowIfNull( selector ); |
| | | 47 | | |
| | 0 | 48 | | if ( value is null || value is string text && string.IsNullOrWhiteSpace( text ) ) { return source; } |
| | | 49 | | |
| | | 50 | | |
| | 0 | 51 | | var equal = Expression.Equal( selector.Body, Expression.Constant( value, typeof( TValue ) ) ); |
| | | 52 | | |
| | | 53 | | |
| | 0 | 54 | | return source.Where( Expression.Lambda<Func<TEntity, bool>>( equal, selector.Parameters ) ); |
| | | 55 | | } |
| | | 56 | | |
| | | 57 | | /// <summary> |
| | | 58 | | /// Keeps rows where any mapped request value (that isn't blank) equals its column (OR). When every value is blank, |
| | | 59 | | /// no rows are returned. |
| | | 60 | | /// </summary> |
| | | 61 | | /// <typeparam name="TEntity">The row type.</typeparam> |
| | | 62 | | /// <typeparam name="TRequest">The request type.</typeparam> |
| | | 63 | | /// <param name="query">The query.</param> |
| | | 64 | | /// <param name="request">The request holding the values.</param> |
| | | 65 | | /// <param name="mappings">Pairs of request value getter and column selector.</param> |
| | | 66 | | /// <returns>The filtered query.</returns> |
| | | 67 | | public static IQueryable<TEntity> FilterAnyOrNone< TEntity, TRequest >( |
| | | 68 | | this IQueryable<TEntity> query, TRequest request, |
| | | 69 | | IEnumerable<(Func<TRequest, string> getValue, Expression<Func<TEntity, string>> selector)> mappings |
| | | 70 | | ) |
| | | 71 | | { |
| | 0 | 72 | | ArgumentNullException.ThrowIfNull( mappings ); |
| | | 73 | | |
| | 0 | 74 | | var conditions = new List<Expression<Func<TEntity, bool>>>(); |
| | | 75 | | |
| | | 76 | | |
| | 0 | 77 | | foreach ( var (getValue, selector) in mappings ) |
| | | 78 | | { |
| | 0 | 79 | | var value = getValue( request ); |
| | | 80 | | |
| | 0 | 81 | | if ( string.IsNullOrWhiteSpace( value ) ) { continue; } |
| | | 82 | | |
| | | 83 | | |
| | 0 | 84 | | conditions.Add( Expression.Lambda<Func<TEntity, bool>>( Expression.Equal( selector.Body, Expression.Constant |
| | | 85 | | } |
| | | 86 | | |
| | | 87 | | |
| | 0 | 88 | | if ( conditions.Count == 0 ) { return query.Where( _ => false ); } |
| | | 89 | | |
| | | 90 | | |
| | 0 | 91 | | Expression<Func<TEntity, bool>> none = PredicateBuilder.New<TEntity>( false ); |
| | | 92 | | |
| | | 93 | | |
| | 0 | 94 | | return query.Where( conditions.Aggregate( none, ( predicate, condition ) => predicate.Or( condition ) ) ); |
| | | 95 | | } |
| | | 96 | | |
| | | 97 | | /// <summary>Keeps rows whose text <paramref name="selector"/> contains <paramref name="value"/>; no filter when it |
| | | 98 | | /// <typeparam name="TEntity">The row type.</typeparam> |
| | | 99 | | /// <typeparam name="TValue">The selected type (text).</typeparam> |
| | | 100 | | /// <param name="source">The query.</param> |
| | | 101 | | /// <param name="value">The text to look for.</param> |
| | | 102 | | /// <param name="selector">The text property.</param> |
| | | 103 | | /// <returns>The filtered query.</returns> |
| | | 104 | | public static IQueryable<TEntity> FilterIfAnyContains< TEntity, TValue >( this IQueryable<TEntity> source, string? v |
| | | 105 | | { |
| | 0 | 106 | | ArgumentNullException.ThrowIfNull( selector ); |
| | | 107 | | |
| | 0 | 108 | | if ( string.IsNullOrWhiteSpace( value ) ) { return source; } |
| | | 109 | | |
| | | 110 | | |
| | 0 | 111 | | var contains = Expression.Call( selector.Body, _stringContains, Expression.Constant( value ) ); |
| | | 112 | | |
| | | 113 | | |
| | 0 | 114 | | return source.Where( Expression.Lambda<Func<TEntity, bool>>( contains, selector.Parameters ) ); |
| | | 115 | | } |
| | | 116 | | |
| | | 117 | | /// <summary>Keeps rows whose property <paramref name="propertyName"/> equals <paramref name="value"/> (nullable pro |
| | | 118 | | /// <typeparam name="T">The row type.</typeparam> |
| | | 119 | | /// <param name="source">The query.</param> |
| | | 120 | | /// <param name="propertyName">The property name.</param> |
| | | 121 | | /// <param name="value">The value, converted to the property type.</param> |
| | | 122 | | /// <returns>The filtered query.</returns> |
| | | 123 | | /// <exception cref="ArgumentException"><typeparamref name="T"/> has no property <paramref name="propertyName"/>.</e |
| | | 124 | | public static IQueryable<T> WhereEquals< T >( this IQueryable<T> source, string propertyName, object value ) |
| | | 125 | | { |
| | 0 | 126 | | var (parameter, property, valueType, isNullable) = Property<T>( propertyName ); |
| | 0 | 127 | | var constant = Expression.Constant( Convert.ChangeType( value, valueType, C |
| | | 128 | | |
| | | 129 | | |
| | 0 | 130 | | Expression body = isNullable |
| | 0 | 131 | | ? Expression.AndAlso( Expression.Property( property, "HasValue" ), Expression.Equal( Expre |
| | 0 | 132 | | : Expression.Equal( property, constant ); |
| | | 133 | | |
| | | 134 | | |
| | 0 | 135 | | return source.Where( Expression.Lambda<Func<T, bool>>( body, parameter ) ); |
| | | 136 | | } |
| | | 137 | | |
| | | 138 | | /// <summary>Keeps rows whose property <paramref name="propertyName"/> is one of <paramref name="values"/>.</summary |
| | | 139 | | /// <typeparam name="T">The row type.</typeparam> |
| | | 140 | | /// <param name="source">The query.</param> |
| | | 141 | | /// <param name="propertyName">The property name.</param> |
| | | 142 | | /// <param name="values">The values, converted to the property type.</param> |
| | | 143 | | /// <returns>The filtered query.</returns> |
| | | 144 | | /// <exception cref="ArgumentNullException"><paramref name="values"/> is <see langword="null"/>.</exception> |
| | | 145 | | /// <exception cref="ArgumentException"><typeparamref name="T"/> has no property <paramref name="propertyName"/>.</e |
| | | 146 | | public static IQueryable<T> WhereIn< T >( this IQueryable<T> source, string propertyName, IEnumerable<object> values |
| | | 147 | | { |
| | 0 | 148 | | ArgumentNullException.ThrowIfNull( values ); |
| | | 149 | | |
| | 0 | 150 | | var (parameter, property, valueType, isNullable) = Property<T>( propertyName ); |
| | 0 | 151 | | var typedValues = (IList) Activator.CreateInstance( typeof( List<> ).MakeGener |
| | | 152 | | |
| | | 153 | | |
| | 0 | 154 | | foreach ( var item in values ) { typedValues.Add( Convert.ChangeType( item, valueType, CultureInfo.InvariantCult |
| | | 155 | | |
| | | 156 | | |
| | 0 | 157 | | var contains = _enumerableContains.MakeGenericMethod( valueType ); |
| | 0 | 158 | | var list = Expression.Constant( typedValues ); |
| | 0 | 159 | | Expression body = isNullable |
| | 0 | 160 | | ? Expression.AndAlso( Expression.Property( property, "HasValue" ), Expression.Call( co |
| | 0 | 161 | | : Expression.Call( contains, list, property ); |
| | | 162 | | |
| | | 163 | | |
| | 0 | 164 | | return source.Where( Expression.Lambda<Func<T, bool>>( body, parameter ) ); |
| | | 165 | | } |
| | | 166 | | |
| | | 167 | | |
| | | 168 | | private static (ParameterExpression Parameter, MemberExpression Property, Type ValueType, bool IsNullable) Property< |
| | | 169 | | { |
| | 0 | 170 | | var propertyInfo = typeof( T ).GetProperty( propertyName ) ?? throw new ArgumentException( $"Property '{property |
| | 0 | 171 | | var parameter = Expression.Parameter( typeof( T ), "x" ); |
| | 0 | 172 | | var underlying = Nullable.GetUnderlyingType( propertyInfo.PropertyType ); |
| | | 173 | | |
| | | 174 | | |
| | 0 | 175 | | return ( parameter, Expression.Property( parameter, propertyInfo ), underlying ?? propertyInfo.PropertyType, und |
| | | 176 | | } |
| | | 177 | | } |