Lambda expression Does NotContains statement exist?

Lambda expression for the Contains statement, which I can generate using this code.

Expression

 Company => Company.Name.Contains("test1") 

Source

 var method = typeof(string).GetMethod("Contains", new[] { typeof(string) }); var startsWithDishExpr = Expression.Call(argLeft, method, argRight); 

Works great for the Contains statement. How to change the code to work for the NotContains operator.

Source

 var method = typeof(string).GetMethod("NotContains", new[] { typeof(string) }); var startsWithDishExpr = Expression.Call(argLeft, method, argRight); 
Operator

NotContains does not work. Anyone have a suggestion?

+7
source share
1 answer

There is no string.NotContains method, so making a method call called NotContains does not work.

A simple solution is to combine the not operator with the Contains method. As usual, you write !x.Contains(y) , not x.NotContains(y) .

To create such an expression, you can use Expression.Not(callExpression) .

+16
source

All Articles