c# - Use linq distinct function? -


this question has answer here:

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

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? -