[Solved] Use linq distinct function? [duplicate]


You could use GroupBy on an anonymous type with both properties to make them distinct:

var coursesList = 
     from c in not_subsribe
     group c by new { Id = c.Cours.ID, Name = c.Cours.Name } into g
     order by g.Key.Name
     select new SelectListItem
     {
         Text = g.Key.Name,
         Value = g.Key.Id
     };

That works because an anonymous type automatically overrides Equals+GetHashCode for you what is needed for GroupBy or Distinct. So now you could also 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
     }); 

2

solved Use linq distinct function? [duplicate]