Create Student test

public class Student { public int ID { get; set; } public string Name { get; set; }}Copy the code

Create a list

List<Student> list = new List<Student>();
Copy the code

Add data

list.Add(new Student() { ID = 1, Name = "Jack" });
list.Add(new Student() { ID = 2, Name = "Jane" });
list.Add(new Student() { ID = 2, Name = "Mary" });
Copy the code

Now, determine if the Name is repeated

Use double for loops

private bool IsRepeat()
{
    for (int i = 0; i < list.Count; i++)
    {
        for (int j = i + 1; j < list.Count; j++)
        {
            if (list[i].Name == list[j].Name)
            {
                return true;
            }
        }
    }
    return false;
}
Copy the code

Use Linq

bool isRepeat = list.GroupBy(i => i.Name).Where(g => g.Count() > 1).Count() > 0;
Copy the code