Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 496279
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T05:40:16+00:00 2026-05-13T05:40:16+00:00

I am trying to construct an expression tree to operate against a string array.

  • 0

I am trying to construct an expression tree to operate against a string array. I need to figure out how to use the Equal method.

Could anyone give me an example of
1) using the Expression.Equal (or .Equals) method to compare a string to a constant, and
2) using an Expression of whatever sort to use the string.Contains() method for filtering purposes.

I am trying to learn the Expression Tree mechanics, but as of yet, I have not found a good tutorial. I greatly appreciate your help.

string[] arr = {"s1","s2","s3"};
IQueryable<String> queryableData = arr.AsQueryable<string>();

// what should go below here?
ParameterExpression p1 = Expression.Parameter(typeof(string), "c");
Expression left = Expression.Constant("s2");
Expression e1 = Expression.Equal(left, p1);

IQueryable<string> res = queryableData.Provider.CreateQuery<string>(e2);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T05:40:16+00:00Added an answer on May 13, 2026 at 5:40 am

    Aaah, I see what you are asking… And you’re really entering some very murky waters, one of the few areas that the .NET reflection libraries aren’t beautiful to work with. You have to create a call expression to call Queryable.Where() on the queryableData object, and create a new query using that expression… The problem is that getting a generic version of a method in .NET isn’t necessarily the easiest thing you’ve ever run across in your life:

    MethodCallExpression call = Expression.Call(
        null, // Calling Queryable.Where(), extension method, not instance method
        getGenericMethod<string>(typeof(Queryable), "Where", typeof(IQueryable<string>), typeof(Expression<Func<string,bool>>)),
        Expression.Constant(queryableData),
        Expression.Lamda(
           e1,
           p1)
    );
    IQueryable<string> res = queryableData.Provider.CreateQuery<string>(call);
    

    You would also have to define getGenericMethod (you can find better implementations for this online at other places, this is really quite a simple approach):

    private static MethodInfo getGenericMethod<T>(Type type, string name, params Type[] paramTypes)
    {
        MethodInfo[] methods = type.GetMethods(name);
        foreach(MethodInfo mi in methods)
        {
            if(!mi.IsGenericMethodDefinition) // or some similar property
                continue;
            if(mi.GetGenericArguments().Length != 1)
                continue;
            if(mi.GetParameters().Length != paramTypes.Length)
                continue;
    
    
            MethodInfo genMethod = mi.MakeGenericMethod(new Type[]{typeof(T)});
            var ps = genMethod.GetParameters();
            bool isGood = true;
            for(int i = 0; i < ps.Length; i++)
            {
                if(ps[i].ParameterType != paramTypes[i])
                {
                   isGood = false;
                   break;
                }
            }
    
            if(isGood)
                return genMethod;
    
        }
    
        return null;
    }
    

    There are almost undoubtedly a few errors in there, but I hope you can see where to go from there…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to evaluate a test string against a regular expression I build from
I'm trying to construct a regular expression that would extract the name of the
I am trying to construct a deeply nested associative array but i don't know
I'm trying to construct a datatype that is essentially a binary tree whose: each
Im trying to construct a regular expression to just match if the length of
I'm trying to construct my linq-to-sql expression so that it only generates a single
I am trying to construct a regular expression that will match the following URL:
I am trying to construct a LINQ query, with expression trees, to do the
I have a regex expression that I'm trying to construct that processes a file
I'm trying to generate a truth table for a given boolean expression. I could

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.