| | | 1 | | using System.Linq.Expressions; |
| | | 2 | | using Elsa.Extensions; |
| | | 3 | | |
| | | 4 | | // ReSharper disable once CheckNamespace |
| | | 5 | | namespace Elsa.Persistence.EFCore.Extensions; |
| | | 6 | | |
| | | 7 | | /// <summary> |
| | | 8 | | /// Provides extension methods for <see cref="Expression"/> objects. |
| | | 9 | | /// </summary> |
| | | 10 | | public static class ExpressionExtensions |
| | | 11 | | { |
| | | 12 | | /// <summary> |
| | | 13 | | /// Builds an expression that checks if the specified property is contained in the specified list of entities. |
| | | 14 | | /// </summary> |
| | | 15 | | /// <param name="keySelector">The key selector.</param> |
| | | 16 | | /// <param name="entities">The entities.</param> |
| | | 17 | | /// <typeparam name="TEntity">The type of the entity.</typeparam> |
| | | 18 | | /// <returns>The expression.</returns> |
| | | 19 | | public static Expression<Func<TEntity, bool>> BuildContainsExpression<TEntity>(this Expression<Func<TEntity, string> |
| | | 20 | | { |
| | 0 | 21 | | var compiledKeySelector = keySelector.Compile(); |
| | 0 | 22 | | var list = entities.Select(compiledKeySelector).ToList(); |
| | 0 | 23 | | var property = keySelector.GetProperty()!; |
| | 0 | 24 | | var param = Expression.Parameter(typeof(TEntity)); |
| | | 25 | | |
| | 0 | 26 | | var body = Expression.Call( |
| | 0 | 27 | | typeof(Enumerable), |
| | 0 | 28 | | "Contains", |
| | 0 | 29 | | new[] {compiledKeySelector.Method.ReturnType}, |
| | 0 | 30 | | Expression.Constant(list), Expression.Property(param, property)); |
| | | 31 | | |
| | 0 | 32 | | return Expression.Lambda<Func<TEntity, bool>>(body, param); |
| | | 33 | | } |
| | | 34 | | |
| | | 35 | | /// <summary> |
| | | 36 | | /// Builds an expression that checks if the specified property is equal to the specified entity's property. |
| | | 37 | | /// </summary> |
| | | 38 | | /// <param name="keySelector">The key selector.</param> |
| | | 39 | | /// <param name="entity">The entity.</param> |
| | | 40 | | /// <typeparam name="TEntity">The type of the entity.</typeparam> |
| | | 41 | | /// <returns>The expression.</returns> |
| | | 42 | | public static Expression<Func<TEntity, bool>> BuildEqualsExpression<TEntity>(this Expression<Func<TEntity, string>> |
| | | 43 | | { |
| | 396 | 44 | | var keyName = keySelector.GetProperty()!.Name; |
| | | 45 | | |
| | | 46 | | // Define parameters for the lambda expression |
| | 396 | 47 | | var parameter = Expression.Parameter(typeof(TEntity), "x"); |
| | 396 | 48 | | var keySelectorLambda = Expression.Lambda<Func<TEntity, string>>(Expression.Property(parameter, keyName), parame |
| | | 49 | | |
| | | 50 | | // Build the expression that compares the keys |
| | 396 | 51 | | var entityKey = keySelectorLambda.Compile()(entity); |
| | 396 | 52 | | var comparison = Expression.Equal(keySelectorLambda.Body, Expression.Constant(entityKey)); |
| | | 53 | | |
| | | 54 | | // Create the final lambda expression that can be used in AnyAsync |
| | 396 | 55 | | var lambda = Expression.Lambda<Func<TEntity, bool>>(comparison, parameter); |
| | | 56 | | |
| | 396 | 57 | | return lambda; |
| | | 58 | | } |
| | | 59 | | } |