< Summary

Information
Class: Pozitron.QuerySpecification.LikeExtension
Assembly: Pozitron.QuerySpecification.EntityFrameworkCore
File(s): /home/runner/work/QuerySpecification/QuerySpecification/src/QuerySpecification.EntityFrameworkCore/Extensions/LikeExtension.cs
Tag: 44_11195777782
Line coverage
100%
Covered lines: 22
Uncovered lines: 0
Coverable lines: 22
Total lines: 48
Line coverage: 100%
Branch coverage
100%
Covered branches: 6
Total branches: 6
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.cctor()100%11100%
Like(...)100%66100%

File(s)

/home/runner/work/QuerySpecification/QuerySpecification/src/QuerySpecification.EntityFrameworkCore/Extensions/LikeExtension.cs

#LineLine coverage
 1using System.Data;
 2using System.Diagnostics;
 3using System.Reflection;
 4
 5namespace Pozitron.QuerySpecification;
 6
 7internal static class LikeExtension
 8{
 19    private static readonly MethodInfo _likeMethodInfo = typeof(DbFunctionsExtensions)
 110        .GetMethod(nameof(DbFunctionsExtensions.Like), [typeof(DbFunctions), typeof(string), typeof(string)])!;
 11
 112    private static readonly PropertyInfo _functionsProp = typeof(EF).GetProperty(nameof(EF.Functions))!;
 113    private static readonly MemberExpression _functions = Expression.Property(null, _functionsProp);
 14
 15    public static IQueryable<T> Like<T>(this IQueryable<T> source, IEnumerable<LikeExpression<T>> likeExpressions)
 16    {
 17        Debug.Assert(_likeMethodInfo is not null);
 18        Debug.Assert(_functionsProp is not null);
 19
 2120        Expression? expr = null;
 2121        var parameter = Expression.Parameter(typeof(T), "x");
 22
 10223        foreach (var likeExpression in likeExpressions)
 24        {
 3025            var propertySelector = ParameterReplacerVisitor.Replace(
 3026                likeExpression.KeySelector,
 3027                likeExpression.KeySelector.Parameters[0],
 3028                parameter) as LambdaExpression;
 29
 30            Debug.Assert(propertySelector is not null);
 31
 3032            var patternAsExpression = ((Expression<Func<string>>)(() => likeExpression.Pattern)).Body;
 33
 3034            var efLikeExpression = Expression.Call(
 3035                null,
 3036                _likeMethodInfo,
 3037                _functions,
 3038                propertySelector.Body,
 3039                patternAsExpression);
 40
 3041            expr = expr is null ? efLikeExpression : Expression.OrElse(expr, efLikeExpression);
 42        }
 43
 2144        return expr is null
 2145            ? source
 2146            : source.Where(Expression.Lambda<Func<T, bool>>(expr, parameter));
 47    }
 48}