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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T13:34:47+00:00 2026-06-13T13:34:47+00:00

I have a customer entity whose records I want to be able to search

  • 0

I have a customer entity whose records I want to be able to search by multiple arbitrary attributes.

E.g.:

Dictionary<string, string> searchList = new Dictionary<string, string>();
searchList.Add("LastName", "Foo");
searchList.Add("FirstName", "Bar");

I could of course write something along these lines:

foreach (KeyValuePair<string, string> kv in searchList)
{
    switch (kv.Key)
    {
       case "FirstName" :
          List<Customer> someResultPart = this._dbSet.Where(customer => customer.FirstName == kv.Value).ToList();
       break;
       case "LastName" :
          // etc.
    }
}

// Do intersection of all result parts ...

Aside from the fact that querying n times and intersecting later on is obviously fugly, I would much rather not have that switch statement in there (since it would force me to change it every single time anything in the Customer class changes.

Is there any way to:

  • “dynamically” build the query based on the searchList dictionary, thereby eliminating the need for the switch construct?
  • “concatenate” the various conditions into a single db query?
  • 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-13T13:34:48+00:00Added an answer on June 13, 2026 at 1:34 pm

    You can use Dynamic LINQ

    string predicate = String.Format("{0} = \"{1}\"", kv.Key, kv.Value);
    List<Customer> someResultPart = _dbSet.Where(predicate);
    

    Or this way:

    List<Customer> someResultPart = _dbSet.Where("{0}");
    

    I’d even constructed full search predicate like this:

    Dictionary<string, object> searchList = new Dictionary<string, object>();
    searchList.Add("LastName", "Foo");
    searchList.Add("FirstName", "Bar");
    searchList.Add("Id", 42); // yep, not only strings
    
    var conditions = searchList.Select((kvp, i) => String.Format("{0} = @{1}", kvp.Key, i));
    string predicate = String.Join(" and ", conditions);
    object[] values = searchList.Select(kvp => kvp.Value).ToArray();
    
    var query = _dbSet.Where(predicate, values);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am new to CRM development. I have a Custom Entity customer. This Entity
I have a 'Customer' POCO entity within my Entity Framework 4 project. I want
I have for one customer entity multiple viewmodels depending on the existing views like
I have a Customer class defined like this: @Entity public class Customer { //...
Trying to map the following schema using the Entity Framework. A Customer can have
I have an Entity Framework data model. Part of the model is a Customer
i have this code : IEnumerable<string> q = customers /*EF entity*/ .Select (c =>
I have a customer dimension. While creating cube and defining dimensions the default attributes
Let's say I have 'Customer' table in SQL DB and I'm using Entity Framework.
I have a Login entity and a Customer entity. Login.username is a foreign key

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.