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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T11:52:58+00:00 2026-05-19T11:52:58+00:00

Using a Dictionary I’m looping over an IEnumerable and apply filters according to the

  • 0

Using a Dictionary I’m looping over an IEnumerable and apply filters according to the filter dictionary each time. FilterType enum decides what field the filter should be applied to and the string is the freetext filter itself.

The problem I have is that if I add more than one filter to the dictionary (i.e. both a FilterType.Customer and a FilterType.Name filter) the filters will be applied to both the Customer and Name fields BUT the freetext filter will be taken from the first filter added. I.e. in the code below the nameFilterBox.Text gets applied to both the Name and Customer fields in the LINQ query, whereas customerFilterBox.Text doesn’t get applied at all. So the FilterType part of the dictionary gets read twice but the string filter only gets read from the first one.

I really can’t understand why this is happening.

static IEnumerable<Jobb> jobQuery = GetInitialJobQuery();

Dictionary<FilterType, string> filters = new Dictionary<FilterType, string>();
filters.Add(FilterType.Name, nameFilterBox.Text);
filters.Add(FilterType.Customer, customerFilterBox.Text);

foreach (var filter in filters)
{
    switch (filter.Key)
    {
        case FilterType.Customer:
            jobQuery = jobQuery.Where(x => x.KUNDREF != null &&
                       x.KUNDREF.ToLower().Contains(filter.Value.ToLower()));
            break;

        case FilterType.Name:
            jobQuery = jobQuery.Where(x => x.JOBBESKR != null &&
                       x.JOBBESKR.ToLower().Contains(filter.Value.ToLower()));
            break;
    }
}
  • 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-19T11:52:59+00:00Added an answer on May 19, 2026 at 11:52 am

    This is the foreach loop variable capturing bug. Your lambdas operate on the last value in filters, not the one you think.

    The workaround is to declare a new variable inside the foreach loop:

    foreach (var filterForeach in filters)
    {
        var filter = filterForeach;  // for the lambdas
        switch (filter.Key)
        {
            case FilterType.Customer:
                jobQuery = jobQuery.Where(x => x.KUNDREF != null &&
                           x.KUNDREF.ToLower().Contains(filter.Value.ToLower()));
                break;
    
            case FilterType.Name:
                jobQuery = jobQuery.Where(x => x.JOBBESKR != null &&
                           x.JOBBESKR.ToLower().Contains(filter.Value.ToLower()));
                break;
        }
    }
    

    Alternatively, since you use only filter.Value inside the lambdas, you could do this too:

    foreach (var filter in filters)
    {
        var value = filter.Value.ToLower();  // for the lambdas
        switch (filter.Key)
        {
            case FilterType.Customer:
                jobQuery = jobQuery.Where(x => x.KUNDREF != null &&
                           x.KUNDREF.ToLower().Contains(value));
                break;
    
            case FilterType.Name:
                jobQuery = jobQuery.Where(x => x.JOBBESKR != null &&
                           x.JOBBESKR.ToLower().Contains(value));
                break;
        }
    }
    

    Eric Lippert blogged about this here: Closing over the loop variable considered harmful.

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

Sidebar

Related Questions

I'd like to call a function in python using a dictionary with matching key-value
I am using LINQ to query a generic dictionary and then use the result
I have a dictionary that I am using to define the ratings of copper
Is there similar component for Delphi like dictionary of Python? I'm now using TStringList
While using dictionary, i always override GetHashCode and Equals ( or provide a custom
Instead of using Dictionary<TKey,TValue> I want some type of collection class that can use
I am using dictionary for storing ID,otherID and bool value. Unfortunately it compares array
I'm using Dictionary<string, string> as configuration for instruments, and it'd be easier for my
is there a way that I can save the model by using dictionary for
How does the process of hashing work in Dictionary? I read that using dictionary

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.