return通过 C# 中的 return 方法从列表中的 linq 获取多个 class 模型数据

returning multiple class model data from linq in list via return method in C#

我有 LINQ 输出,我正试图将其传递到列表中,但出现以下错误

在 linq 结果中我试图从两个 class 模型传递数据,如果我做一个 class 模型(listOfCoursesWithoutURL)然后它工作但我需要传递 processedCourseInstance。我创建了两个 classes 的 ModelView 但不确定我在这张图片中缺少什么

视图模型

public class CoursesInstanceStudyLevel_ViewModel
{
    public CourseInstanceModel _CourseInstanceModel { get; set; }
    public StudyLevelModel  _StudyLevelModel { get; set; }
}

我的Class

 public List<CoursesInstanceStudyLevel_ViewModel> ProcessAllCoursesApplicationURL(CourseApplicationsURLFeed_Model _obj)
    {

   using(var _uof = new Courses_UnitOfWork())
    {
    _uof.CourseInstances_Repository.GetAll();

     var _listOfCoursesWithoutURL = (from b in ListOfCoursesInstances 
                                           where b.ApplicationURL == null
                                           select b).ToList();


    var processedCourseInstance = (from _courseInstances in _uof.CourseInstances_Repository.GetAll()
                                         join _courses in _uof.Courses_Repository.GetAll() on _courseInstances.CourseID equals _courses.CourseID 
                                         join _studylevel in _uof.StudyLevel_Repository.GetAll() on _courses.StudyLevelId equals _studylevel.StudyLevelID
                                         orderby _courseInstances.CourseCode
                                         select new { _courseInstances, _studylevel }).ToList();


  return processedCourseInstance;     // it doesn't work ... refer to screen shot
  // return _listOfCoursesWithoutURL //it works 
 }
}

错误

此处:

select new { _courseInstances, _studylevel })

您正在定义一个匿名对象。您已经准备好类型,所以使用那个:

select new CoursesInstanceStudyLevel_ViewModel 
{ 
      _CourseInstanceModel = _courseInstances, 
      _StudyLevelModel = _studylevel 

}

假设 CourseInstanceModelStudyLevelModel 是正确的类型

在以下代码段中突出显示的行表示您选择的是匿名对象而不是具体对象 CourseIntaceStudyLeve_ViewModel

select new { _courseInstances, _studylevel }

您必须将查询更改为以下...

var processedCourseInstance = (from _courseInstances in _uof.CourseInstances_Repository.GetAll()
                                             join _courses in _uof.Courses_Repository.GetAll() on _courseInstances.CourseID equals _courses.CourseID 
                                             join _studylevel in _uof.StudyLevel_Repository.GetAll() on _courses.StudyLevelId equals _studylevel.StudyLevelID
                                             orderby _courseInstances.CourseCode
                                             select new CoursesInstanceStudyLevel_ViewModel(){
     _CourseInstanceModel  = _courseInstances.FirstOrDefault(),
     StudyLevelModel  = _studylevel.FirstOrDefault()}).ToList();

根据您的视图模型定义,我假设您只需要第一门课程和第一门学习级别,并在那里应用 FirstOrDefault。您可以选择接受或更改您的视图模型定义。

这是我的答案,它有效

 var processedCourseInstance = 
   (from _courseInstances in _uof.CourseInstances_Repository.GetAll()
             join _courses in _uof.Courses_Repository.GetAll() on _courseInstances.CourseID equals _courses.CourseID 
             join _studylevel in _uof.StudyLevel_Repository.GetAll() on _courses.StudyLevelId equals _studylevel.StudyLevelID
             orderby _courseInstances.CourseCode
                select new CoursesInstanceStudyLevel_ViewModel() {
                          _CourseInstanceModel = _courseInstances, 
                          _StudyLevelModel  = _studylevel 
                       }).ToList();