C# 将repo中的所有整数类型获取到现有对象的列表中

C# 将repo中的所有整数类型获取到现有对象的列表中,c#,C#,我正试图找到一种方法来循环遍历一个存储库,该存储库将获得所有CourseId值。到目前为止,我只能得到找到的第一个值,但不能将它们全部作为列表。我怎样才能得到所有的值作为一个列表,这是我应该循环的地方吗 以下是我迄今为止所做的工作 StudentVM studentVm = new StudentVM(); studentVm.Student = StudentRepository.Get(id); //Loop here?

我正试图找到一种方法来循环遍历一个存储库,该存储库将获得所有
CourseId
值。到目前为止,我只能得到找到的第一个值,但不能将它们全部作为列表。我怎样才能得到所有的值作为一个列表,这是我应该循环的地方吗

以下是我迄今为止所做的工作

StudentVM studentVm = new StudentVM();
            studentVm.Student = StudentRepository.Get(id);
            //Loop here?
            studentVm.SelectedCourseIds = new List<int>() {
                studentVm.Student.Courses.Select(x => x.CourseId).FirstOrDefault()
            };
            studentVm.SetCourseItems(CourseRepository.GetAll());
            studentVm.SetMajorItems(MajorRepository.GetAll());
            studentVm.SetStateItems(StateRepository.GetAll());
            return View(studentVm);
StudentVM StudentVM=newstudentvm();
studentVm.Student=StudentRepository.Get(id);
//在这里循环?
studentVm.SelectedCourseIds=新列表(){
studentVm.Student.Courses.Select(x=>x.CourseId).FirstOrDefault()
};
studentVm.SetCourseItems(CourseRepository.GetAll());
studentVm.SetMajorItems(MajorRepository.GetAll());
SetStateItems(StateRepository.GetAll());
返回视图(studentVm);

您可以使用
ToList()
而不是
FirstOrDefault()
。并删除
new List
部分,因为
ToList()
将已返回列表:

studentVm.SelectedCourseIds = studentVm.Student.Courses.Select(x => x.CourseId).ToList();

FirstOrDefault()
替换为
ToList()
?它说它无法将“System.Collections.Generic.List”转换为intIt。我花了一段时间才弄明白这一点。