people.Distinct()。ToList()不会删除所有重复项

时间:2019-03-13 12:46:35

标签: c# list model-view-controller

我有一个列表,该列表通过方法将对象添加到其中,无论对象添加到何处,这些对象都包含相同的信息。然后,我执行people.Distinct().ToList(),该删除所有重复项。也许我不太了解,但是我认为它将删除所有重复项,但是列表仍然在两个索引中包含相同的值。

GIF显示: Gif

代码:

public static List<Person> GetEmailAddress(EmailViewModel model, EyeTEntities context)
    {
        List<Person> people = new List<Person>();
        List<Person> StudentOutstanding = new List<Person>();
        List<Person> Students = new List<Person>();
        foreach (ListBoxViewModel lbvm in model.SenderList)
        {
            switch (lbvm.Type)
            {
                case (int)Shared.Enums.EmailContactType.Employee:
                    {
                        people.Add(GetEmployee(context, lbvm.ID));
                        List<Person> Schools = GetSchools(context, lbvm.ID);
                        people.AddRange(Schools);
                        if (!model.TeachersOnly)
                        {
                            Students.AddRange(GetStudents(context, Schools, null));
                        }
                    }
                    break;
                case (int)Shared.Enums.EmailContactType.School:
                    {
                        Person school = GetSchool(context, lbvm.ID);
                        people.Add(school);
                        if (!model.TeachersOnly)
                        {
                            Students.AddRange(GetStudents(context, null, school.ID));
                        }
                    }
                    break;
                case (int)Shared.Enums.EmailContactType.Student:
                    {
                        Person Student = (from e in context.Students
                                                 where e.SchoolID == lbvm.ID
                                                 select new Person
                                                 {
                                                     Address = e.Email,
                                                     Name = e.FirstName + " " + e.Lastname,
                                                     ContactPerson = e.Contact
                                                 }).FirstOrDefault();
                        Students.Add(Student);                            
                    }
                    break;
                default:
                    //Console.WriteLine("Default case");
                    break;
            }


        }
        if (model.Outstanding)
        {
            Students = ReturnOutstandingStudents(Students);
            people.Clear();                
        }
        people.AddRange(Students);
        people = people.Distinct().ToList();
        return people;
    }

谢谢。

0 个答案:

没有答案