Lambda expressions and how to combine them?

前端 未结 4 1334
执念已碎
执念已碎 2020-12-31 05:19

How can I combine two lambda expressions into one using an OR ?

I have tried the following but merging them requires me to pass parameters into the Expressio

4条回答
  •  时光说笑
    2020-12-31 05:59

    ** edit: oops read over the OR thing, updated it ***

    Hi,

    Not sure if you just want to call them seperatley or you want to combine them from an academic point of view.

    You can just call them like this:

    bool OR(int i1, Func f1, Func f2){
        return f1(i1) || f2(i1);
    }
    

    That will do the trick.

    or you can rewrite that as

    bool MyOR = (i1, f1, f2) => f1(i1) || f2(i1);
    

    And when you're qurious, create an expression from that and look at that. (doing this by hand, don;t have VS here now, so please be easy on my typos)

    Expression, Func, bool>> exprOR = 
    (i1, f1, f2) => f1(i1) || f2(i1); 
    

    If you want to look at the anatomy of the expression, you can look at this article i wrote: http://www.codeproject.com/KB/WPF/WpfExpressionTree.aspx

    Just pass the expression to the apadater and see how it;s built up.

    Regards Gert-Jan

提交回复
热议问题