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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T17:54:45+00:00 2026-05-27T17:54:45+00:00

This is probably a very common problem which has a lot of answers. I

  • 0

This is probably a very common problem which has a lot of answers. I was not able to get to an answer because I am not very sure how to search for it.

I have two collections of objects – both come from the database, and in some cases those collections are of the same object type. Further, I need to do some operations for every combination of those collections. So, for example:

foreach(var a in collection1){
 foreach(var b in collection2){
   if(a.Name == b.Name && a.Value != b.Value)
      //do something with this combination
   else 
      //do something else
}
}

This is very inefficient and it gets slower based on the number of objects in both collections.

What is the best way to solve this type of problems?

EDIT:

I am using .NET 4 at the moment so I am also interested in suggestions using Parallelism to speed that up.

EDIT 2:
I have added above an example of the business rules that need to be performed on each combination of objects. However, the business rules defined in the example can vary.

EDIT 3:
For example, inside the loop the following will be done:
If the business rules are satisfied (see above) a record will be created in the database with a reference to object A and object B. This is one of the operations that I need to do. (Operations will be configurable from child classes using this class).

  • 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-27T17:54:46+00:00Added an answer on May 27, 2026 at 5:54 pm

    If you really have to to process every item in list b for each item in list a, then it’s going to take time proportional to a.Count * b.Count. There’s nothing you can do to prevent it. Adding parallel processing will give you a linear speedup, but that’s not going to make a dent in the processing time if the lists are even moderately large.

    How large are these lists? Do you really have to check every combination of a and b? Can you give us some more information about the problem you’re trying to solve? I suspect that there’s a way to bring a more efficient algorithm to bear, which would reduce your processing time by orders of magnitude.

    Edit after more info posted

    I know that the example you posted is just an example, but it shows that you can find a better algorithm for at least some of your cases. In this particular example, you could sort a and b by name, and then do a straight merge. Or, you could sort b into an array or list, and use binary search to look up the names. Either of those two options would perform much better than your nested loops. So much better, in fact, that you probably wouldn’t need to bother with parallelizing things.

    Look at the numbers. If your a has 4,000 items in it and b has 100,000 items in it, your nested loop will do 400 million comparisons (a.Count * b.Count). But sorting is only n log n, and the merge is linear. So sorting and then merging will be approximately (a.Count * 12) + (b.Count * 17) + a.Count + b.Count, or in the neighborhood of 2 million comparisons. So that’s approximately 200 times faster.

    Compare that to what you can do with parallel processing: only a linear speedup. If you have four cores and you get a pure linear speedup, you’ll only cut your time by a factor of four. The better algorithm cut the time by a factor of 200, with a single thread.

    You just need to find better algorithms.

    LINQ might also provide a good solution. I’m not an expert with LINQ, but it seems like it should be able to make quick work of something like this.

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

Sidebar

Related Questions

This is probably a very novice question, but I could not find an answer.
OK, this is probably very basic, but I can't find the problem. I'm trying
I'm feeling fairly frustrated, because I feel like this is probably very easy/obvious and
This is probably a very common question but here it goes. I created an
This is probably a very common question, but I was unable to find an
I probably sholdn't obsess about this too much, but my project has a very
This is probably a common situation, but I couldn't find a specific answer on
This is probably very simple but it's really confusing me. When I implement the
This is probably very easy to do but for some reason I can't seem
This is probably a very simple question, I apologise - I'm new to rails.

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.