-
Notifications
You must be signed in to change notification settings - Fork 166
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Missing Not() method in Predicate Builder #31
Comments
Why do you want to make your own not-operator? I would need a little more code to not-be-a-pseudo, but I think you could do:
|
It was just for adding more fluent code to the current api provided by LinqKit. ;-). For clarification, we implemented an Specification pattern whereby the Specifications exists of Expressions. With the .Not() extension method this pattern became better to read. |
I think that all the System.Linq.Expressions, at least unary-expressions (ArrayLength, Convert, ConvertChecked, Negate, NegateChecked, Not, Quote, TypeAs, UnaryPlus) could be extension methods, however this shouldn't be LinqKit's improvement but .NET Framework Class Library. You can make a PR if you feel that these would be very helpful, but LinqKit is not the only tool using expression-traversal. Or should we close this issue for now? |
Hi, |
Based on the above I could not figure out how todo this, so for anyone else looking for a solution here is what we found to be working after quite a while of trial and error. Our use case was to negate an entire predicate chain that contained many ands / ors. negating the individual chain elements would have made the query unreadable / unmaintainable. We would find it helpful if ExpressionStarter would provide a negation option for an entire predicate chain out of the box. var predicate = PredicateBuilder.New<TEntity>(your start condition);
Expression<Func<TEntity, bool>> expression = predicate; // its important to use implicit conversion here
if (negate)
{
expression = expression.Not();
}
query.Where(expression);
// register extension method
public class ExtensionMethods {
public static Expression<Func<T, bool>> Not<T>(this Expression<Func<T, bool>> one)
{
var candidateExpr = one.Parameters[0];
var body = Expression.Not(one.Body);
return Expression.Lambda<Func<T, bool>>(body, candidateExpr);
}
} |
Great project, but missing the Not() option in the Predicate Builder:
public static Expression<Func<T, bool>> Not<T>(this Expression<Func<T, bool>> expression) { return Expression.Lambda<Func<T, bool>>(Expression.Not(expression.Body), expression.Parameters); }
This can be used for negating the expression for example:
inner = inner.Or (p => p.Description.Contains ("foo")).Not();
The text was updated successfully, but these errors were encountered: