这个PredicateBuilder类如何工作? [英] How does this PredicateBuilder class work?

查看:137
本文介绍了这个PredicateBuilder类如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在阅读约瑟夫·阿尔巴哈里在C#4.0的精彩书,我遇到了这个类:

  public static class PredicateBuilder 
{
public static Expression< Func< T,bool>真< T> (){return f =>真正; }
public static Expression< Func< T,bool>> F< T> (){return f =>假; }

public static Expression< Func< T,bool>>或< T> (这个表达式< Func< T,bool> expr1,
Expression< Func< T,bool> expr2)
{
var invokedExpr = Expression.Invoke(expr2,expr1。 Parameters.Cast< Expression>());

return Expression.Lambda< Func< T,bool>>
(Expression.OrElse(expr1.Body,invokedExpr),expr1.Parameters);
}

public static Expression< Func< T,bool> < T> (这个表达式< Func< T,bool> expr1,
Expression< Func< T,bool> expr2)
{
var invokedExpr = Expression.Invoke(expr2,expr1。 Parameters.Cast< Expression>());
return Expression.Lambda< Func< T,bool>>
(Expression.AndAlso(expr1.Body,invokedExpr),expr1.Parameters);
}
}

任何人都可以向我解释这个函数的作用,怎么运行的?我知道它是用来添加条件到表达式树,但它实际上如何工作?我从来没有使用过这些类,如Expression等。这个具体的代码在做什么?

  var invokedExpr = Expression.Invoke(expr2,expr1.Parameters.Cast< Expression>()); 

return Expression.Lambda< Func< T,bool>>
(Expression.OrElse(expr1.Body,invokedExpr),expr1.Parameters);

我知道Func是一个委托,应该返回true或false,



提前感谢:)

解决方案

a href =http://msdn.microsoft.com/en-us/library/bb397951.aspx =nofollow>表达式树树从表示谓词的两个输入表达式构建谓词。 / p>

表达式树是一种使用lambda在树状结构(而不​​是直接的委托)中生成代码表示的方法。这需要代表谓词( Expression > )的两个表达式树,并将它们组合成表示或 和case in the second method)。



表达式树及其相应的实用程序如上所述,对于像ORM这样的东西很有用。例如,Entity Framework使用具有 IQueryable< T> 的表达式树将定义为lambda的代码转换为在服务器上运行的SQL。


I have been reading Joseph Albahari's brilliant book on C# 4.0 and I came across this class:

public static class PredicateBuilder
    {
        public static Expression<Func<T, bool>> True<T> () { return f => true; }
        public static Expression<Func<T, bool>> False<T> () { return f => false; }

        public static Expression<Func<T, bool>> Or<T> (this Expression<Func<T, bool>> expr1,
                                                  Expression<Func<T, bool>> expr2)
        {
            var invokedExpr = Expression.Invoke (expr2, expr1.Parameters.Cast<Expression> ());

            return Expression.Lambda<Func<T, bool>>
                 (Expression.OrElse (expr1.Body, invokedExpr), expr1.Parameters);
        }

        public static Expression<Func<T, bool>> And<T> (this Expression<Func<T, bool>> expr1,
                                                   Expression<Func<T, bool>> expr2)
        {
            var invokedExpr = Expression.Invoke (expr2, expr1.Parameters.Cast<Expression> ());
            return Expression.Lambda<Func<T, bool>>
                 (Expression.AndAlso (expr1.Body, invokedExpr), expr1.Parameters);
        }
    }

Can anybody explain to me what this function is doing and how it works? I know it is used to add and and or conditions to the expression tree but how does it actually work? I have never used these classes like Expression and such. What is this specific code doing?

var invokedExpr = Expression.Invoke (expr2, expr1.Parameters.Cast<Expression> ());

                return Expression.Lambda<Func<T, bool>>
                     (Expression.OrElse (expr1.Body, invokedExpr), expr1.Parameters);

I know Func is a delegate which should return either true or false but what is this code in general doing ?

Thanks in advance :)

解决方案

This is using Expression Trees to "build" a predicate from two input expressions representing predicates.

Expression Trees are a way to use lambda's to generate a representation of code in a tree like structure (rather than a delegate directly). This takes two expression trees representing predicates (Expression<Func<T,bool>>), and combines them into a new expression tree representing the "or" case (and the "and" case in the second method).

Expression trees, and their corresponding utilities like above, are useful for things like ORMs. For example, Entity Framework uses expression trees with IQueryable<T> to turn "code" defined as a lambda into SQL that's run on the server.

这篇关于这个PredicateBuilder类如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆