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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T23:25:57+00:00 2026-05-24T23:25:57+00:00

I am getting some behavior from a LINQ Where clause that I can’t quite

  • 0

I am getting some behavior from a LINQ Where clause that I can’t quite grok.

For instance, I am trying to search a list for terms using a AND semantic.

Here is some code:

var items = new List<string>()
{
    "beef",
    "beef soup",
    "beef ribs",
    "chicken soup",
    "soup",
    "pork ribs",
    "ribs"
};

var term = "soup beef";
var terms = term.Split(' ');
var result = items.AsQueryable();
foreach(var t in terms)
{
    result = result.Where( i => i.Contains(t) );
}
result.ToList().Dump();

The result are:

  • beef beef
  • soup
  • beef ribs

However, I was looking for it to an AND result, returning just:

  • beef soup

Now, I CAN GET THE RESULT I WANT by simply adding

.ToList().AsQueryable()

to the end of the Where clause.

Of course, this is not what I want to do if the back end is a database.

What is really odd here is that when I check ‘result’ in the loop, I get the following:

  1. Before first where clause => full list -> makes sense.
  2. After first where (for ‘soup’) => [‘beef soup’, ‘chicken soup’, ‘soup’] -> still looks good
  3. Before second loop (for ‘beef’) => [‘beef’, ‘beef soup’, ‘beef ribs’] -> WHOA! Whats up here
  4. After second loop => stays the same

Can someone explain to me what is going on (and how to fix this correctly)?

  • 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-24T23:25:57+00:00Added an answer on May 24, 2026 at 11:25 pm

    Review Access To Modified Closure

    http://weblogs.asp.net/fbouma/archive/2009/06/25/linq-beware-of-the-access-to-modified-closure-demon.aspx

    Essentially the problem is that t is being modified in your foreach loop, but you are using it within your closure (the Where lambda method), which is accessing t by reference.

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

Sidebar

Related Questions

I'm getting some weird behavior from IE when trying to change a session variable
I am getting some strange behavior from list view. I need to change background
I am getting some behavior I can't decode from GameKit. sometimes the player who
I am getting some unexpected behavior from Html.EditorFor(). I have this controller: [HandleError] public
So I've used UITableView many times before but I'm getting some unusual behavior that
I'm getting some rather unexpected behavior from hashes in ruby here's a simplified demonstration
I am getting some strange behavior involving database queries that I have never seen
I'm getting some very odd behavior and I can't figure it out. I've modified
I'm getting some strange behavior from a listview/the getChildAt method. I have a HashSet,
I'm getting some fairly odd behavior here... I noticed that only localhost, a header

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.