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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T01:36:54+00:00 2026-05-13T01:36:54+00:00

Thanks to those who answered my last question I got the code below to

  • 0

Thanks to those who answered my last question I got the code below to work which allows the developer to send multiple where clauses to a method which includes each of them in a LINQ statement. However, how can I get the inclusion of the where clauses to be dynamic? Instead of this:

return customers
       .Where(whereClauses[0])
       .Where(whereClauses[1])
       .ToList();

something like this (pseudo-code):

List<Customer> customers = new List<Customer>();
foreach (var whereClause in whereClauses)
{
    customers
    .Where(whereClause...???)
    .ToList();
}
return customers;

Here is the code that works:

using System;
using System.Collections.Generic;
using System.Linq;

namespace TestDynamicLinq2343
{
    public class Program
    {
        static void Main(string[] args)
        {
            List<Customer> customers = Customer.GetCustomers();

            List<Func<Customer, bool>> whereClauses = new List<Func<Customer, bool>>();
            whereClauses.Add(c => c.LastName.ToUpper().Contains("A"));
            whereClauses.Add(c => c.FirstName.ToUpper().Contains("J"));

            foreach (var customer in Customer.GetFilteredCustomers(customers, whereClauses))
            {
                Console.WriteLine(customer.LastName);
            }

            Console.ReadLine();
        }
    }

    public class Customer
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Street { get; set; }
        public string Location { get; set; }
        public string ZipCode { get; set; }

        public static List<Customer> GetCustomers()
        {
            List<Customer> customers = new List<Customer>();
            customers.Add(new Customer { FirstName = "Jim", LastName = "Jones" });
            customers.Add(new Customer { FirstName = "Joe", LastName = "Adams" });
            customers.Add(new Customer { FirstName = "Jake", LastName = "Johnson" });
            customers.Add(new Customer { FirstName = "Angie", LastName = "Reckar" });
            customers.Add(new Customer { FirstName = "Jean", LastName = "Anderson" });
            return customers;
        }

        public static List<Customer> GetFilteredCustomers(List<Customer> customers, List<Func<Customer, bool>> whereClauses)
        {
            return customers
                   .Where(whereClauses[0])
                   .Where(whereClauses[1])
                   .ToList();
        }
    }
}
  • 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-13T01:36:54+00:00Added an answer on May 13, 2026 at 1:36 am
    IEnumerable<Customer> dbCustomers = customers;
    foreach (var whereClause in whereClauses)
    {
        dbCustomers = dbCustomers.Where(whereClause);
    }
    return dbCustomers.ToList();
    

    maybe interesting extensionMethod:

    public static class IEnumerableExtension
    {
        public static void AttachWhereClauses<T>(this IEnumerable<T> source, IEnumerable<Func<T, bool>> whereClauses)
        {
            foreach (var whereClause in whereClauses)
            {
                source = source.Where(whereClause);
            }
        }
    }
    var listedCustomers = customers.AttachWhereClauses(whereClauses).ToList();
    

    but: not tested – i do not know for sure, if attaching the whereClauses to the same object works!

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

Sidebar

Related Questions

Thanks to those who answered my last couple questions, I got the following code
Thanks to those who've answered my previous questions and gotten me this far. I
Firstly, I'd like to thank those who answered my previous question ages ago. Currently
EDIT: I've been convinced that this question is somewhat non-sensical. Thanks to those who
EDIT: Thanks Kenny and everyone else who answered similarly. It really was a DOH
I'm optimizing my website according to Google's site optimization standards: http://code.google.com/speed/page-sp...mageDimensions For those who
Advance thanks for those who will reply bcoz i google it lot but no
I have a pool of 100 users who have answered a question. Only about
Thanks to the epic work of Dennis Williamson, I am now able to calculate
Thanks in advance to anyone who can think of a more efficient or 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.