How to create an expression tree to represent "String.Contains (" term ") in C #?

I am just starting with expression trees, so I hope this makes sense. I am trying to create an expression tree for a view:

t => t.SomeProperty.Contains("stringValue"); 

So far I have received:

  private static Expression.Lambda<Func<string, bool>> GetContainsExpression<T>(string propertyName, string propertyValue) { var parameterExp = Expression.Parameter(typeof(T), "type"); var propertyExp = Expression.Property(parameter, propertyName); var containsMethodExp = Expression.*SomeMemberReferenceFunction*("Contains", propertyExp) //this is where I got lost, obviously :) ... return Expression.Lambda<Func<string, bool>>(containsMethodExp, parameterExp); //then something like this } 

I just don't know how to reference the String.Contains () method.

Help evaluate.

+45
c # lambda expression-trees
Nov 10 '08 at 18:11
source share
4 answers

Something like:

 class Foo { public string Bar { get; set; } } static void Main() { var lambda = GetExpression<Foo>("Bar", "abc"); Foo foo = new Foo { Bar = "aabca" }; bool test = lambda.Compile()(foo); } static Expression<Func<T, bool>> GetExpression<T>(string propertyName, string propertyValue) { var parameterExp = Expression.Parameter(typeof(T), "type"); var propertyExp = Expression.Property(parameterExp, propertyName); MethodInfo method = typeof(string).GetMethod("Contains", new[] { typeof(string) }); var someValue = Expression.Constant(propertyValue, typeof(string)); var containsMethodExp = Expression.Call(propertyExp, method, someValue); return Expression.Lambda<Func<T, bool>>(containsMethodExp, parameterExp); } 

You may find this helpful.

+106
Nov 10 '08 at 18:20
source share

How about this:

 Expression<Func<string, string, bool>> expFunc = (name, value) => name.Contains(value); 

In client code:

  bool result = expFunc.Compile()("FooBar", "Foo"); //result true result = expFunc.Compile()("FooBar", "Boo"); //result false 
+7
Jul 15 '09 at 6:34
source share

To search, for example:

 ef.Entities.Where(entity => arr.Contains(entity.Name)).ToArray(); 

which will contain the trace string:

 SELECT .... From Entities ... Where Name In ("abc", "def", "qaz") 

I am using the method that I created below:

 ef.Entities.Where(ContainsPredicate<Entity, string>(arr, "Name")).ToArray(); public Expression<Func<TEntity, bool>> ContainsPredicate<TEntity, T>(T[] arr, string fieldname) where TEntity : class { ParameterExpression entity = Expression.Parameter(typeof(TEntity), "entity"); MemberExpression member = Expression.Property(entity, fieldname); var containsMethods = typeof(Enumerable).GetMethods(BindingFlags.Static | BindingFlags.Public) .Where(m => m.Name == "Contains"); MethodInfo method = null; foreach (var m in containsMethods) { if (m.GetParameters().Count() == 2) { method = m; break; } } method = method.MakeGenericMethod(member.Type); var exprContains = Expression.Call(method, new Expression[] { Expression.Constant(arr), member }); return Expression.Lambda<Func<TEntity, bool>>(exprContains, entity); } 
+5
Mar 13 '12 at 4:12
source share

Here is an example of criteria. States (MyParam)

 var method = typeof(Enumerable).GetRuntimeMethods().Single(m => m.Name == nameof(Enumerable.Contains) && m.GetParameters().Length == 2); var containsMethod = method.MakeGenericMethod(typeof(string)); var doesContain = Expression.Call(containsMethod, Expression.Constant(criteria.ToArray()), Expression.Property(p, "MyParam")); 

Actual use at https://raw.githubusercontent.com/xavierjohn/Its.Cqrs/e44797ef6f47424a1b145d69889bf940b5581eb8/Domain.Sql/CatchupEventFilter.cs

0
Dec 07 '17 at 6:29
source share



All Articles