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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T04:35:10+00:00 2026-05-27T04:35:10+00:00

Trying to build up a query to filter on data in the following manner

  • 0

Trying to build up a query to filter on data in the following manner works OK, returning users filtered by whatever filters are in the FilterNamesAndValues parameter.

GetAllUsersFiltered(..., Dictionary<string,string> FilterNamesAndValues)
{
    ....

    List<DataContracts.IUser> lstUsers = new List<DataContracts.IUser>();
    ....

    var query = from u in lstUsers select u;

    string firstName = string.Empty;
    FilterNamesAndValues.TryGetValue("FirstName", out firstName);
    query = query.Where(u => u.FirstName == firstName);

    string company = string.Empty;
    FilterNamesAndValues.TryGetValue("Company", out company);
    query = query.Where(u => u.CompanyName == company);

    ....
    return query.ToList();
}

The example below however doesn’t work and I can’t see why:

GetAllUsersFiltered(..., Dictionary<string,string> FilterNamesAndValues)
{

    ....

    List<DataContracts.IUser> lstUsers = new List<DataContracts.IUser>();

    ....

    var query = from u in lstUsers select u;

    foreach (KeyValuePair<string, string> kv in FilterNamesAndValues)
        {
            if (kv.Value != null)
            {
                switch (kv.Key)
                {
                    case "FirstName":
                        query = query.Where(u => u.FirstName == kv.Value);
                        break;

                    case "Company":
                        query = query.Where(u => u.CompanyName == kv.Value);
                        break;

                }
            }
        }

        return query.ToList();
}

After the application has hit the first switch case, I can do a query.ToList() and see a row in there. But by the time the execution has gone around the loop to hit the second filter, query.ToList() returns nothing. The query is not filtered successively the way it was in the first example and worse than that, the filter conditions have effectively been lost. There’s probably an obvious explanation for this, but right now I can’t see it.

  • 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-27T04:35:11+00:00Added an answer on May 27, 2026 at 4:35 am

    The problem is that you’re closing over kv in the foreach, but the query is executed using deferred execution. This causes it to close over the wrong value. For details on what’s happening, I’d recommend Eric Lippert’s post titled “Closing over the loop variable considered harmful“.

    You can solve this via a temporary:

    foreach (KeyValuePair<string, string> kvOriginal in FilterNamesAndValues)
        {
            // Make a temporary in the correct scope!
            KeyValuePair<string, string> kv = kvOriginal;
            if (kv.Value != null)
            {
                switch (kv.Key)
                {
                    case "FirstName":
                        query = query.Where(u => u.FirstName == kv.Value);
                        break;
    
                    case "Company":
                        query = query.Where(u => u.CompanyName == kv.Value);
                        break;
    
                }
            }
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to build a query with hibernate criteria for the following scenario: Two
I have a query I'm trying to build so that I can filter by
I am trying to build the following query: `new Select(GTekst = ArrGruppe.Tekst, GLTekst =
I am trying to build a query that analyzes data in our time tracking
I'm trying to build a query similar to the following, but am unsure if
I am trying to build a CAML query for SharePoint 2007 environment, to get
Greetings all, I have a question. I am trying to build a parametrized query
I am trying to find the best way to build a dynamic linq query
I'm trying to build a grammar with the following: NUMERIC: INTEGER | FLOAT |
I'm trying to build more generic query functionality into my application. What I'd like

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.