1

I'm generating a c# Linq expression dynamically as below, which will (in the example below) run string.Contains against the collection values.

var dynamicMethod = "Contains";
var parameter = Expression.Parameter(typeof (MyClass), "type");
var property = Expression.Property(parameter, "MyProperty");
var constantValue = Expression.Constant("PropertyValue", property.Type);
var method = property.Type.GetMethod(dynamicMethod, new[] {property.Type});
var expression = Expression.Call(property, method, constantValue);

For the above code, I'd want something equivalent to !Contains.

Any suggestions?

Thanks.

LJW
  • 2,378
  • 1
  • 21
  • 35

1 Answers1

9

What about Expression.Not? That would simply be:

var condition = Expression.Not (contains_call);
Jb Evain
  • 17,319
  • 2
  • 67
  • 67
  • Works great...I was originally trying Expression.Negate() but appears that Expression.Not() is what I was looking for. – afreeland Nov 13 '13 at 16:15