Given
string[] stringArray = { "test1", "test2", "test3" };
then this returns true:
bool doesContain = stringArray.Any(s => "testa test2 testc".Contains(s));
My ultimate goal is to make a linq expression tree out of this. The question is how do I get the method info of "Any"
?
The following doesn't work because it returns null.
MethodInfo info = typeof(string[]).GetMethod("Any", BindingFlags.Static | BindingFlags.Public);
Further explanation:
I am creating search functionality. I use EF and so far using linq expression trees works to create a dynamic lambda expression tree. In this case I have an array of strings from which any string should occur in a description field. The working lambda expression that goes into the Where
clause is:
c => stringArray.Any(s => c.Description.Contains(s));
So, to make the body of the lambda expression I need a call to "Any"
.
Final code:
Thanks to I4V's answer, creating this part of the expression tree now looks like this (and works):
//stringArray.Any(s => c.Description.Contains(s));
if (!String.IsNullOrEmpty(example.Description))
{
string[] stringArray = example.Description.Split(' '); //split on spaces
ParameterExpression stringExpression = Expression.Parameter(typeof(string), "s");
Expression[] argumentArray = new Expression[] { stringExpression };
Expression containsExpression = Expression.Call(
Expression.Property(parameterExpression, "Description"),
typeof(string).GetMethod("Contains"),
argumentArray);
Expression lambda = Expression.Lambda(containsExpression, stringExpression);
Expression descriptionExpression = Expression.Call(
null,
typeof(Enumerable)
.GetMethods()
.Where(m => m.Name == "Any")
.First(m => m.GetParameters().Count() == 2)
.MakeGenericMethod(typeof(string)),
Expression.Constant(stringArray),
lambda);}
And then descriptionExpression
goes into a larger lambda expression tree.
Maybe something like this?
var mi = typeof(Enumerable)
.GetMethods()
.Where(m => m.Name == "Any")
.First(m => m.GetParameters().Count() == 2)
.MakeGenericMethod(typeof(string));
And you can invoke it as:
var result = mi.Invoke(null, new object[] { new string[] { "a", "b" },
(Func<string, bool>)(x => x == "a") });
You can also do
// You cannot assign method group to an implicitly-typed local variable,
// but since you know you want to operate on strings, you can fill that in here:
Func<IEnumerable<string>, Func<string,bool>, bool> mi = Enumerable.Any;
mi.Invoke(new string[] { "a", "b" }, (Func<string,bool>)(x=>x=="a"))
And if you're working with Linq to Entities, you might want the IQueryable overload:
Func<IQueryable<string>, Expression<Func<string,bool>>, bool> mi = Queryable.Any;
mi.Invoke(new string[] { "a", "b" }.AsQueryable(), (Expression<Func<string,bool>>)(x=>x=="b"));