开发者

How does this PredicateBuilder class work?

开发者 https://www.devze.com 2023-02-16 14:49 出处:网络
I have been reading Joseph Albahari\'s brilliant book on C# 4.0 and I came across this class: public static class PredicateBuilder

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.开发者_如何学PythonCast<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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号