Concatenate two Func delegates

核能气质少年 提交于 2019-12-03 15:14:58

问题


Assume that I have thes Class:

public class Order
{
   int OrderId {get; set;}
   string CustomerName {get; set;}
}

I declare below variables, too

Func<Order, bool> predicate1 = t=>t.OrderId == 5 ;
Func<Order, bool> predicate2 = t=>t.CustomerName == "Ali";

Is there any way that concatenate these variables(with AND/OR) and put the result in 3rd variable? for example:

Func<Order, bool> predicate3 = predicate1 and predicate2;

or

Func<Order, bool> predicate3 = predicate1 or predicate2;

回答1:


And:

Func<Order, bool> predicate3 = 
    order => predicate1(order) && predicate2(order);

Or:

Func<Order, bool> predicate3 = 
    order => predicate1(order) || predicate2(order);


来源:https://stackoverflow.com/questions/17088268/concatenate-two-func-delegates

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!