C# Entity Framework 核心条件投影

C# Entity Framework Core Conditional Projection

我目前使用 Entity Framework Core,它运行得非常好。但是,我试图在我的应用程序中优化的一件事是在查询时从数据库返回计算数据。我首先使用代码,其中每个模型直接映射到单个 table in.

这是我的持久性模型的简化示例:

public class User
{
    public int Id { get; set; }

    public string Name { get; set; }

    public ICollection<UserRole> Roles { get; set; }
}

public class UserRole
{
    public int Id { get; set; }

    public int UserId { get; set; }

    public User User { get; set; }

    public string Role { get; set; }
}

我目前使用的是规范模式的变体,它使我能够在执行前对查询 运行 可变数量的 .Include / .ThenInclude。但是,我希望能够有条件地启用投影的特定部分。

例如,我的领域模型可能会这样显示:

public class UserImpl
{
    public User User { get; set; }

    public int? RoleCount { get; set; }

    public static Expression<Func<User, UserImpl>> Projection(UserImplParams opts) {
        return u => new UserImpl
        {
            User = u,
            RoleCount = opts != null && opts.IncludeRoleCount ? u.Roles.Count() : default(int?)
        };
    }
}

public class UserImplParams
{
    public bool IncludeRoleCount { get; set; }
}

我想实现的是一种类似于此的方法:

var opts= new UserImplParams
{
    IncludeUserRole = true
};

await _databaseContext.Users.Select(UserImpl.Projection(opts)).ToListAsync();

我希望 EF Core 能够看到:

u => new UserImpl
{
    User = u,
    RoleCount = u.Roles.Count()
};

u => new UserImpl
{
    User = u,
    RoleCount = default(int?)
};

这可能吗?这主要是因为此表达式可能包含多个投影属性,甚至嵌套属性。每次只为少量数据将整个数据发送到数据库似乎效率低下。

[编辑] 我已经在我的网站上发布了此代码的最新版本 https://eliottrobson.me/entity-framework-core-projection-performance/ 基本相同,但增加了对更多场景的支持。


我想这样做的部分原因是过早的优化。我敢肯定,在 90% 的情况下,发送带有 CASE WHEN 1=1 或 1=0(对于 true 和 false)的大量 SQL 将得到正确优化。然而,事实是 CASE 语句并不总是短路 https://dba.stackexchange.com/questions/12941/does-sql-server-read-all-of-a-coalesce-function-even-if-the-first-argument-is-no/12945#12945.

事不宜迟,这是我关于如何实现这一目标的解决方案。

主要功能在于这个新 class:

public class ProjectionExpressionVisitor : ExpressionVisitor
{
    internal Expression<Func<TSource, TDest>> Optimise<TSource, TDest>(Expression<Func<TSource, TDest>> expression)
    {
        return Visit(expression) as Expression<Func<TSource, TDest>>;
    }

    protected override Expression VisitConditional(ConditionalExpression node)
    {
        var test = ReduceExpression(node.Test);

        // The conditional is now a constant, we can replace the branch
        if (test is ConstantExpression testNode)
        {
            var value = (dynamic) testNode.Value;
            return value ? ReduceExpression(node.IfTrue) : ReduceExpression(node.IfFalse);
        }

        // If it is not a conditional, we follow the default behaviour
        return base.VisitConditional(node);
    }

    public Expression ReduceExpression(Expression node)
    {
        if (node is ConstantExpression)
        {
            // Constants represent the smallest item, so we can just return it
            return node;
        }
        else if (node is MemberExpression memberNode)
        {
            return ReduceMemberExpression(memberNode);
        }
        else if (node is BinaryExpression binaryNode)
        {
            return ReduceBinaryExpression(binaryNode);
        }

        // This is not a supported expression type to reduce, fallback to default
        return node;
    }

    public Expression ReduceMemberExpression(MemberExpression node)
    {
        if (
            node.Expression.NodeType == ExpressionType.Constant ||
            node.Expression.NodeType == ExpressionType.MemberAccess
        )
        {
            var objectMember = Expression.Convert(node, typeof(object));
            var getterLambda = Expression.Lambda<Func<object>>(objectMember);
            var getter = getterLambda.Compile();
            var value = getter();

            return Expression.Constant(value);
        }

        return node;
    }

    public Expression ReduceBinaryExpression(BinaryExpression node)
    {
        var left = ReduceExpression(node.Left);
        var right = ReduceExpression(node.Right);

        var leftConst = left as ConstantExpression;
        var rightConst = right as ConstantExpression;

        // Special optimisations
        var optimised = OptimiseBooleanBinaryExpression(node.NodeType, leftConst, rightConst);
        if (optimised != null) return Expression.Constant(optimised);

        if (leftConst != null && rightConst != null)
        {
            var leftValue = (dynamic)leftConst.Value;
            var rightValue = (dynamic)rightConst.Value;

            switch (node.NodeType)
            {
                case ExpressionType.Add:
                    return Expression.Constant(leftValue + rightValue);
                case ExpressionType.Divide:
                    return Expression.Constant(leftValue / rightValue);
                case ExpressionType.Modulo:
                    return Expression.Constant(leftValue % rightValue);
                case ExpressionType.Multiply:
                    return Expression.Constant(leftValue * rightValue);
                case ExpressionType.Power:
                    return Expression.Constant(leftValue ^ rightValue);
                case ExpressionType.Subtract:
                    return Expression.Constant(leftValue - rightValue);
                case ExpressionType.And:
                    return Expression.Constant(leftValue & rightValue);
                case ExpressionType.AndAlso:
                    return Expression.Constant(leftValue && rightValue);
                case ExpressionType.Or:
                    return Expression.Constant(leftValue | rightValue);
                case ExpressionType.OrElse:
                    return Expression.Constant(leftValue || rightValue);
                case ExpressionType.Equal:
                    return Expression.Constant(leftValue == rightValue);
                case ExpressionType.NotEqual:
                    return Expression.Constant(leftValue != rightValue);
                case ExpressionType.GreaterThan:
                    return Expression.Constant(leftValue > rightValue);
                case ExpressionType.GreaterThanOrEqual:
                    return Expression.Constant(leftValue >= rightValue);
                case ExpressionType.LessThan:
                    return Expression.Constant(leftValue < rightValue);
                case ExpressionType.LessThanOrEqual:
                    return Expression.Constant(leftValue <= rightValue);
            }
        }

        return node;
    }

    private bool? OptimiseBooleanBinaryExpression(ExpressionType type, ConstantExpression leftConst, ConstantExpression rightConst)
    {
        // This is only a necessary optimisation when only part of the binary expression is constant
        if (leftConst != null && rightConst != null)
            return null;

        var leftValue = (dynamic)leftConst?.Value;
        var rightValue = (dynamic)rightConst?.Value;

        // We can check for constants on each side to simplify the reduction process
        if (
            (type == ExpressionType.And || type == ExpressionType.AndAlso) &&
            (leftValue == false || rightValue == false))
        {
            return false;
        }
        else if (
            (type == ExpressionType.Or || type == ExpressionType.OrElse) &&
            (leftValue == true || rightValue == true))
        {
            return true;
        }

        return null;
    }
}

从根本上说,我们的想法是通过尽可能减少条件表达式来优化它们,然后在混合参数 lambda 时应用一些特殊情况逻辑。

用法如下

var opts = new UserImplParams
{
    IncludeUserRole = true
};

var projection = UserImpl.Projection(opts);

var expression = new ProjectionExpressionVisitor().Optimise(projection);

await _databaseContext.Users.Select(expression).ToListAsync();

希望这对遇到类似问题的其他人有所帮助。

可以有条件的先改组名(在第一个select),然后再组,
现在您有两种类型的组

            .GroupBy(x => new {x.Brand})
            .Select(x => new DisputeReportListModel
            {
                Amount = x.Sum(y => y.Amount),
                Scheme = _isMastercard(x.Key.Brand) ? "MASTERCARD" : "VISA",
            }).AsEnumerable()
            .GroupBy(x => new {x.Scheme})
            .Select(x => new DisputeReportListModel
            {
                Amount = x.Sum(y => y.Amount),
                Scheme = x.Key.Scheme
            })
            .ToList();