栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

结合两个表达式(表达式>)

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

结合两个表达式(表达式>)

好了,您可以使用

expression.AndAlso
/
OrElse
etc组合逻辑表达式,但是问题是参数;您
Parameterexpression
在expr1和expr2中使用相同的代码吗?如果是这样,则更容易:

var body = expression.AndAlso(expr1.Body, expr2.Body);var lambda = expression.Lambda<Func<T,bool>>(body, expr1.Parameters[0]);

这也可以很好地否定单个操作:

static expression<Func<T, bool>> Not<T>(    this expression<Func<T, bool>> expr){    return expression.Lambda<Func<T, bool>>(        expression.Not(expr.Body), expr.Parameters[0]);}

否则,根据LINQ提供程序,您可能可以将它们与

Invoke

// OrElse is very similar...static expression<Func<T, bool>> AndAlso<T>(    this expression<Func<T, bool>> left,    expression<Func<T, bool>> right){    var param = expression.Parameter(typeof(T), "x");    var body = expression.AndAlso( expression.Invoke(left, param), expression.Invoke(right, param)        );    var lambda = expression.Lambda<Func<T, bool>>(body, param);    return lambda;}

在某个地方,我有一些代码重新编写了一个替换节点的表达式树,以消除对的需要

Invoke
,但是它相当长(而且我不记得我把它留在哪里了……)。


选择最简单路线的通用版本:

static expression<Func<T, bool>> AndAlso<T>(    this expression<Func<T, bool>> expr1,    expression<Func<T, bool>> expr2){    // need to detect whether they use the same    // parameter instance; if not, they need fixing    Parameterexpression param = expr1.Parameters[0];    if (ReferenceEquals(param, expr2.Parameters[0]))    {        // simple version        return expression.Lambda<Func<T, bool>>( expression.AndAlso(expr1.Body, expr2.Body), param);    }    // otherwise, keep expr1 "as is" and invoke expr2    return expression.Lambda<Func<T, bool>>(        expression.AndAlso( expr1.Body, expression.Invoke(expr2, param)), param);}

从.NET 4.0开始,有一个

expressionVisitor
类允许您构建EF安全的表达式。

    public static expression<Func<T, bool>> AndAlso<T>(        this expression<Func<T, bool>> expr1,        expression<Func<T, bool>> expr2)    {        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>>( expression.AndAlso(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);        }    }


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/596970.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号