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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T22:07:23+00:00 2026-05-10T22:07:23+00:00

I ran into a scenario where LINQ to SQL acts very strangely. I would

  • 0

I ran into a scenario where LINQ to SQL acts very strangely. I would like to know if I’m doing something wrong. But I think there is a real possibility that it’s a bug.

The code pasted below isn’t my real code. It is a simplified version I created for this post, using the Northwind database.

A little background: I have a method that takes an IQueryable of Product and a ‘filter object’ (which I will describe in a minute). It should run some ‘Where’ extension methods on the IQueryable, based on the ‘filter object’, and then return the IQueryable.

The so-called ‘filter object’ is a System.Collections.Generic.List of an anonymous type of this structure: { column = fieldEnum, id = int }

The fieldEnum is an enum of the different columns of the Products table that I would possibly like to use for the filtering.

Instead of explaining further how my code works, it’s easier if you just take a look at it. It’s simple to follow.

enum filterType { supplier = 1, category } public IQueryable<Product> getIQueryableProducts() {     NorthwindDataClassesDataContext db = new NorthwindDataClassesDataContext();     IQueryable<Product> query = db.Products.AsQueryable();      //this section is just for the example. It creates a Generic List of an Anonymous Type     //with two objects. In real life I get the same kind of collection, but it isn't hard coded like here     var filter1 = new { column = filterType.supplier, id = 7 };     var filter2 = new { column = filterType.category, id = 3 };     var filterList = (new[] { filter1 }).ToList();     filterList.Add(filter2);      foreach(var oFilter in filterList)     {         switch (oFilter.column)         {             case filterType.supplier:                 query = query.Where(p => p.SupplierID == oFilter.id);                 break;             case filterType.category:                 query = query.Where(p => p.CategoryID == oFilter.id);                 break;             default:                 break;         }     }     return query; } 

So here is an example. Let’s say the List contains two items of this anonymous type, { column = fieldEnum.Supplier, id = 7 } and { column = fieldEnum.Category, id = 3}.

After running the code above, the underlying SQL query of the IQueryable object should contain:

WHERE SupplierID = 7 AND CategoryID = 3 

But in reality, after the code runs the SQL that gets executed is

WHERE SupplierID = 3 AND CategoryID = 3 

I tried defining query as a property and setting a breakpoint on the setter, thinking I could catch what’s changing it when it shouldn’t be. But everything was supposedly fine. So instead I just checked the underlying SQL after every command. I realized that the first Where runs fine, and query stays fine (meaning SupplierID = 7) until right after the foreach loop runs the second time. Right after oFilter becomes the second anonymous type item, and not the first, the ‘query’ SQL changes to Supplier = 3. So what must be happening here under-the-hood is that instead of just remembering that Supplier should equal 7, LINQ to SQL remembers that Supplier should equal oFilter.id. But oFilter is a name of a single item of a foreach loop, and it means something different after it iterates.

  • 1 1 Answer
  • 1 View
  • 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. 2026-05-10T22:07:24+00:00Added an answer on May 10, 2026 at 10:07 pm

    I have only glanced at your question, but I am 90% sure that you should read the first section of On lambdas, capture, and mutability (which includes links to 5 similar SO questions) and all will become clear.

    The basic gist of it is that the variable oFilter in your example has been captured in the closure by reference and not by value. That means that once the loop finishes iterating, the variable’s reference is to the last one, so the value as evaluated at lambda execution time is the final one as well.

    The cure is to insert a new variable inside the foreach loop whose scope is only that iteration rather than the whole loop:

    foreach(var oFilter in filterList) {     var filter = oFilter; // add this     switch (oFilter.column) // this doesn't have to change, but can for consistency     {         case filterType.supplier:             query = query.Where(p => p.SupplierID == filter.id); // use `filter` here             break; 

    Now each closure is over a different filter variable that is declared anew inside of each loop, and your code will run as expected.

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

Sidebar

Related Questions

I ran into this very odd scenario. This won't hide the H1: if ($('#content
I ran into an interesting (and very frustrating) issue with the equals() method today
I ran into an issue with something that I am probably just overlooking. I
Hoping someone has ran into this scenario and can lead me in the right
(Rhetorical Question) I ran into a bizarre scenario today where I wanted PHP to
This is scenario that I ran into a few times: I copy some text
I am a bit new to Fluent nHibernate and ran into a scenario with
Possible Duplicate: Type result with Ternary operator in C# I ran into this scenario,
I ran into a very disturbing issue that's been puzzling me for a while
I've ran into this scenario a few times now and I cant explain it.

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.