如何仅在添加范围中将T,布尔函数的表达式添加到列表中?

时间:2018-11-09 09:50:59

标签: c#

我有列表new List<Expression<Func<Test, bool>>>

并且我想添加项到.Add()范围内的所有操作

我该如何实现?

var a = new List<Func<Test, bool>>();

var b = new List<Expression<Func<Test, bool>>>();

a.Add
(
    new Func<Test, bool>(x => x.test == false) // works
);

b.Add
(
    new Expression<Func<Test, bool>>(x => x.test == false) // fails
);

1 个答案:

答案 0 :(得分:3)

只需省略类型并让编译器进行推断:

var a = new List<Func<Test, bool>>();

var b = new List<Expression<Func<Test, bool>>>();

a.Add
(
    x => x.test == false
);

b.Add
(
    x => x.test == false 
);