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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T20:21:28+00:00 2026-05-14T20:21:28+00:00

When I run this snippet of code: string[] words = new string[] { foo,

  • 0

When I run this snippet of code:

string[] words = new string[] { "foo", "bar" };
var results = from row in Assets select row;
foreach (string word in words)
{
    results = results.Where(row => row.Name.Contains(word));
}

I get this SQL:

-- Region Parameters
DECLARE @p0 VarChar(5) = '%bar%'
DECLARE @p1 VarChar(5) = '%bar%'
-- EndRegion
SELECT ... FROM [Assets] AS [t0]
WHERE ([t0].[Name] LIKE @p0) AND ([t0].[Name] LIKE @p1)

Note that @p0 and @p1 are both bar, when I wanted them to be foo and bar.

I guess Linq is somehow binding a reference to the variable word rather than a reference to the string currently referenced by word? What is the best way to avoid this problem?

(Also, if you have any suggestions for a better title for this question, please put it in the comments.)

Note that I tried this with regular Linq also, with the same results (you can paste this right into Linqpad):

string[] words = new string[] { "f", "a" };
string[] dictionary = new string[] { "foo", "bar", "jack", "splat" };
var results = from row in dictionary select row;
foreach (string word in words)
{
    results = results.Where(row => row.Contains(word));
}
results.Dump();

Dumps:

bar
jack
splat
  • 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-14T20:21:29+00:00Added an answer on May 14, 2026 at 8:21 pm

    You’re using what’s called a “closure”, which means that you’re defining an anonymous function (your lambda) that uses a local variable in its body. Specifically, you’re “closing” over the loop variable word. The issue that arises with closures results from delayed execution, which means that the body of your lambda is not run when you define it, but when it’s invoked.

    Because of this, you almost never want to close over the loop variable. Because of delayed execution, the value of the variable within the lambda will be whatever it is when the lambda is executed. For lambdas declared inside a loop and invoked outside of it, this means that it will always have the last value from the loop.

    To counteract this, use a local variable declared inside the loop. This will cause it to capture the value at that point in time and pass a new variable to every lambda that’s created. Like so:

    string[] words = new string[] { "foo", "bar" }; 
    var results = from row in Assets select row; 
    foreach (string word in words) 
    { 
        string tempWord = word;
    
        results = results.Where(row => row.Name.Contains(tempWord)); 
    } 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 436k
  • Answers 436k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer something like $('.circle.runned').each(function(){ if($(this).offset().left + $(this).width < $('#wraper').offset().left || $(this).offset().left… May 15, 2026 at 3:56 pm
  • Editorial Team
    Editorial Team added an answer Try setting up your own proxy and connecting to it... May 15, 2026 at 3:56 pm
  • Editorial Team
    Editorial Team added an answer Methods that return void state more clearly that they have… May 15, 2026 at 3:56 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.