C# - 使用foreach比较两个对象列表以进行更改

时间:2017-05-15 11:22:08

标签: c# list foreach

对于我的班级作业,我需要将旧学生列表与新列表进行比较,并添加新学生,删除学生,并将学生更改为单独的列表。教师使用嵌套的foreach循环指定而不是LINQ,但我的问题是一旦旧学生列表与新学生中的条目匹配并移动到旧列表中的下一个学生,就会中断循环。

我的代码现在通过嵌套的foreach运行,将条目与旧列表中的第一个条目进行比较,结果在没有ID匹配的情况下出现,因此它将它们放入已删除的列表中并结束循环而不转到旧名单中的下一个学生。

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;
        }
    }

3 个答案:

答案 0 :(得分:0)

使用旗帜怎么样?

        foreach (Student o in oldList)
        {
            bool flag = false;
            foreach (Student n in newList)
            {
                if (FindStudent(o.ID, n.ID))
                {
                    if (CheckChanges(o, n))
                    {
                        changed.Add(n);
                        flag = true;
                        break;
                    }
                }
                else
                {
                    removed.Add(o);
                    flag = true;
                    break;
                }
            }
            if(flag) continue;
        }

答案 1 :(得分:0)

如果您的Student课程覆盖了Equals方法,那么您可以执行以下操作:

public static void CompareStudents(List<Student> oldList, List<Student> newList)
{
    List<Student> added = new List<Student>();
    List<Student> removed = new List<Student>();
    List<Student> changed = new List<Student>();

    foreach(Student n in newList){
        // the added list is a subset of the newList so we begin by cloning the newList
        added.Add(n);
    }

    foreach (Student o in oldList)
    {
        bool existsInNewList = false;
        // we remove every o from the added list
        added.Remove(o);

        foreach (Student n in newList)
        {
            if (o.ID.Equals(n.ID))
            {
                // o and n have the same Id
                existsInNewList = true;
                if (!o.Equals(n))
                {
                    // o and n have the same Id but are different
                    changed.Add(n);
                    added.Remove(n);
                }
                // eventually add a break; here so you don't loop after you've found a n that matches o
            }
        }
        if(!existsInNewList){
            // none of the newStudents have the same Id as o
            removed.Add(o);
        }
    }
}

最后,您应该让所有三个列表addedremovedchanged填充正确的Students

答案 2 :(得分:-1)

我认为您应该使用以下代码:

List<Student> OldStudents = new List<Student>();
List<Student> NewStudents = new List<Student>();
List<Student> StudentsEdit = new List<Student>();

 foreach (var oStud in OldStudents)
     {
        foreach (var nStud in NewStudents)
        {
          if (oStud != nStud)
            StudentsEdit.Add(oStud);
        }
     }

我解释LINQ是由教练提出的,我的不好, 希望这会有所帮助。

此致

N Baua