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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T16:49:36+00:00 2026-05-10T16:49:36+00:00

Assume my objects are in perfect working order (i.e. TDD makes me think they

  • 0

Assume my objects are in perfect working order (i.e. TDD makes me think they work).

I have a list that I create like this (except indented properly):

var result = from v in vendors              from p in v.Products              orderby p.Name              select p; 

This works – I get all products from all vendors.

Now I have a list of conditions, built up at runtime by the user. Let’s apply them:

foreach (Attribute a in requiredAttributes) {     result = result.Where(p => p.Attributes.Contains(a)); } 

This may be primitive, but I thought it’d work. However, after this foreach loop is finished, when you enumerate ‘result’ it will contain all products that has the LAST Attribute of the requiredAttributes collection in it’s Attributes property (also a collection).

To me, this smells like ‘a’ is overwritten somewhere with each trip trough the loop, and only the last one applies.

Short of somehow writing an extension method to IEnumerable called ContainsAll(IEnumerable) or something to that effect, how can I achieve what I want, which is basically a logical AND, giving me only those products that has ALL the required attributes?

  • 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. 2026-05-10T16:49:36+00:00Added an answer on May 10, 2026 at 4:49 pm

    (Edited for clarity.)

    The problem is the foreach loop, and the fact that the ‘a’ variable is being captured and then changed each time. Here’s a modification which will work, by effectively introducing a ‘new’ variable for each iteration of the loop, and capturing that new variable.

    foreach (Attribute a in requiredAttributes) {     Attribute copy = a;     result = result.Where(p => p.Attributes.Contains(copy)); } 

    Omer’s solution is a cleaner one if you can use it, but this may help if your real code is actually more complicated 🙂

    EDIT: There’s more about the issue in this closures article – scroll down to ‘Comparing capture strategies: complexity vs power’.

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

Sidebar

Related Questions

I have a hierarchical list of objects. Assume that the structure is as follows:
Let's assume I have 2 List<T> List1 and List2 that look like this: List
Assume you have some objects which have several fields they can be compared by:
Assume that I have a set of objects that need to be analyzed in
Assume you are doing something like the following List<string> myitems = new List<string> {
Assume that we have N erlang nodes, running same application. I want to share
Assume, for whatever reason, that we have three inline scripts on a page. Each
Assume you have a page in ASP.NET where it makes sense to use JavaScript/jQuery
I am looking to find a way to take two objects that have identical
I have an application which makes use of System.Timers.Timer objects to do expirations and

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.