using System;
using System.Linq.Expressions;
public class Test
{
public static Expression<Func<T, bool>> Or<T>(
Expression<Func<T, bool>> source, Expression<Func<T, bool>> expression
) {
if (source == null)
return expression;
var p = Expression.Parameter(typeof(T));
return (Expression<Func<T,bool>>)Expression.Lambda(
Expression.Or(
Expression.Invoke(source, p)
, Expression.Invoke(expression, p)
)
, p
);
}
public static void Main()
{
Expression<Func<int,bool>> a = x=>x > 5;
Expression<Func<int,bool>> b = x=>x < -5;
var or = Or(a, b);
var f = (Func<int,bool>)or.Compile();
for (int i = -10 ; i <= 10 ; i++) {
Console.WriteLine("{0} - {1}", i, f(i));
}
}
}
dXNpbmcgU3lzdGVtOwp1c2luZyBTeXN0ZW0uTGlucS5FeHByZXNzaW9uczsKCnB1YmxpYyBjbGFzcyBUZXN0CnsKCQoJCiAgICBwdWJsaWMgc3RhdGljIEV4cHJlc3Npb248RnVuYzxULCBib29sPj4gT3I8VD4oCiAgICAJRXhwcmVzc2lvbjxGdW5jPFQsIGJvb2w+PiBzb3VyY2UsIEV4cHJlc3Npb248RnVuYzxULCBib29sPj4gZXhwcmVzc2lvbgogICAgKSB7CiAgICAgICAgaWYgKHNvdXJjZSA9PSBudWxsKQogICAgICAgICAgICByZXR1cm4gZXhwcmVzc2lvbjsKICAgICAgICB2YXIgcCA9IEV4cHJlc3Npb24uUGFyYW1ldGVyKHR5cGVvZihUKSk7CiAgICAgICAgcmV0dXJuIChFeHByZXNzaW9uPEZ1bmM8VCxib29sPj4pRXhwcmVzc2lvbi5MYW1iZGEoCiAgICAgICAgCUV4cHJlc3Npb24uT3IoCiAgICAgICAgCQlFeHByZXNzaW9uLkludm9rZShzb3VyY2UsIHApCiAgICAgICAgCSwgICBFeHByZXNzaW9uLkludm9rZShleHByZXNzaW9uLCBwKQogICAgICAgIAkpCiAgICAgICAgLCAgIHAKICAgICAgICApOwogICAgfQkKCQoJcHVibGljIHN0YXRpYyB2b2lkIE1haW4oKQoJewoJCUV4cHJlc3Npb248RnVuYzxpbnQsYm9vbD4+IGEgPSB4PT54ID4gNTsKCQlFeHByZXNzaW9uPEZ1bmM8aW50LGJvb2w+PiBiID0geD0+eCA8IC01OwoJCXZhciBvciA9IE9yKGEsIGIpOwoJCXZhciBmID0gKEZ1bmM8aW50LGJvb2w+KW9yLkNvbXBpbGUoKTsKCQlmb3IgKGludCBpID0gLTEwIDsgaSA8PSAxMCA7IGkrKykgewoJCQlDb25zb2xlLldyaXRlTGluZSgiezB9IC0gezF9IiwgaSwgZihpKSk7CgkJfQoJfQp9