c# - Use linq distinct function? -
this question has answer here:
- linq's distinct() on particular property 17 answers
how can use linq distinct operator here remove remove duplicate courses.cours.name,
var courseslist = courses in not_subsribe select new selectlistitem { text = courses.cours.name, value = courses.cours.id .tostring() };
you use groupby
on anonymous type both properties make them distinct:
var courseslist = c in not_subsribe group c new { id = c.cours.id, name = c.cours.name } g order g.key.name select new selectlistitem { text = g.key.name, value = g.key.id };
that works because anonymous type automatically overrides equals
+gethashcode
needed groupby
or distinct
. use distinct
:
var courseslist = not_subsribe .select(c => new { id = c.cours.id, name = c.cours.name }) .distinct() .orderby(x => x.name) .select(x => new selectlistitem { text = g.key.name, value = g.key.id });
Comments
Post a Comment