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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T11:05:24+00:00 2026-05-12T11:05:24+00:00

Let’s say we have a collection of Person objects class Person { public string

  • 0

Let’s say we have a collection of Person objects

class Person 
{
     public string PersonName {get;set;}
     public string PersonAddress {get;set;}    
}

And somewhere in the code defined collection

List<Person> pesonsList = new List<Person>();

We need to have a filter that need to filter the collection and return the result to the end user. Let’s say we have a collection of Filter type objects

class Filter 
{
    public string FieldName {get;set;}
    public string FilterString {get;set;}
}

And somewhere in the code we have

List<Filter> userFilters = new List<Filter>(); 

So we need to filter the content of the personsList collection by filters defined in the userFilters collection. Where the Filter.FieldName == “PersonName” || Filter.FieldName == “PersonAddress”. How can I do that with LINQ in a cool way ? The solutions like switch/case, or
may be, I thought, extension method on personsList that determines from the FiledName the property of the Person to look into, are known. Something else ? Something tricky:)
Thank you.

  • 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-12T11:05:24+00:00Added an answer on May 12, 2026 at 11:05 am

    You can build a lambda expression to create a proper predicate using the Expression class.

    public static Expression<Func<TInput, bool>> CreateFilterExpression<TInput>(
                                                       IEnumerable<Filter> filters)
    {
        ParameterExpression param = Expression.Parameter(typeof(TInput), "");
        Expression lambdaBody = null;
        if (filters != null)
        {
            foreach (Filter filter in filters)
            {
                Expression compareExpression = Expression.Equal(
                        Expression.Property(param, filter.FieldName),
                        Expression.Constant(filter.FilterString));
                if (lambdaBody == null)
                    lambdaBody = compareExpression;
                else
                    lambdaBody = Expression.Or(lambdaBody, compareExpression);
            }
        }
        if (lambdaBody == null)
            return Expression.Lambda<Func<TInput, bool>>(Expression.Constant(false));
        else
            return Expression.Lambda<Func<TInput, bool>>(lambdaBody, param);
    }
    

    With this helper method, you can create an extension method on any IQueryable<T> class, so this should work for every LINQ backend:

    public static IQueryable<T> Where<T>(this IQueryable<T> source, 
                                              IEnumerable<Filter> filters)
    {
        return Queryable.Where(source, CreateFilterExpression<T>(filters));
    }
    

    …which you can call like this:

    var query = context.Persons.Where(userFilters);
    

    If you want to support IEnumerable<T> collections as well, you’ll need to use this extra extension method:

    public static IEnumerable<T> Where<T>(this IEnumerable<T> source, 
                                               IEnumerable<Filter> filters)
    {
        return Enumerable.Where(source, CreateFilterExpression<T>(filters).Compile());
    }
    

    Note that this only works for string properties. If you want to filter on fields, you’ll need to change Expression.Property into Expression.Field (or MakeMemberAccess), and if you need to support other types than string properties, you’ll have to provide more type information to the Expression.Constant part of the CreateFilterExpression method.

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

Sidebar

Related Questions

Let's say you have a class called Customer, which contains the following fields: UserName
Let's say I'm building a data access layer for an application. Typically I have
Let's say we have a simple function defined in a pseudo language. List<Numbers> SortNumbers(List<Numbers>
Let's say I have a drive such as C:\ , and I want to
Let's say I'm writing a PHP (>= 5.0) class that's meant to be a
Let's say that we have an ARGB color: Color argb = Color.FromARGB(127, 69, 12,
Let's say you create a wizard in an HTML form. One button goes back,
Let me try to explain what I need. I have a server that is
I have a French site that I want to parse, but am running into
Let's aggregate a list of free quality web site design templates. There are a

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.