Find the Any method on an array of strings

c# expression-trees linq

Question

Given

string[] stringArray = { "test1", "test2", "test3" };

If this returns true, then:

bool doesContain = stringArray.Any(s => "testa test2 testc".Contains(s));

My final objective is to turn this into a linq expression tree. How can I find out the technique information for"Any" ? The next is ineffective since it returns nil.

MethodInfo info = typeof(string[]).GetMethod("Any", BindingFlags.Static | BindingFlags.Public);

Additional clarification

I'm developing search capabilities. I use EF, and so far, creating a dynamic lambda expression tree by utilizing linq expression trees has worked. In this instance, I have a string array from which I can choose any text to appear in a description field. The operational lambda expression used in theWhere is a clause:

c => stringArray.Any(s => c.Description.Contains(s));

I thus require a call to in order to create the lambda expression's body."Any" .

Last code:

As a result of I4V's response, constructing this portion of the expression tree now appears as follows (and functions):

//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);}

after thatdescriptionExpression enters a bigger lambda expression tree.

1
4
5/7/2013 6:02:01 PM

Accepted Answer

maybe along these lines?

var mi = typeof(Enumerable)
            .GetMethods()
            .Where(m => m.Name == "Any")
            .First(m => m.GetParameters().Count() == 2)
            .MakeGenericMethod(typeof(string));

You may also refer to it as:

var result = mi.Invoke(null, new object[] { new string[] { "a", "b" }, 
                                           (Func<string, bool>)(x => x == "a") });
1
5/7/2013 4:01:55 PM

Popular Answer

You may also

// 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"))

Additionally, the IQueryable overload may be useful if you're using Linq to Entities:

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"));


Related Questions





Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow