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

Let's say we have two objects. Furthermore, let's assume that they really have no
Assume some domain and view objects (that have no common base class) public class
assume I have two objects article and comment . What I would like to
Assume you have the following simple objects: class Order { public Customer[] Customers {
Let's assume I have a list with objects of type Value . Value has
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
Lets assume I have a list of objects in an array and i want
Let's assume I have a keyspace with a column family that stores user objects
For example, let's assume that i have two QuerySEt objects: queryset1 = my_model1.objects.all().order_by('-created') queryset2

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.