我有两个类型的表达式,Expression<Func<T, bool>>我想采取OR,AND或NOT这些并得到一个相同类型的新表达式
Expression<Func<T, bool>> expr1;
Expression<Func<T, bool>> expr2;
...
//how to do this (the code below will obviously not work)
Expression<Func<T, bool>> andExpression = expr AND expr2
Run Code Online (Sandbox Code Playgroud) 你如何转换Expression<Func<IBar, T>>为Expression<Func<Bar, T>>Bar实施IBar?
这个更通用的问题有答案:
将表达式<Func <T1,bool >>动态转换为Expression <Func <T2,bool>
这是这种情况下最好的方法吗?鉴于Bar实施IBar,有没有更简单的方法?
所以,鉴于这个人为的示例代码:
public class Foo<T>
{
private readonly List<T> _list = new List<T>();
public void Add(T item)
{
_list.Add(item);
}
public bool AnyFunc(Func<T, bool> predicate)
{
return _list.Any(predicate);
}
public bool AnyExpression(Expression<Func<T, bool>> expression)
{
return _list.AsQueryable().Any(expression);
}
}
public interface IBar
{
string Name { get; }
}
public class Bar : IBar
{
public string Name { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
这证明了这个问题:
public class Test() …Run Code Online (Sandbox Code Playgroud)