I'm trying to find all the duplicates in my ObservableCollection, using Lambda/LINQ
Say I have my ObservableCollection<Student> studentList
Each Student has fields
Name
Age
Class
Now in the list, say the following is input in the list
studentList.Add(new Student()
{
Name = Name,
Age= Age,
Class = Class
});
Each time the user enters the data and clicks the button, the data is added. So in my list I would have (as an example)
Bob, 6, Blue Class
Jerry 8, Red Class
Andy 7, Red Class
Bob, 10, Red Class
I would like to loop through the list and find out which names are duplicates So far my code is:
bool cont = false;
foreach (var dup in studentList)
{
if (!studentList.Any(x => x.Name== dup.Name))
{
cont = true;
}
else
{
cont = false;
break;
}
}
But something is not right. It will always "Bob" in the if statement. And if I entered 4 completley different names, it would still say false. It takes the last entered value and always compares it to that one, never actually checking them all.
It is a bit hard to explain. If I knew where the problem lie, I would be of more help.
All I would like is to loop through my list to find matching values, and to set the bool to false and break the loop, so that the user can be notified and can make changes before they continue. And I would like the solution to be in a Lambda/LINQ statement