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


i reasonably new linq , c# apologies if being dumb. have query brings list of product info, prices of these products , categories these products in based on 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                  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 far goes example row of y has id = 1 , cat1 = foo , row has id = 1 , cat1 = bar, query above bring both back. there way modify query can bring 1 row each id.

i've googled 2 hours , tried group bys, .distinct iequalitycomparer , other methods found no success. understanding isn't enough , i'd grateful help!

you should making hashset key property.

list<product> distinctproducts = new list<product>(); hashset<string> dupset = new hashset<string>(); foreach (product p in product) {     if (dupset.containskey(p.cat1))     {          // not add     }     else     {          dupset.add(p.cat1);          distinctproducts.add(p);     } } 

if want remove items original list can use loop starting @ end (you can not remove items list in loop).


Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -