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 7651973
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T11:38:42+00:00 2026-05-31T11:38:42+00:00

The following code is a helper I’ve created for a system which allows administrators

  • 0

The following code is a helper I’ve created for a system which allows administrators to create their own queries on a database. It returns a lambda expression based on the method and value provided.

I am attempting to find a way to pass parameters to the method call – in this example I am using the StartsWith parameter of String, and attempting to set StringComparison.OrdinalIgnoreCase as a parameter.

There will be others parameters required too, depending on the type of the property specified. I’m hoping that understanding the method of supplying the string comparison property will enable me to add the rest later.

The underlying code works correctly, I just need it to be case-insensitive.

I have used this question as a guide, but the solution does not seem applicable here.

Here is the code:

public static class LambdaExpressionHelper<T> {
    public static Expression<Func<T, bool>> Build(string propertyName, string method, string propertyValue) {
        PropertyInfo propertyInfo = typeof(T).GetProperty(propertyName);

        ParameterExpression e = Expression.Parameter(typeof(T), "e");
        MemberExpression m = Expression.MakeMemberAccess(e, propertyInfo);
        ConstantExpression c = Expression.Constant(propertyValue, m.Type);
        MethodInfo mi = m.Type.GetMethod(method, new Type[] { m.Type }, );

        // The below caused errors
        //object classInstance = Activator.CreateInstance(typeof(T), null);
        //object[] paramArray = new object[] { StringComparison.OrdinalIgnoreCase };
        //mi.Invoke(classInstance, paramArray);         

        Expression call = Expression.Call(m, mi, c);

        Expression<Func<T, bool>> lambda = Expression.Lambda<Func<T, bool>>(call, e);
        return lambda;
    }
}

// External code:
var lambda = LambdaExpressionHelper<MailingListMember>.Build("EmailAddress", "StartsWith", "RoRy@");

Thanks for any help.

  • 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-31T11:38:43+00:00Added an answer on May 31, 2026 at 11:38 am

    In the general case this should work:

    public static Expression<Func<T, bool>>
    Build(string propertyName, string method, params object[] args) 
    {
        var propertyInfo = typeof(T).GetProperty(propertyName);
        var e = Expression.Parameter(typeof(T), "e");
        var m = Expression.MakeMemberAccess(e, propertyInfo);
    
        var mi = m.Type.GetMethod(method, args.Select(a => a.GetType()).ToArray());
        var c = args.Select(a => Expression.Constant(a, a.GetType())).ToArray();
    
        Expression call = Expression.Call(m, mi, c);
    
        Expression<Func<T, bool>> lambda = Expression.Lambda<Func<T, bool>>(call, e);
        return lambda;
    }
    

    The idea is that you accept any number of constant arguments, and use their types to select the appropriate overload when calling GetMethod. After that, you create the appropriate number of constant expressions from those arguments to pass to Expression.Call.

    So with the above code, you could do:

    var l1 = LambdaExpressionHelper<MailingListMember>.Build(
      "EmailAddress", "StartsWith", "RoRy@");
    var l2 = LambdaExpressionHelper<MailingListMember>.Build(
      "EmailAddress", "StartsWith", "RoRy@", StringComparison.OrdinalIgnoreCase);
    

    If you also need to get the value StringComparison.OrdinalIgnoreCase from the string "StringComparison.OrdinalIgnoreCase", I would factor this out into a separate method so that the interface of Build can remain generic. Exactly how to do it is covered in Getting Enum value via reflection (and I guess in lots of similar questions as well).

    Note: I don’t have convenient access to a compiler right now, so please excuse any mistakes.

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

Sidebar

Related Questions

I have a page X.cshtml with the following code: @helper CodeTest{ <h1>Test</h1> } And
I have the following code to generate some spans using cakephp's helper <div id=numberRow>
I had a doubt which is the following: When I use the following code:
I have the following code in my application helper. route = ActionController::Routing::Routes.recognize_path(current_uri) controller =
I'm trying to use the button_to rails helper. I wrote the following code: <%=
I call an action helper in one of my views using the following code
I am creating Recaptcha in MVC 2.0. I used following code to create Captcha..
I have the following code inside a helper in rails: def custom_options_tag(opts={}) ['am', 'pm'].each
I have the following code in a Rails 3.2.1 helper that keeps escaping HTML
Let's say that I have the following code in application_helper.rb : def do_something if

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.