Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lambda expression NotContains operator Exists?

Tags:

c#

lambda

Lambda expression for Contains operator I am able to generate using this code.

Expression

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

Source code

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

Its working fine for Contains operator. How to modify to code to work for NotContains operator.

Source code

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

NotContains operator not working. Anybody have suggestion?

like image 893
sivaL Avatar asked Sep 17 '12 10:09

sivaL


1 Answers

There is no string.NotContains method, so creating a call to a method called NotContains doesn't work.

A simple solution is to combine the not operator, with the Contains method. Just like normally you'd write !x.Contains(y) and not x.NotContains(y).

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

like image 170
CodesInChaos Avatar answered Sep 23 '22 01:09

CodesInChaos