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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T22:10:51+00:00 2026-05-25T22:10:51+00:00

I want to create dynamic lambda expressions so that I can filter a list

  • 0

I want to create dynamic lambda expressions so that I can filter a list using a set of filtering parameters. This is what I have so far:

The expression is built using the methods bellow, where T is the object type of the list

    public static Expression<Func<T, bool>> GetExpression<T>(IList<DynamicFilter> filters)
    {
        if (filters.Count == 0)
            return null;

        ParameterExpression param = Expression.Parameter(typeof(T), "t");
        Expression exp = null;

        if (filters.Count == 1)
            exp = GetExpression<T>(param, filters[0]);

        [...]

        return Expression.Lambda<Func<T, bool>>(exp, param);
    }

    private static Expression GetExpression<T>(ParameterExpression param, DynamicFilter filter)
    {
        MemberExpression member = Expression.Property(param, filter.PropertyName);
        ConstantExpression constant = Expression.Constant(filter.Value);

        [...]

        return Expression.Call(member, filterMethod, constant);
    }

I then call

List<Example> list = ...;
var deleg = ExpressionBuilder.GetExpression<Example>(dynFiltersList).Compile();
list = list.Where(deleg).ToList();

This works just as expected with an object that contains only simple types, but if there are complex types inside, the code doesn’t work anymore. For example, let’s say I have a member of custom type Field inside the Example class and Field has a string property Value. If filter.PropertyName would be ‘Field0’ (of type Field), the code would work just fine, but if I have ‘Field0.Value’ I would get an obvious error stating that there is no property named ‘Field0.Value’ inside class Example.

I tried modifying the expression building method, like this:

        MemberExpression member = null;
        if (filter.PropertyName.Contains('.'))
        {
            string[] props = filter.PropertyName.Split('.');

            ParameterExpression param1 = Expression.Parameter(typeof(T).GetProperty(props[0]).PropertyType, "t1");
            member = Expression.Property(param1, props[0]);
        }
        else
        {
            member = Expression.Property(param, filter.PropertyName);
        }

but then I got a Lambda parameter not in scope error when compiling the expression. I sort of understand why I get this error, but I don’t know how to make this work.

Bottom line is I need to make the expression building method work recursively when forming the MemberExpression. I ultimately need to obtain a list = list.Where(deleg).ToList(); that translates to something like this list = list.Where(obj => obj.Field0.Value == 'something').ToList();

I’ve just started working with expressions so I don’t really know too much in this area, but any help would be appreciated.

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-25T22:10:52+00:00Added an answer on May 25, 2026 at 10:10 pm

    I realize this is a fairly old post, but I had the exact same problem and found something close in an answer that Mark Gravell posted here. I just modified it slightly to meet my needs and below is the result:

        private Expression GetDeepProperty(Expression parameter, string property)
        {
            var props = property.Split('.');
            var type = parameter.Type;
    
            var expr = parameter;
            foreach (var prop in props)
            {
                var pi = type.GetProperty(prop);
                expr = Expression.Property(expr, pi);
                type = pi.PropertyType;
            }
    
            return expr;
        }
    

    Implementation:

    var method = typeof (string).GetMethod("Contains", new Type[] {typeof (string)}, null);
    var lambdaParameter = Expression.Parameter(typeof(TEntity), "te");
    var filterExpression = Expression.Lambda<Func<TEntity, bool>> (
                filters.Select(filter => Expression.Call(GetDeepProperty(lambdaParameter, filter.Property),
                                                          method,
                                                          Expression.Constant(filter.Value))).
                    Where(exp => exp != null).
                    Cast<Expression>().
                    ToList().
                    Aggregate(Expression.Or), lambdaParameter);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an .NET 4.0 application using Caliburn.Micro. I want to create a dynamic
I want to create dynamic content based on this. I know it's somewhere, as
this table i want to create job_id dynamic Jobs Title text Job Description text
I have some data and want to create some dynamic charts. I have looked
i have create dynamic datepicker inside gridview. i want to know how to get
I'm working on creating a Hibernate query engine that can create dynamic queries. Thus
I have a list which contains dynamic data. i.e list[0].id=0; list[1].id=1; list[2].id=2; I want
I am trying to create dynamic subdomains in .htaccess. I want something like this:
I have been learning how to create dynamic images and dynamic stylesheets using ASP.NET
I have issue where i want to create Dynamic function which will do some

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.