| | 1 | | using Plainquire.Filter.Abstractions.ExpressionVisitors; |
| | 2 | | using System; |
| | 3 | | using System.Collections.Generic; |
| | 4 | | using System.Diagnostics.CodeAnalysis; |
| | 5 | | using System.Linq; |
| | 6 | | using System.Linq.Expressions; |
| | 7 | | using System.Reflection; |
| | 8 | |
|
| | 9 | | namespace Plainquire.Filter.Abstractions; |
| | 10 | |
|
| | 11 | | /// <summary> |
| | 12 | | /// Extension methods for <see cref="Expression"/>. |
| | 13 | | /// </summary> |
| | 14 | | [SuppressMessage("ReSharper", "UnusedMember.Global", Justification = "Provided as library, can be used from outside")] |
| | 15 | | public static class ExpressionExtensions |
| | 16 | | { |
| 2 | 17 | | private static readonly ConstantExpression _emptyString = Expression.Constant(string.Empty, typeof(string)); |
| 2 | 18 | | private static readonly MethodInfo _stringToUpperMethodInfo = typeof(string).GetMethod(nameof(string.ToUpper), Type. |
| 2 | 19 | | private static readonly MethodInfo _stringContainsMethodInfo = typeof(string).GetMethod(nameof(string.Contains), [ty |
| 2 | 20 | | private static readonly MethodInfo _stringStartsWithMethodInfo = typeof(string).GetMethod(nameof(string.StartsWith), |
| 2 | 21 | | private static readonly MethodInfo _stringEndsWithMethodInfo = typeof(string).GetMethod(nameof(string.EndsWith), [ty |
| 2 | 22 | | private static readonly MethodInfo _enumerableAnyMethodInfo = typeof(Enumerable).GetMethods().First(method => method |
| 2 | 23 | | private static readonly MethodInfo _objectToStringMethodInfo = typeof(object).GetMethods().First(method => method.Na |
| | 24 | |
|
| | 25 | | /// <summary> |
| | 26 | | /// Combines a property and an expression body to a lambda expression. |
| | 27 | | /// </summary> |
| | 28 | | /// <typeparam name="TEntity">The type of the class that declares <typeparamref name="TProperty"/>.</typeparam> |
| | 29 | | /// <typeparam name="TProperty">The type of the property.</typeparam> |
| | 30 | | /// <typeparam name="TResult">The type of the result.</typeparam> |
| | 31 | | /// <param name="propertySelector">The property to use as first parameter.</param> |
| | 32 | | /// <param name="expression">The expression body to use.</param> |
| | 33 | | public static Expression<Func<TEntity, TResult>> CreateLambda<TEntity, TProperty, TResult>(this Expression<Func<TEnt |
| 16115 | 34 | | => Expression.Lambda<Func<TEntity, TResult>>(expression, propertySelector.Parameters); |
| | 35 | |
|
| | 36 | | /// <summary> |
| | 37 | | /// Combines a property and an expression body to a lambda expression. |
| | 38 | | /// </summary> |
| | 39 | | /// <typeparam name="TEntity">The type of the class that declares member from <paramref name="propertySelector"/>.</ |
| | 40 | | /// <typeparam name="TResult">The type of the result.</typeparam> |
| | 41 | | /// <param name="propertySelector">The property to use as first parameter.</param> |
| | 42 | | /// <param name="expression">The expression body to use.</param> |
| | 43 | | public static Expression<Func<TEntity, TResult>> CreateLambda<TEntity, TResult>(this LambdaExpression propertySelect |
| 31 | 44 | | => Expression.Lambda<Func<TEntity, TResult>>(expression, propertySelector.Parameters); |
| | 45 | |
|
| | 46 | | /// <summary> |
| | 47 | | /// Combines multiple expression with conditional AND. |
| | 48 | | /// </summary> |
| | 49 | | /// <typeparam name="TSource">The type of the source.</typeparam> |
| | 50 | | /// <param name="expressions">The expressions to combine.</param> |
| | 51 | | public static Expression<Func<TSource, bool>>? CombineWithConditionalAnd<TSource>(this IEnumerable<Expression<Func<T |
| 16347 | 52 | | => expressions.Combine(Expression.AndAlso); |
| | 53 | |
|
| | 54 | | /// <summary> |
| | 55 | | /// Combines multiple expression with conditional OR. |
| | 56 | | /// </summary> |
| | 57 | | /// <typeparam name="TSource">The type of the source.</typeparam> |
| | 58 | | /// <param name="expressions">The expressions to combine.</param> |
| | 59 | | public static Expression<Func<TSource, bool>>? CombineWithConditionalOr<TSource>(this IEnumerable<Expression<Func<TS |
| 6 | 60 | | => expressions.Combine(Expression.OrElse); |
| | 61 | |
|
| | 62 | | /// <summary> |
| | 63 | | /// Replaces the parameter of a lambda expression (<c>x => true</c> => <c>y => true</c>). |
| | 64 | | /// </summary> |
| | 65 | | /// <param name="origin">The origin lambda expression.</param> |
| | 66 | | /// <param name="replacement">The lambda expression holding the replacement as parameter.</param> |
| | 67 | | public static LambdaExpression ReplaceParameter(this LambdaExpression origin, LambdaExpression replacement) |
| | 68 | | { |
| 15 | 69 | | var body = new ExpressionReplaceVisitor(origin.Parameters[0], replacement.Body).Visit(origin.Body); |
| 15 | 70 | | return Expression.Lambda(body, replacement.Parameters[0]); |
| | 71 | | } |
| | 72 | |
|
| | 73 | | /// <summary> |
| | 74 | | /// Creates typed property selector for the given type. |
| | 75 | | /// </summary> |
| | 76 | | /// <param name="declaringType">The Type of the declaring class/struct/record.</param> |
| | 77 | | /// <param name="propertyName">The Name of the property.</param> |
| | 78 | | public static Expression<Func<TEntity, TProperty>> CreatePropertySelector<TEntity, TProperty>(this Type declaringTyp |
| 6 | 79 | | => (Expression<Func<TEntity, TProperty>>)declaringType.CreatePropertySelector(propertyName); |
| | 80 | |
|
| | 81 | | /// <summary> |
| | 82 | | /// Creates an untyped property selector for the given type. |
| | 83 | | /// </summary> |
| | 84 | | /// <param name="declaringType">The Type of the declaring class/struct/record.</param> |
| | 85 | | /// <param name="propertyName">The Name of the property.</param> |
| | 86 | | public static LambdaExpression CreatePropertySelector(this Type declaringType, string propertyName) |
| | 87 | | { |
| 18226 | 88 | | var propertyParameter = Expression.Parameter(declaringType, "x"); |
| 18226 | 89 | | var propertyExpression = Expression.Property(propertyParameter, propertyName); |
| 18226 | 90 | | return Expression.Lambda(propertyExpression, propertyParameter); |
| | 91 | | } |
| | 92 | |
|
| | 93 | | /// <summary> |
| | 94 | | /// Applies a call to <see cref="object.ToString()"/> to the given expression. |
| | 95 | | /// </summary> |
| | 96 | | /// <param name="expression">The expression to add the call.</param> |
| | 97 | | public static Expression ObjectToString(this Expression expression) |
| 1512 | 98 | | => Expression.Call(expression, _objectToStringMethodInfo); |
| | 99 | |
|
| | 100 | | /// <summary> |
| | 101 | | /// Adds a cast to the given expression. |
| | 102 | | /// </summary> |
| | 103 | | /// <param name="expression">The expression to cast.</param> |
| | 104 | | /// <param name="sourceType">The type of the source.</param> |
| | 105 | | /// <param name="destinationType">The type to cast to.</param> |
| | 106 | | public static Expression Cast(this Expression expression, Type sourceType, Type destinationType) |
| 1056 | 107 | | => sourceType != destinationType |
| 1056 | 108 | | ? Expression.Convert(expression, destinationType) |
| 1056 | 109 | | : expression; |
| | 110 | |
|
| | 111 | | /// <summary> |
| | 112 | | /// Applies a call to <see cref="string.ToUpper()"/> to the given expression. |
| | 113 | | /// </summary> |
| | 114 | | /// <param name="expression">The expression to add the call.</param> |
| | 115 | | public static Expression StringToUpper(this Expression expression) |
| 4400 | 116 | | => Expression.Call(expression, _stringToUpperMethodInfo); |
| | 117 | |
|
| | 118 | | /// <summary> |
| | 119 | | /// Applies a call to <see cref="string.Contains(string)"/> to the given expression. |
| | 120 | | /// </summary> |
| | 121 | | /// <param name="expression">The expression to add the call.</param> |
| | 122 | | /// <param name="value">The value expression to seek</param> |
| | 123 | | public static Expression StringContains(this Expression expression, Expression value) |
| 1096 | 124 | | => Expression.Call(expression, _stringContainsMethodInfo, value); |
| | 125 | |
|
| | 126 | | /// <summary> |
| | 127 | | /// Applies a call to <see cref="string.StartsWith(string)"/> to the given expression. |
| | 128 | | /// </summary> |
| | 129 | | /// <param name="expression">The expression to add the call.</param> |
| | 130 | | /// <param name="value">The value expression to seek</param> |
| | 131 | | public static Expression StringStartsWith(this Expression expression, Expression value) |
| 1206 | 132 | | => Expression.Call(expression, _stringStartsWithMethodInfo, value); |
| | 133 | |
|
| | 134 | | /// <summary> |
| | 135 | | /// Applies a call to <see cref="string.EndsWith(string)"/> to the given expression. |
| | 136 | | /// </summary> |
| | 137 | | /// <param name="expression">The expression to add the call.</param> |
| | 138 | | /// <param name="value">The value expression to seek</param> |
| | 139 | | public static Expression StringEndsWith(this Expression expression, Expression value) |
| 768 | 140 | | => Expression.Call(expression, _stringEndsWithMethodInfo, value); |
| | 141 | |
|
| | 142 | | /// <summary> |
| | 143 | | /// Creates an expression indicates whether the specified property equals <c>null</c> |
| | 144 | | /// </summary> |
| | 145 | | /// <typeparam name="TEntity">The type of the class that declares <typeparamref name="TProperty"/>.</typeparam> |
| | 146 | | /// <typeparam name="TProperty">The type of the property.</typeparam> |
| | 147 | | /// <param name="property">The property to check for <c>null</c>.</param> |
| | 148 | | public static Expression IsNull<TEntity, TProperty>(this Expression<Func<TEntity, TProperty>> property) |
| 0 | 149 | | => Expression.Equal(property.Body, Expression.Constant(null, typeof(TProperty))); |
| | 150 | |
|
| | 151 | | /// <summary> |
| | 152 | | /// Creates an expression indicates whether the specified property not equals <c>null</c> |
| | 153 | | /// </summary> |
| | 154 | | /// <typeparam name="TEntity">The type of the class that declares <typeparamref name="TProperty"/>.</typeparam> |
| | 155 | | /// <typeparam name="TProperty">The type of the property.</typeparam> |
| | 156 | | /// <param name="property">The property to check for <c>null</c>.</param> |
| | 157 | | public static Expression IsNotNull<TEntity, TProperty>(this Expression<Func<TEntity, TProperty>> property) |
| 2888 | 158 | | => IsNotNull(property, typeof(TProperty)); |
| | 159 | |
|
| | 160 | | /// <summary> |
| | 161 | | /// Creates an expression indicates whether the specified property not equals <c>null</c> |
| | 162 | | /// </summary> |
| | 163 | | /// <param name="property">The property to check for <c>null</c>.</param> |
| | 164 | | /// <param name="propertyType">The type of the property.</param> |
| | 165 | | public static Expression IsNotNull(this LambdaExpression property, Type propertyType) |
| 2919 | 166 | | => Expression.NotEqual(property.Body, Expression.Constant(null, propertyType)); |
| | 167 | |
|
| | 168 | | /// <summary> |
| | 169 | | /// Creates an expression indicates whether the specified property equals <see cref="string.Empty"/>. |
| | 170 | | /// </summary> |
| | 171 | | /// <typeparam name="TEntity">The type of the class that declares <typeparamref name="TProperty"/>.</typeparam> |
| | 172 | | /// <typeparam name="TProperty">The type of the property.</typeparam> |
| | 173 | | /// <param name="property">The property to check.</param> |
| | 174 | | public static Expression StringIsEmpty<TEntity, TProperty>(this Expression<Func<TEntity, TProperty>> property) |
| 246 | 175 | | => Expression.Equal(property.Body, _emptyString); |
| | 176 | |
|
| | 177 | | /// <summary> |
| | 178 | | /// Applies a call to <see cref="Enumerable.Any{TSource}(IEnumerable{TSource})"/> to the given expression. |
| | 179 | | /// </summary> |
| | 180 | | /// <param name="property">The property to add the call.</param> |
| | 181 | | /// <param name="propertyType">The type of the property.</param> |
| | 182 | | /// <param name="predicate">The nested predicate.</param> |
| | 183 | | public static LambdaExpression EnumerableAny(this LambdaExpression property, Type propertyType, LambdaExpression pre |
| | 184 | | { |
| 17 | 185 | | var genericEnumerableAnyMethod = _enumerableAnyMethodInfo.MakeGenericMethod(propertyType); |
| 17 | 186 | | var propertyContainsAnyMatchesPredicate = Expression.Call(genericEnumerableAnyMethod, property.Body, predicate); |
| 17 | 187 | | return Expression.Lambda(propertyContainsAnyMatchesPredicate, property.Parameters); |
| | 188 | | } |
| | 189 | |
|
| | 190 | | private static Expression<Func<TSource, bool>>? Combine<TSource>(this IEnumerable<Expression<Func<TSource, bool>>?> |
| | 191 | | { |
| 16353 | 192 | | var notNullExpressions = expressions |
| 16353 | 193 | | .WhereNotNull() |
| 16353 | 194 | | .ToList(); |
| | 195 | |
|
| 16353 | 196 | | if (!notNullExpressions.Any()) |
| 220 | 197 | | return null; |
| | 198 | |
|
| 16133 | 199 | | return notNullExpressions.Aggregate((first, second) => |
| 16133 | 200 | | { |
| 16133 | 201 | | // Parameter rewrite doesn't work with 'Invoke' in EF Core 3.x anymore. |
| 16133 | 202 | | // Use the good old ExpressionVisitor, example at https://stackoverflow.com/a/5431309/1271211 |
| 16133 | 203 | | var replacedSecond = new ExpressionParameterReplaceVisitor(second.Parameters, first.Parameters).VisitAndConv |
| 16133 | 204 | | var conditionalExpr = fn(first.Body, replacedSecond); |
| 16133 | 205 | | var finalExpr = Expression.Lambda<Func<TSource, bool>>(conditionalExpr, first.Parameters); |
| 16133 | 206 | | return finalExpr; |
| 16133 | 207 | | }); |
| | 208 | | } |
| | 209 | | } |