c# - Filter duplicates from a list to populate ComboBox -


i've been looking way filter out duplicates list populate form, far have found create duplicate list either hashset or other methods involve grouping duplicates separate list, i'm not interested in keeping extras.

currently i'm getting in combobox is:

123 123 456 456 789 789 

etc... trouble is, i'm collecting data in models (or classes) such:

list<modelname>  modelname<1> {    string name = bob;    int number = 123; } modelname<2> {    string name = jim;    int number = 123; } modelname<3> {    string name = bob;    int number = 456; } 

is there way fill list unique classes:

modelname<1> {     name;      number; } modelname<2> {     name; } modelname<3> {     number; } 

and filter out , dispose of double ups?

you can use linq distinct operator remove duplicates collection:

var listwithoutduplicates = listwithduplicates.distinct().tolist(); 

if want customize way elements compared equality can use overload requires iequalitycomparer<t>.

in case, if want define "equality" having same value of location property can use equalitycomparer:

class equalitycomparer : iequalitycomparer<classname> {    public boolean equals(classname x, classname y) {     return equals(x.location, y.location);   }    public int32 gethashcode(classname obj) {     return obj.location.gethashcode();   }  } 

and distinct items location:

var listwithoutduplicates = listwithduplicates.distinct(new equalitycomparer).tolist(); 

Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -