Search code examples
c#asp.net-mvcasp.net-mvc-3razorrazor-2

How to remove duplicate entries from model list in razor view


I have Registration list which has duplicate entries in it for student then how can i remove these duplicate entries in razor view. I know that i can handle this in c# but I want to remove duplicate entries in razor view. Below is my code:-

  foreach (var stud in Model.Registrations)
                {
                    <text> @registration.Student.FirstName @stud.Student.LastName </text><br />
                }

what is the statement for that. is it something like this:-

       foreach (var stud in Model.Registrations.GroupBy(s => s.Student.ID).Select(g => g.First()))
    {
      // Student Name and ID
    }

Above statement is not work for me. What is the code to do this?


Solution

  • foreach (var stud in Model.Registrations.GroupBy(x => x.Id).Select(y => y.First()).ToList())
      {
         <text> @stud.Student.FirstName @stud.Student.LastName </text><br />
      }
    

    use any one unique column to get unique rows in the "GroupBy(x => x.Id)" here i am using Id(as example), so that you will not get duplicate rows...