如何在不使用Linq的情况下比较两个添加,删除和更改列表? C#

时间:2019-05-21 01:28:48

标签: c#

启动项目时遇到了一些麻烦。我得到了需要做什么的信息,但是我完全无法实现如何的实现。我绝不是要别人为我做这件事,只是朝着正确的方向前进。当我已经编写了代码时,我得到了一个程序。在这段代码中,有两种方法包含两个对象列表。

我应该将这两个列表进行比较,以查找两个列表之间发生的添加,删除和更改。我不允许使用LINQ,但它应该有一定的效率。我想我不确定如何从方法中“拉”这些列表并在其中使用它们,然后进行比较。我知道我可以使用additions = newList.Except(oldList);之类的位,但是我不确定当列表在其中时如何实现。谢谢您的任何建议!

在此处快速编辑帖子。我忘了提到一个Student类,它为idNumber,firstName,lastName,studentYear和studentRank设置必需的值。这两个列表都包含哪些内容,需要相互比较。

我尝试过类似的事情:

foreach (Student s in newList)
{
    if (!(oldList.Contains(s)))
    {
        additions.Add(s);
    }
}

我知道有些地方 additions = newList.Except(oldList);

但是我不确定如何使用这些单独方法中的列表。我也无法使用LINQ,在我看到的针对此类问题的99%的帖子中都建议使用

以下是创建列表的内容:

public static List<Student> GetStudentsOld()
{
    var oldStudents = new List<Student>();

    oldStudents.Add(new Student("111", "Michael", "Tucker", "Junior", 10));
    oldStudents.Add(new Student("222", "Svetlana", "Omelchenko", "Senior", 2));
    oldStudents.Add(new Student("333", "Michiko", "Osada", "Senior", 7));
    oldStudents.Add(new Student("444", "Hugo", "Garcia", "Junior", 16));
    oldStudents.Add(new Student("555", "Cesar", "Garcia", "Freshman", 4));
    oldStudents.Add(new Student("666", "Fadi", "Fakhouri", "Senior", 72));
    oldStudents.Add(new Student("777", "Hanying", "Feng", "Senior", 11));
    oldStudents.Add(new Student("888", "Debra", "Garcia", "Junior", 41));
    oldStudents.Add(new Student("999", "Terry", "Adams", "Senior", 6));
    oldStudents.Add(new Student("211", "Bob", "Stephenson", "Junior", 150));

    return oldStudents;
}
public static List<Student> GetStudentsNew()
{
    List<Student> newStudents = new List<Student>();

    newStudents.Add(new Student("111", "Michael", "Tucker", "Junior", 10));
    newStudents.Add(new Student("222", "Svetlana", "Omelchenko", "Senior", 2));
    newStudents.Add(new Student("333", "Michiko", "Osada", "Senior", 7));
    newStudents.Add(new Student("311", "Sven", "Mortensen", "Freshman", 53));
    newStudents.Add(new Student("444", "Hugo", "Garcia", "Freshman", 16));
    newStudents.Add(new Student("555", "Cesar", "Garcia", "Freshman", 4));
    newStudents.Add(new Student("666", "Fadi", "Fakhouri", "Senior", 72));
    newStudents.Add(new Student("777", "Hanying", "Feng", "Senior", 11));
    newStudents.Add(new Student("888", "Debra", "Garcia", "Junior", 41));
    newStudents.Add(new Student("411", "Lance", "Tucker", "Junior", 60));
    newStudents.Add(new Student("999", "Terry", "Adams", "Senior", 6));

    return newStudents;
}

我正在寻找诸如

的结果
  

Console.WriteLine(“这是添加项列表:”)   Additions(GetStudentsOld(),GetStudentsNew(),添加项);

然后显示它们。

以下是添加项列表:

  

“ 411”,“长矛”,“塔克”,“少年”,60岁

1 个答案:

答案 0 :(得分:1)

方法具有返回类型,您可以通过设置与方法调用的结果相同类型的变量来捕获值:

List<Student> oldStudents = GetStudentsOld();
List<Student> newStudents = GetStudentsNew();

然后,我们可以像其他集合一样使用forforeach循环遍历这两个列表。我们可以先循环浏览新学生列表,然后再为每个新学生循环浏览旧学生列表,然后寻找匹配项,从而在旧学生集合中搜索新学生。

请注意,这是执行此操作的基本方法,但不是最有效的方法。

还请注意,这是假设您已重写Equals方法以对两个学生进行比较。如果您还没有,并且超出了到目前为止所学的知识,则可以比较一些唯一的属性(例如Id)或一组属性(例如FirstName LastName)来确定如果有匹配项。

例如:

List<Student> additions = new List<Student>();

// For each new student
foreach(Student newStudent in newStudents)
{
    bool foundMatch = false;

    // See if there is a match in the old students list
    foreach(Student oldStudent in oldStudents)
    {
        // If we find a match, set our variable and exit this inner foreach loop
        if (newStudent.Equals(oldStudent))
        {
            foundMatch = true;
            break;
        }
    }

    // Now that we're out of that inner loop, see if we found a match
    // If we didn't, capture this student in the additions list
    if (!foundMatch)
    {
        additions.Add(newStudent);
    }
}