正如你可以看到下面我需要有一个输出显示添加,更改,和删除学生。我认为我已经研究了如何准确地做到这一点,并不断寻找我有什么设置目前在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);
}
下面是学生类,但不允许我编辑它:
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;
}
}
}
下面是我的NEW加法方法,但我不确定为什么会出现重载错误:
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;
}
3条答案
按热度按时间fumotvh31#
你可以用普通学生创建第三个数组,然后对于所有不在那里的学生,考虑在new students数组中的一个新学生,当然:
编辑:
3yhwsihp2#
这个方法奏效了,并得到了我需要的结果:
添加代码:
已删除学生的代码:
变更代码:
jdzmm42g3#
使用两个列表的问题在于比较,例如Except扩展方法将使用
IEquatable
的默认实现。您有一个构造函数,但不包括类定义。如果字段和属性 * 仅 * 包含值类型,然后应用逐字节比较。如果你有任何引用类型,那么你需要覆盖默认的Equals方法。如果你只有值类型字段,那么下面的代码应该可以工作。增加:
删除:
差异:
我应该注意到
Console.WriteLine
不会像您期望的那样工作,因为您传入的是引用类型,而不是将被转换为您期望的字符串的类型。