< Summary

Information
Class: Elsa.Persistence.EFCore.Extensions.ExpressionExtensions
Assembly: Elsa.Persistence.EFCore.Common
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Persistence.EFCore.Common/Extensions/ExpressionExtensions.cs
Line coverage
41%
Covered lines: 7
Uncovered lines: 10
Coverable lines: 17
Total lines: 59
Line coverage: 41.1%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
BuildContainsExpression(...)100%210%
BuildEqualsExpression(...)100%11100%

File(s)

/home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Persistence.EFCore.Common/Extensions/ExpressionExtensions.cs

#LineLine coverage
 1using System.Linq.Expressions;
 2using Elsa.Extensions;
 3
 4// ReSharper disable once CheckNamespace
 5namespace Elsa.Persistence.EFCore.Extensions;
 6
 7/// <summary>
 8/// Provides extension methods for <see cref="Expression"/> objects.
 9/// </summary>
 10public 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    {
 021        var compiledKeySelector = keySelector.Compile();
 022        var list = entities.Select(compiledKeySelector).ToList();
 023        var property = keySelector.GetProperty()!;
 024        var param = Expression.Parameter(typeof(TEntity));
 25
 026        var body = Expression.Call(
 027            typeof(Enumerable),
 028            "Contains",
 029            new[] {compiledKeySelector.Method.ReturnType},
 030            Expression.Constant(list), Expression.Property(param, property));
 31
 032        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    {
 39644        var keyName = keySelector.GetProperty()!.Name;
 45
 46        // Define parameters for the lambda expression
 39647        var parameter = Expression.Parameter(typeof(TEntity), "x");
 39648        var keySelectorLambda = Expression.Lambda<Func<TEntity, string>>(Expression.Property(parameter, keyName), parame
 49
 50        // Build the expression that compares the keys
 39651        var entityKey = keySelectorLambda.Compile()(entity);
 39652        var comparison = Expression.Equal(keySelectorLambda.Body, Expression.Constant(entityKey));
 53
 54        // Create the final lambda expression that can be used in AnyAsync
 39655        var lambda = Expression.Lambda<Func<TEntity, bool>>(comparison, parameter);
 56
 39657        return lambda;
 58    }
 59}