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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T19:09:09+00:00 2026-06-12T19:09:09+00:00

been struggling with this for a while now. I’m dipping my toe in the

  • 0

been struggling with this for a while now.

I’m dipping my toe in the WebAPI world and I have a List that can contains products with the same name but different prices. What I need to do is remove all references to a product is variations in price occur.

eg.
name = “Cornflakes” Price = “1.99M”
name = “Cornflakes” Price = “1.89M”
name = “Rice Krispies” Price = “2.09M”
name = “Cornflakes” Price = “2.09M”

No cornflakes should appear in my final list.

I’ve got the bulk written but it’s removing the products too soon and I’m unsure where I should be removing them.

public IEnumerable<Product> GetProductsByCategory(int Id)
    {
        List<Product> sourceProductList = products.Where(p => p.CategoryID == Id).ToList();
        List<Product> tempProducts = new List<Product>();
        List<Product> targetProductList = new List<Product>();

        foreach (var product in sourceProductList)
        {
            bool isInTempList = tempProducts.Any(x => x.Name == product.Name);
            if (!isInTempList)
            {
                tempProducts.Add(product);
            }
            else
            {
                Product tempProduct = product;
                bool isPriceDifferent = tempProducts.Where(y => y.Name == tempProduct.Name).Any(y => y.Price != tempProduct.Price);
                if (isPriceDifferent)
                {
                    tempProducts.RemoveAll(p => p.Name == product.Name); 
                    // too soon as I may have lots of products with the same name
                    // but need to remove based on product.Name
                }
            }
        }
        targetProductList.AddRange(tempProducts);

        return targetProductList;
    }

Any help would be greatly appreciated.

Note: other cereals are available

  • 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-06-12T19:09:10+00:00Added an answer on June 12, 2026 at 7:09 pm

    Please try this:

    class Program
        {
            static void Main(string[] args)
            {
                var list = new List<Product>
                    {
                        new Product() {Name = "Cornflakes", Price = 100},
                        new Product() {Name = "Cornflakes", Price = 200},
                        new Product() {Name = "Rice Krispies", Price = 300},
                        new Product() {Name = "Cornflakes", Price = 400}
                    };
    
                var uniqueItems = list.Where(w => (!list.Any(l=>l.Name.Equals(w.Name) && l != w)));
    
            }
    
            public class Product
            {
    
                public string Name { get; set; }
                public decimal Price { get; set; }
            }
        }
    

    In the result you will have only one “Rice Krispies” item. I’m sure it will work faster than solution with GroupBy and Distinct, because we don’t need to do these unnecessary things in your case.

    Working Code – http://ideone.com/X8A3v

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

Sidebar

Related Questions

I have been struggling with this for a while now. given a set of
I've been struggling with this issue for a while now. I have OpenCart 1.5.2.1
I've been struggling with this for quite a while My application contains a list
I've been struggling with this for a while now so hopefully someone can help
I have been struggling with this a while now. I implemented a basic IHttpHandler
I have been struggling with this for a while now. I have a Master
Iv been struggling with this for a while now an I have exhausted all
I've been struggling with this for a while now and I really can't find
I have been struggling with this for a while now, somehow JQuery is escaping
I've been struggling with this issue for a while now. Maybe you can help.

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.