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

  • Home
  • SEARCH
  • 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 6331741
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T18:07:41+00:00 2026-05-24T18:07:41+00:00

How should I construct Expression tree for string.IndexOf(substring, StringComparison.OrdinalIgnoreCase) ? I can get it

  • 0

How should I construct Expression tree for string.IndexOf("substring", StringComparison.OrdinalIgnoreCase)?

I can get it working without the second argument: StringComparison.OrdinalIgnoreCase. These are my attempts so far:

var methodCall = typeof (string).GetMethod("IndexOf", new[] {typeof (string)});
Expression[] parms = new Expression[]{right, Expression.Constant("StringComparison.OrdinalIgnoreCase", typeof (Enum))};
var exp =  Expression.Call(left, methodCall, parms);
return exp;

Also tried this:

var methodCall = typeof (string).GetMethod(method, new[] {typeof (string)});
Expression[] parms = new Expression[]{right, Expression.Parameter(typeof(Enum) , "StringComparison.OrdinalIgnoreCase")};
var exp =  Expression.Call(left, methodCall, parms);
return exp;

Please remember that I can get it working if I ignore the OrdinalIgnoreCase parameter.

Thanks

  • 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-24T18:07:43+00:00Added an answer on May 24, 2026 at 6:07 pm

    I suspect there are two problems.

    The first is the way you’re getting the method – you’re asking for a method with only a single string parameter, instead of one with two parameters:

    var methodCall = typeof (string).GetMethod("IndexOf",
                                new[] { typeof (string), typeof(StringComparison) });
    

    The second is the value you’re giving – it should be the actual value of the constant, not a string:

    Expression[] parms = new Expression[] { right, 
        Expression.Constant(StringComparison.OrdinalIgnoreCase) };
    

    EDIT: Here’s a complete working sample:

    using System;
    using System.Linq.Expressions;
    
    class Test
    {
        static void Main()
        {
            var method = typeof (string).GetMethod("IndexOf",
                    new[] { typeof (string), typeof(StringComparison) });
    
            var left = Expression.Parameter(typeof(string), "left");
            var right = Expression.Parameter(typeof(string), "right");
    
            Expression[] parms = new Expression[] { right, 
                    Expression.Constant(StringComparison.OrdinalIgnoreCase) };
    
            var call = Expression.Call(left, method, parms);
            var lambda = Expression.Lambda<Func<string, string, int>>
                (call, left, right);
    
            var compiled = lambda.Compile();
            Console.WriteLine(compiled.Invoke("hello THERE", "lo t"));
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to construct an expression tree to operate against a string array.
How could I construct a LINQ expression to remove values from one list that
Given a string of identifiers separated by : , is it possible to construct
Following on from this question I asked yesterday: Can I shorten this regular expression?
I need to create a regular expression that allows a string to contain any
I am trying to define a replacement rule with optional argument color_RGBColor which should
I.e., I get a list of words and I want to construct a simple
How would I construct a regular expression to find all words that end in
should i use Classname() or __construct() as constructor in CodeIgniter? both work, which should
I've seen recommendations (Juval Lowy, et al) that a service contract should have no

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.