簡體   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