Search code examples
c#linqdistinctiequalitycomparer

Removing Items from a list of objects when an object property is duplicated


I am reasonably new to linq and c# so apologies if I am being dumb. I have a query which brings back a list of product info, prices of these products and categories these products are in based on some variables passed in:

var y = (from pl in dc.Products 
                 join pr in dc.Prices 
                 on pl.ID equals pr.ProductID 
                 join c in dc.Categories
                 on pl.ID equals c.ProductID
                 where pr.Currency == Currency  
                 && (string.IsNullOrEmpty(Cat1) || c.Cat1 == Cat1)
                 && (string.IsNullOrEmpty(Cat2) || c.Cat2 == Cat2)
                 && (string.IsNullOrEmpty(Cat3) || c.Cat3 == Cat3)
                 && (string.IsNullOrEmpty(Cat4) || c.Cat4 == Cat4)
                 && (string.IsNullOrEmpty(Cat5) || c.Cat5 == Cat5)
                 select new {pl,pr,c});

This works fine as far as it goes but say for example a row of y has ID = 1 and Cat1 = Foo and another row has ID = 1 and Cat1 = Bar, then obviously the query above will bring both back. Is there a way to modify this query so that I can bring back just one row for each ID.

I've Googled this for about 2 hours and tried group bys, .Distinct with an IEqualityComparer and other methods I found but with no success. My understanding isn't good enough and I'd be grateful for any help!


Solution

  • You should do this by making a HashSet where the key is your property.

    List<Product> distinctProducts = new List<Product>();
    HashSet<string> dupSet = new HashSet<string>();
    foreach (Product p in Product)
    {
        if (dupSet.ContainsKey(p.Cat1))
        {
             // do not add
        }
        else
        {
             dupSet.Add(p.Cat1);
             distinctProducts.Add(p);
        }
    }
    

    If you want to remove the items from the original list you can use a for loop starting at the end (you can not remove items from a list in a for loop).