c# - Don't bind duplicate items to checked list box -
i've got list of objects have team details of salesmen. list has several teams have same name salesman different.
the teamdetails class has following attributes:
string teamname; string region; int teamsales; string salesmanfullname; string salesmanaddress;
the user has option find teams have sales on value. these teams added check box list.
this how i'm populating check box list:
var viewlist = tosearch in globalvariables.allsalesmenlist tosearch.teamsales > convert.toint32(txtsalessearch.text) select tosearch; searchcheckedlistbox.datasource = viewlist.tolist(); searchcheckedlistbox.displaymember = "teamname";
the problem i'm having team name shown more once if team has more 1 salesman.
how prevent checkbox having repeated values?
try use distinct comparer:
var viewlist = tosearch in globalvariables.allsalesmenlist tosearch.teamsales > convert.toint32(txtsalessearch.text) select tosearch; searchcheckedlistbox.datasource = viewlist.distinct(new teamcomparer()).tolist(); searchcheckedlistbox.displaymember = "teamname";
comparer code:
public class teamcomparer : iequalitycomparer<teamdetails> { public bool equals(teamdetails x, teamdetails y) { if (x.teamname == y.teamname) return true; return false; } public int gethashcode(teamdetails obj) { if (object.referenceequals(obj, null)) return 0; return obj.teamname.gethashcode(); } }
Comments
Post a Comment