通过C#中的return方法从列表中的linq返回多个类模型数据

时间:2015-01-20 14:31:26

标签: c# asp.net-mvc linq entity-framework

我有LINQ输出,我试图在列表中传递,但我收到以下错误 enter image description here

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

视图模型

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

我的班级

 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 
 }
}

错误

enter image description here

3 个答案:

答案 0 :(得分:2)

这里:

select new { _courseInstances, _studylevel })

您正在定义一个匿名对象。您已准备好类型,因此请使用以下类型:

select new CoursesInstanceStudyLevel_ViewModel 
{ 
      _CourseInstanceModel = _courseInstances, 
      _StudyLevelModel = _studylevel 

}

假设CourseInstanceModelStudyLevelModel是正确的类型

答案 1 :(得分:1)

使用以下代码段中的突出显示行,您将选择一个匿名对象而不是具体的CourseIntaceStudyLeve_ViewModel

  

选择新的{_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。您可以选择同步或更改视图模型定义。

答案 2 :(得分:0)

这是我的答案,它有效

 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();