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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T17:48:01+00:00 2026-05-31T17:48:01+00:00

I was reading C# 4 in a Nutshell and I’ve come to this piece

  • 0

I was reading C# 4 in a Nutshell and I’ve come to this piece of code:

IQueryable<Product> SearchProducts (params string[] keywords)
{
  IQueryable<Product> query = dataContext.Products;

  foreach (string keyword in keywords)
  {
    string temp = keyword;
    query = query.Where (p => p.Description.Contains (temp));
  }
  return query;
}

Right after the code there’s a ‘warning’ that goes like this:

The temporary variable in the loop is required to avoid the outer variable trap, where the same variable is captured for each iteration of the foreach loop.

I don’t get it, I don’t understand why the temp variable is necessary. What is the outter variable trap?

From: http://www.albahari.com/nutshell/predicatebuilder.aspx

Can anybody please clarify this?

  • 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-31T17:48:02+00:00Added an answer on May 31, 2026 at 5:48 pm

    Because there is only one variable called keyword that is closed over. The temporary variable, however, is different each iteration.

    Thus, without the temporary variable, when the lambda is executed later, keyword evaluates to the last value it was assigned in the loop.

    Another solution, in some cases, is to force the evaluation (and end up with a List or such).

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

Sidebar

Related Questions

Reading code from other posts, I'm seeing something like this. struct Foo { Foo()
I was reading Algorithms in a Nutshell (O'Reilly) and came across this symbol in
Reading this question I found this as (note the quotation marks) code to solve
Reading Java Concurrency In Practice, there's this part in section 3.5: public Holder holder;
i have came across the sentence in a nutshell while reading a technical book
I'm reading C# 4.0 in a Nutshell by the Albahari brothers and I came
I am confused by a code listing in a book i am reading, C#
Reading source code of my current project, I see: [self retain] in one class,
Reading over some example Objective C code just now. @property (nonatomic, strong) IBOutlet UILabel
Reading or writing a C code, I often have difficulties translating the numbers from

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.