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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T13:19:07+00:00 2026-06-02T13:19:07+00:00

While troubleshooting a bug I ran into this. It seems to be due to

  • 0

While troubleshooting a bug I ran into this. It seems to be due to deferred execution and the query is using the last value of variable keyword. What is the recommended pattern to solve this.

var query = from c in dbContext.Cars
            select c;

var keywords = new string[] { "Clean", "Car" };

foreach (var keyword in keywords)
{
    query = query.Where(c => c.Name.Contains(keyword));
}

var cars = query.ToList();

Results in query like this (note the value of both parameters is “Car”)

exec sp_executesql N'SELECT 
[Extent1].[CarID] AS [MID], 
[Extent1].[Name] AS [Name], 
FROM [dbo].[Cars] AS [Extent1]
WHERE ([Extent1].[Name] LIKE @p__linq__0 ESCAPE N''~'') AND ([Extent1].[Name] LIKE @p__linq__1 ESCAPE N''~'')
',N'@p__linq__0 nvarchar(4000),@p__linq__1 nvarchar(4000)',@p__linq__0=N'%Car%',@p__linq__1=N'%Car%'

Debugging in LINQPad also shows same query

DECLARE p__linq__0 NVarChar(1) = '%Car%'
DECLARE p__linq__1 NVarChar(1) = '%Car%'

SELECT 
[Extent1].[CarID] AS [MID], 
[Extent1].[Name] AS [Name], 
FROM [dbo].[Cars] AS [Extent1]
WHERE ([Extent1].[Name] LIKE @p__linq__0 ESCAPE N'~') AND ([Extent1].[Name] LIKE @p__linq__1 ESCAPE N'~')
  • 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-06-02T13:19:12+00:00Added an answer on June 2, 2026 at 1:19 pm

    It’s due to the way variables are captured in anonymous functions. Change this:

    foreach (var keyword in keywords)
    {
        query = query.Where(c => c.Name.Contains(keyword));
    }
    

    to this:

    foreach (var keyword in keywords)
    {
        var copy = keyword;
        query = query.Where(c => c.Name.Contains(copy));
    }
    

    and it should work. See Eric Lippert’s blog post for more details. The behaviour is changing for C# 5, so you won’t need to worry at that point.

    Note, however, that you could also try to let LINQ do this for you:

    var query = dbContext.Cars.Where(c => keywords.All(key => c.Name.Contains(key));
    

    I’m pretty sure that would work for Any (an “OR” effectively) but I don’t know for sure whether it will work with All. Work a try…

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

Sidebar

Related Questions

I'm currently using an SQL CONNECT statement to access a remote system. While troubleshooting,
This is a very weird bug I'm having. I'm using Modernizer and after a
I have been troubleshooting this for a while, and I am kind of new
While using regex to help solve a problem in the Python Challenge , I
While troubleshooting a misbehavior in Spree where the product list was not paginating and
In a recent project, I had the pleasure of troubleshooting a bug that involved
I am using SQL Developer and want to output the contents of a variable
I've tried to include as much info in this post as possible. I'm using
While troubleshooting a server with very high I/O wait, I notice there is a
I'm having some problems with deploying my application and while troubleshooting, I came across

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.