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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T22:58:18+00:00 2026-06-14T22:58:18+00:00

I am trying to create a reusable method using expressions that looks something like

  • 0

I am trying to create a reusable method using expressions that looks something like this:

Expression<Func<Order, bool>> CreateExpression(Expression<Func<Order, int>> parameter, FilterOperator operator, int value)

So I can use it like this:

IQueryable<Order> orders = db.Orders;

var filtered = orders.Where(CreateExpression(o => o.OrderID, FilterOperator.GreaterThan, 100));

I’m not sure how to write the method though. How can I write a method that will create this Expression for me?

I need to be able to do something like this:

if(operator == FilterOperator.GreaterThan)
   return m => m.OrderID > value;
else if(operator == FilterOperator.LessThan)
   return m => m.OrderID < value;

But I want to use the expression that is passed in instead of using OrderID directly. How can I do this?

  • 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-06-14T22:58:19+00:00Added an answer on June 14, 2026 at 10:58 pm
    static Expression<Func<T, bool>> CreateExpression<T>(Expression<Func<T, int>> parameter, FilterOperator @operator, int value)
    {
        var argExpr = Expression.Parameter(typeof(T), "p");
        var paramExpr = Expression.Invoke(parameter, argExpr);
        var constExpr = Expression.Constant(value);
        Expression compExpr = null;
        switch(@operator)
        {
            case FilterOperator.GreaterThan:
                compExpr = Expression.GreaterThan(paramExpr, constExpr);
                break;
            case FilterOperator.LessThan:
                compExpr = Expression.LessThan(paramExpr, constExpr);
                break;
        }
    
        return Expression.Lambda<Func<T, bool>>(compExpr, argExpr);
    }
    

    If you can’t use Invoke and your parameter expression is a member expression, then you can just re-create it using your new parameter:

    static Expression<Func<T, bool>> CreateExpression<T>(Expression<Func<T, int>> parameter, FilterOperator @operator, int value)
    {
        var memberExpr = (MemberExpression)parameter.Body;
        PropertyInfo property = (PropertyInfo)memberExpr.Member;
    
        var argExpr = Expression.Parameter(typeof(T), "p");
        var propertyExpr = Expression.Property(argExpr, property);
        var constExpr = Expression.Constant(value);
    
        Expression compExpr = null;
        switch(@operator)
        {
            case FilterOperator.GreaterThan:
                compExpr = Expression.GreaterThan(propertyExpr, constExpr);
                break;
            case FilterOperator.LessThan:
                compExpr = Expression.LessThan(propertyExpr, constExpr);
                break;
        }
    
        return Expression.Lambda<Func<T, bool>>(compExpr, argExpr);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create a generic, reusable view, that looks like a lined notepad.
I'm trying to create a view that only houses reusable HTML blocks that can
I'm trying to do something like this... <Style x:Key="MyBorderStyle" TargetType="Border"> <Setter Property="Padding" Value="{TemplateBinding Padding}"
I am trying to create a reusable function that can open a single instance
I'm trying to create a reusable UserControl in WPF that has a Label and
I am trying to create a reusable function that checks if a variable is
Background / Problem I'm trying to create a reusable JSF composite component that can
I'm trying to create a reusable component to display some photo collection. The basic
I am trying to create some reusable components in Silverlight2 . The difficulty comes
I am trying to create a Validation in a reusable fashion. Purpose: Make the

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.