.NET Core中合并Expression<Func<T,bool>>的正確姿勢

來源:互聯網
上載者:User

標籤:ret   blank   .com   問題:   參考   ict   div   條件   布爾類型   

這是在昨天的 .NET Core 遷移中遇到的問題,之前在 .NET Framework 中是這樣合并 Expression<Func<T,bool>> 的:

public static class ExpressionBuilder{    public static Expression<T> Compose<T>(this Expression<T> first, Expression<T> second, Func<Expression, Expression, Expression> merge)    {        var map = first.Parameters.Select((f, i) => new { f, s = second.Parameters[i] }).ToDictionary(p => p.s, p => p.f);        var secondBody = ParameterRebinder.ReplaceParameters(map, second.Body);        return Expression.Lambda<T>(merge(first.Body, secondBody), first.Parameters);    }    public static Expression<Func<T, bool>> And<T>(this Expression<Func<T, bool>> first, Expression<Func<T, bool>> second)    {        return first.Compose(second, Expression.And);    }        public static Expression<Func<T, bool>> Or<T>(this Expression<Func<T, bool>> first, Expression<Func<T, bool>> second)    {        return first.Compose(second, Expression.Or);    }}

遷移至 .NET Core 之後,Entity Framework Core 產生了不正確的 SQL 陳述式:

WHERE (CASE        WHEN ([q0].[IsActive] = 1) AND ([q.questionUser0].[IsActive] = 1)        THEN CAST(1 AS BIT) ELSE CAST(0 AS BIT)    END & CASE        WHEN [q0].[DealFlag] = 0        THEN CAST(1 AS BIT) ELSE CAST(0 AS BIT)    END) AND (([q0].[Flags] & @__flags_0) = @__flags_1)

在 Server Server 中執行時會出現如下的錯誤:

在應使用條件的上下文(在 ‘AND‘ 附近)中指定了非布爾類型的運算式

後來參考 Combining two expressions (Expression<Func<T, bool>>),通過下面的代碼解決了問題:

public static class ExpressionBuilder{    public static Expression<Func<T, bool>> And<T>(        this Expression<Func<T, bool>> first,         Expression<Func<T, bool>> second)    {        return first.AndAlso<T>(second, Expression.AndAlso);    }    public static Expression<Func<T, bool>> Or<T>(        this Expression<Func<T, bool>> first,         Expression<Func<T, bool>> second)    {        return first.AndAlso<T>(second, Expression.OrElse);    }    private static Expression<Func<T, bool>> AndAlso<T>(    this Expression<Func<T, bool>> expr1,    Expression<Func<T, bool>> expr2,    Func<Expression, Expression, BinaryExpression> func)    {        var parameter = Expression.Parameter(typeof(T));        var leftVisitor = new ReplaceExpressionVisitor(expr1.Parameters[0], parameter);        var left = leftVisitor.Visit(expr1.Body);        var rightVisitor = new ReplaceExpressionVisitor(expr2.Parameters[0], parameter);        var right = rightVisitor.Visit(expr2.Body);        return Expression.Lambda<Func<T, bool>>(            func(left, right), parameter);    }    private class ReplaceExpressionVisitor        : ExpressionVisitor    {        private readonly Expression _oldValue;        private readonly Expression _newValue;        public ReplaceExpressionVisitor(Expression oldValue, Expression newValue)        {            _oldValue = oldValue;            _newValue = newValue;        }        public override Expression Visit(Expression node)        {            if (node == _oldValue)                return _newValue;            return base.Visit(node);        }    }}

.NET Core中合并Expression<Func<T,bool>>的正確姿勢

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.