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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T19:14:32+00:00 2026-05-31T19:14:32+00:00

I am getting a list of products from my database as a BindingList. I

  • 0

I am getting a list of products from my database as a BindingList. I would like to update the quantities some of the products in that list using another list of items already selected by the user.

The idea is when the user brings up a fresh list of products from the database. The list will show the quantities of products that have already been selected from a previous search.

I have come up with the following nested loop. It works but wouldn’t scale well as a search in the database could yield a large list that has to be traversed. How do you guys think I could improve this?

Also, I skimmed through the class where they taught the Big-O notation. What’s the complexity of the below solution?

Thanks.

for (int i = 0; i < dbProducts.Count; i++ )
{
    for (int j = 0; j < GlobalVars.productList.Count; j++)
    {
        EposProduct selectedProduct = GlobalVars.productList.ElementAt(j);
        EposProduct dbProduct = dbProducts.ElementAt(i);
        if(selectedProduct.ProductID == dbProduct.ProductID)
        {
            dbProduct.Quantity = selectedProduct.Quantity;
        }
    }
}
  • 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-31T19:14:34+00:00Added an answer on May 31, 2026 at 7:14 pm

    Your current approach with two nested loops is at best O(n^2) not counting the inner ElementAt method calls. Use a dictionary instead to do this in O(n):

    var gbMap = GlobalVars.productList.ToDictionary(x => x.ProductId, 
                                                    x => x.Quantity); 
    foreach(var product in dbProducts)
    {
        if(gbMap.ContainsKey(product.ProductId))
            product.Quantity = gbMap[product.ProductId];
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am getting from database list of items ordered by descending date. I use
I am getting a list of products from the database through a loop. The
I would like to select one image from products images when i call products
I have a question - I'm using CURL and PROXY. I'm getting list with
I am populating the list view with data I am getting from a JSON
I'm having problems with getting selected items in a select-list. Product product = _pr.GetProducts().ByProductID(productID).First();
Afternoon, I am trying to return a list of products from my SQL Server
I've written a WCF service which tries to read the list items from a
I am getting a really strange error from ActiveResource. I have a method that
Im using Magento CE 1.6.2.. I am displaying products from THREE different categories on

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.