For my class assignment I need to compare an old list of students to a new list and add the new students, removed students, and changed students to separate lists.The instructor specified using nested foreach loops and not LINQ but my issue is breaking out of the loops once the old student list matches an entry in the new students and moving to the next student in the old list.
My code right now runs through the nested foreach, compares the entries to the first entry in the old list and as a result comes up without ID matches so it puts them in the removed list and ends the loops without moving on to the next student in the old list.
public static void CompareStudents(List<Student> oldList, List<Student> newList)
{
foreach (Student o in oldList)
{
foreach (Student n in newList)
{
if (FindStudent(o.ID, n.ID))
{
if (CheckChanges(o, n))
{
changed.Add(n);
break;
}
}
else
{
removed.Add(o);
break;
}
}
}
}
private static bool FindStudent(string oldID, string newID)
{
if (newID.Equals(oldID))
{
return true;
}
else
{
return false;
}
}
public static bool CheckChanges(Student oldStu, Student newStu)
{
if (oldStu.FirstName.Equals(newStu.FirstName) &&
oldStu.LastName.Equals(newStu.LastName) &&
oldStu.StudentYear.Equals(newStu.StudentYear) &&
oldStu.StudentRank.Equals(newStu.StudentRank))
{
return false;
}
else
{
return true;
}
}