繁体   English   中英

C#实体框架核心条件投影

[英]C# Entity Framework Core Conditional Projection

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

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

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 EITHER 看到:

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(真假)的大量 SQL 将被正确优化。 然而,事实是 CASE 语句并不总是短路https://dba.stackexchange.com/questions/12941/does-sql-server-read-all-of-a-coalesce-function-even-if-第一个参数是没有/12945#12945

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

主要功能在这个新类中:

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();

希望这会帮助其他有类似问题的人。

您可以先有条件地更改组名(在第一个选择中),然后再次分组,
现在你有两种类型的组

            .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();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM