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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T11:55:55+00:00 2026-06-13T11:55:55+00:00

I have a classic remove duplicate items from list/nestedlist problem. However, due to the

  • 0

I have a classic remove duplicate items from list/nestedlist problem. However, due to the specific rules that I’m trying to follow, the solution is not straight-forward. I’ve written a sample application that works as desired. But it seems clunky. I am looking for more elegance, and if possible more efficiency. Maybe LINQ/extension methods could help. Any suggestions?


class Program
{
    static void Main(string[] args)
    {
        var sellers = new List<Seller>()
        {
            new Seller()
            {
                Id = 1,
                Products = new List<Product>()
                {
                    new Product() { Sku = "Alpha", Price = 5.0, Shipping = 2.0 },
                    new Product() { Sku = "Beta", Price = 5.0, Shipping = 2.0 }, // more expensive sku within same seller
                    new Product() { Sku = "Beta", Price = 4.0, Shipping = 2.0 },
                    new Product() { Sku = "Gamma", Price = 8.0, Shipping = 2.0 }
                }
            },
            new Seller()
            {
                Id = 2,
                Products = new List<Product>()
                {
                    new Product() { Sku = "Alpha", Price = 5.0, Shipping = 1.0 },
                    new Product() { Sku = "Beta", Price = 5.0, Shipping = 1.0 },
                    new Product() { Sku = "Gamma", Price = 8.0, Shipping = 2.0 }
                }
            }
        };

        // Eliminate duplicate Products amongst all sellers that have matching "Sku".
        // Rules: 
        // Keep the Product with the lowest price. 
        // If price is equal, keep the product with lower shipping.
        // If shipping is also equal, then keep the product with lowest seller Id.
        // If at the end of all comparisons, a seller ends up with no products, then remove that seller.

        // In this example, I expect to have (not necessarily in this order):
        // 1.{Beta, 4.0, 2.0} // Fred.Beta has a lower price than Bob.Beta
        // 1.{Gamma, 8.0, 2.0} // Fred.Gamma is an identical deal to Bob, but Fred is first in the list
        // 2.{Alpha, 5.0, 1.0} // Bob.Alpha has a lower shipping cost than Fred.Alpha

        var newSellers = new List<Seller>();

        foreach (var seller in sellers)
        {
            foreach (var product in seller.Products)
            {
                // TODO: Possible performance improvement? Check for existing seller & product in newSellers before calling any code below.
                var bestSeller = seller;
                var bestProduct = product;
                FindBestSellerAndProduct(sellers, ref bestSeller, ref bestProduct);
                AddIfNotExists(newSellers, bestSeller, bestProduct);
            }
        }

        newSellers.Sort((x, y) => x.Id.CompareTo(y.Id)); // Ensures the list is sorted by seller id... do I really care?
    }

    private static void FindBestSellerAndProduct(IList<Seller> sellers, ref Seller seller, ref Product product)
    {
        string sku = product.Sku;

        foreach (var s in sellers)
        {
            foreach (var p in s.Products.Where(x => x.Sku == sku))
            {
                if ((product.Price > p.Price) ||
                    (product.Price == p.Price && product.Shipping > p.Shipping) ||
                    (product.Price == p.Price && product.Shipping == p.Shipping && seller.Id > s.Id))
                {
                    seller = s;
                    product = p;
                }
            }
        }
    }

    private static void AddIfNotExists(IList<Seller> sellers, Seller seller, Product product)
    {
        var newSeller = sellers.SingleOrDefault(x => x.Id == seller.Id);
        if (newSeller == null)
        {
            // Add input seller and product if seller doesn't already exist in our list.
            newSeller = new Seller() { Id = seller.Id, Products = new List<Product>() };
            newSeller.Products.Add(product);
            sellers.Add(newSeller);
        }
        else
        {
            var newProduct = newSeller.Products.Find(x => x.Sku == product.Sku);
            if (newProduct == null)
            {
                // Add input product if it doesn't already exist in our list
                newSeller.Products.Add(product);
            }
        }
    }

}

// I cannot modify the below classes.
public sealed class Seller
{
    public int Id;
    public List<Product> Products;
}

public sealed class Product
{
    public string Sku;
    public double Price;
    public double Shipping;
}
  • 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-13T11:55:56+00:00Added an answer on June 13, 2026 at 11:55 am

    This query will do the job

    var query = sellers.SelectMany(s => s.Products.Select(p => new { 
                                                       SellerId = s.Id, 
                                                       Product = p })) // 1
                       .OrderBy(x => x.Product.Price) // 2
                       .ThenBy(x => x.Product.Shipping)
                       .ThenBy(x => x.SellerId)
                       .GroupBy(x => x.Product.Sku) // 3
                       .Select(g => g.First()) // 4
                       .GroupBy(x => x.SellerId) // 5
                       .Select(g => new Seller() {  
                            Id = g.Key,
                            Products = g.Select(x => x.Product).ToList() })
                       .ToList();
    

    How it works:

    1. First step – flattening your sequence to list of anonymous types { settlerId, product }
    2. Ordering sequence by your conditions – price, shipping, settlerId
    3. Grouping of ordered sequence by product sku. It will produce groups of { settlerId, product }, where products have same sku, but could belong to different sellers.
    4. Selecting first item from each group. Thus all groups are sorted by your conditions, it will give us best selling products with same sku.
    5. Now we need to create hierarchy again. Do grouping by sellerId, and create Seller object with all it’s best-selling products, if any. If some seller don’t have best-selling products, there will not be group for this seller, and seller will be removed from result.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this classic problem that seems impossible to solve for me. I just
I have a classic ASP app that I am trying to connect to a
I have a classic parent-collection of children relationship that is being used in a
I have a classic sales database that contains millions of rows in certain tables.
We have a classic ASP site that is been hosted in IIS 7.5. I
NET and VB.net code behind. I have a classic ASP page that connects to
What I am trying to do is simple. I have some Classic ASP with
We have a website that uses Classic ASP. Part of our release process substitutes
I have a problem where odd characters (from Word etc) are getting into a
I have a problem which I believe is the classic master/worker pattern, and I'm

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.