如下所示,我需要一个输出来显示添加、更改和删除的学生。虽然我已经研究了如何精确地做到这一点,并在RemovedStudents()
中发现了我目前设置的内容。有没有什么例子可以帮助我完成添加、更改和删除学生方法?我会非常感激任何帮助!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
namespace OldandNewStudents
{
class Program
{
static void Main(string[] args)
{
//Display lists of students
Console.WriteLine("Here is the list of old students: ");
ShowStudents(GetStudentsOld());
Console.WriteLine("Here is the list of new students: ");
ShowStudents(GetStudentsNew());
//Show the additions
Console.WriteLine("Here is the list Additions: ");
//Show the changes
Console.WriteLine("Here is the list of Changes: ");
//Show the removed students
Console.WriteLine("Here is the list of removed students: ");
RemovedStudents(GetStudentsNew(), GetStudentsOld());
Console.ReadLine();
}
public static List<Student> GetStudentsOld()
{
List<Student> students = new List<Student>();
students.Add(new Student("111", "Michael", "Tucker", "Junior", 10));
students.Add(new Student("222", "Svetlana", "Omelchenko", "Senior", 2));
students.Add(new Student("333", "Michiko", "Osada", "Senior", 7));
students.Add(new Student("444", "Hugo", "Garcia", "Junior", 16));
students.Add(new Student("555", "Cesar", "Garcia", "Freshman", 4));
students.Add(new Student("666", "Fadi", "Fakhouri", "Senior", 72));
students.Add(new Student("777", "Hanying", "Feng", "Senior", 11));
students.Add(new Student("888", "Debra", "Garcia", "Junior", 41));
students.Add(new Student("999", "Terry", "Adams", "Senior", 6));
students.Add(new Student("211", "Bob", "Stephenson", "Junior", 150));
return students;
}
public static List<Student> GetStudentsNew()
{
List<Student> students = new List<Student>();
students.Add(new Student("111", "Michael", "Tucker", "Junior", 10));
students.Add(new Student("222", "Svetlana", "Omelchenko", "Senior", 2));
students.Add(new Student("333", "Michiko", "Osada", "Senior", 7));
students.Add(new Student("311", "Sven", "Mortensen", "Freshman", 53));
students.Add(new Student("444", "Hugo", "Garcia", "Freshman", 16));
students.Add(new Student("555", "Cesar", "Garcia", "Freshman", 4));
students.Add(new Student("666", "Fadi", "Fakhouri", "Senior", 72));
students.Add(new Student("777", "Hanying", "Feng", "Senior", 11));
students.Add(new Student("888", "Debra", "Garcia", "Junior", 41));
students.Add(new Student("411", "Lance", "Tucker", "Junior", 60));
students.Add(new Student("999", "Terry", "Adams", "Senior", 6));
return students;
}
public static void ShowStudents(List<Student> stuList)
{
Console.WriteLine();
foreach (Student s in stuList)
{
Console.WriteLine(s);
}
Console.WriteLine();
}
public static void RemovedStudents(List<Student> stuNewList, List<Student> stuOldList)
{
List<Student> removedStudents = stuNewList.Except(stuOldList).ToList();
IEnumerable<Student> differenceQuery = stuNewList.Except(stuOldList);
foreach (Student s in differenceQuery)
Console.WriteLine(s);
}
}
}
这是我的新更新的RemovedStudents方法:
public static void RemovedStudents(List<Student> stuNewList, List<Student> stuOldList)
{
GetStudentsOld().Except(GetStudentsNew());
foreach (Student s in stuNewList)
Console.WriteLine("student: {0} {1}", s.FirstName, s.LastName);
}
这里是学生类(Student Class),虽然我没有编辑权限:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace OldandNewStudents
{
public class Student
{
// Sample Student class
// Each student has a first name, a last name, a class year, and a rank
// that indicates academic ranking in the student body.
public string ID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string StudentYear { get; set; }
public int StudentRank { get; set; }
public Student(string idNumber, string firstName, string lastName, string studentYear, int studentRank)
{
ID = idNumber;
FirstName = firstName;
LastName = lastName;
StudentYear = studentYear;
StudentRank = studentRank;
}
public override string ToString()
{
return ID + " " + FirstName + " " + LastName + " " + StudentYear + " " + StudentRank;
}
}
}
这是我的新加法方法,但我不确定为什么会出现重载错误:
public static List<Student> GetActualNewStudents(List<Student> oldStudents, List<Student> newStudents)
{
List<Student> actualNewStudents = new List<Student>();
foreach (var student in oldStudents) {
if (oldStudents[student].ID != newStudents[student].ID)
{
actualNewStudents.Add(newStudents[student]);
}
}
return actualNewStudents;
}
AddedStudents
方法已经起作用,而你只是要求RemovedStudents
的逻辑。你的RemovedStudents
逻辑看起来很好。如果它不起作用,你将不得不查看Student
的代码,并重写Equals
和GetHashCode()
。 - Rob