C#如何将一个对象从一个列表添加到另一个

时间:2018-06-22 15:26:13

标签: c# list object

我的问题是将特定学生添加到已添加到 清单。这些学生在“学生”和“当前列表”课程中。 这些课程位于“课程”和“列表课程”中。 其他一切都很好。这只是一个基本的控制台应用程序。 如果我使用的是SQL,那么我不会遇到这些问题,而是尝试学习C#。

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApp1
{
    public class Course
    {
        public string courseName { get; set; }
        public string teacherFirst { get; set; }
        public string teacherLast { get; set; }
        public List<Student> studentList;

        public static int courseID = 0;

        public static List<Course> Courses = new List<Course>();

        public Course(int ID, string name, string first, string last, List<Student> newStudentList)
        {
            ID = courseID;
            courseName = name;
            teacherFirst = first;
            teacherLast = last;
            studentList = newStudentList;
        }

        public static void addCourse()
        {
            courseID++;

            Console.Write("\nPlease enter the name of the course: "); // grabbing input from user.
            string name = Console.ReadLine();

            Console.Write("Please enter the teacher's first name: "); // grabbing input from user.
            string first = Console.ReadLine();

            Console.Write("Please enter the teacher's last name: "); // grabbing input from user.
            string last = Console.ReadLine();

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

            Course added = new Course(courseID, name, first, last, newStudentList);
            Courses.Add(added);
        }

        public static void printCourse()
        {
            foreach (var currentDirectory in Courses)
            {
                Console.WriteLine("\nCourse ID: {0}", courseID);
                Console.WriteLine("Course Name: {0}", currentDirectory.courseName);
                Console.WriteLine("Teacher's First Name: {0}", currentDirectory.teacherFirst);
                Console.WriteLine("Teacher's Last Name:  {0}\n", currentDirectory.teacherLast);
            }
        }

        public static void addStudentToClass()
        {
            Console.Write("Please enter the student's ID: ");
            string studentInput = Console.ReadLine();
            int studentID = Convert.ToInt32(studentInput);
            int studentIndex = Student.current.FindIndex(Student => Student.ID == (studentID));
            Console.WriteLine($"Student is located at index: {studentIndex} in the student list");

            Console.Write("Please enter the course ID: ");
            string courseInput = Console.ReadLine();
            int courseID = Convert.ToInt32(courseInput);
            int courseIndex = Course.Courses.FindIndex(Course => Course.courseID == (courseID));
            Console.WriteLine($"Course is located at index: {courseIndex} in the course list");

            // Trying to add a student from the current list in student to the Courses list in Course.
        }
    }
}

2 个答案:

答案 0 :(得分:2)

看看LINQ。这将帮助您轻松使用集合中的对象。例如

var course = Courses.FirstOrDefault(el => el.ID == CourseID);
var student = Students.FirstOrDefault(el => el.ID == studentID);
course.studentList.Add(student);

答案 1 :(得分:0)

如果您将以下内容添加到addStudentToClass

Courses[courseIndex].studentList.Add(Students.current[studentIndex]);

那应该把您当前的代码带到需要的地方。您可能还是希望采用LINQ方法,因为它显然更紧凑。

或者,如果您想稍微调整设计,则可以将“学生”和“课程”收藏集更改为“词典”。这样,您的课程ID和学生ID可以直接用于访问特定的课程/学生。

Dictionary<int, Course> Courses = new Dictionary<int, Course>();
...
addStudentToClass()
{
    Console.Write("Please enter the student's ID: ");
    string studentInput = Console.ReadLine();
    int studentID = Convert.ToInt32(studentInput);

    Console.Write("Please enter the course ID: ");
    string courseInput = Console.ReadLine();
    int courseID = Convert.ToInt32(courseInput);

    Courses[courseID].studentList.Add(Students.current[studentID]);
}