我想做这样的事情:
List<SomeClass> list1 = ...
List<SomeClass> list2 = ...
Predicate<SomeClass> condition = ...
...
list2.RemoveAll (!condition);
...
list2.AddRange (list1.FindAll (condition));
Run Code Online (Sandbox Code Playgroud)
但是,这会导致编译器错误,因为!无法应用Predicate<SomeClass>.有没有办法做到这一点?
我想创建一个方法来接受Expression<Func<T, bool>>并创建它的逻辑逆(即它将返回它将返回的false位置true,反之亦然.这比我想象的要困难得多.这是我所要做的:
public static Expression<Func<T, bool>> Not<T>(this Expression<Func<T, bool>> expression)
{
return Expression.Lambda<Func<T, bool>>(Expression.Not(expression.Body));
}
Run Code Online (Sandbox Code Playgroud)
这编译很好但在调用时会抛出以下异常:
Test method Tests.Common.Unit.LinqPredicateBuilderTests.CanInverseAPredicate threw exception:
System.ArgumentException: Incorrect number of parameters supplied for lambda declaration
Run Code Online (Sandbox Code Playgroud)
我不知道我在做什么.任何人都可以填补空白吗?