获取Model1的列表,其中包含Model2的列表LINQ-MVC 4 EntityFramework 5

本文关键字:列表 LINQ-MVC EntityFramework Model2 Model1 获取 包含 | 更新日期: 2023-09-27 18:26:06

我有一个需求,我需要使用Linq获得Model1(List)的列表,Model1中有Model2(List)列表,我也需要获取它。为此,我创建了一个Linq,但我得到了以下错误:

LINQ to Entities无法识别该方法'System.Collections.Generic.List 1 [OurCourse] ToList[OurCourse](System.Collections.Generic.IEnumerable 1[OurCourse])的方法,而此方法无法转换为存储表达式。

请参阅以下详细信息:

  1. 我有两张表格"学院和课程",列如下:学院:身份证,姓名,联系人,城市,地址

    Cource:ID,学院ID,姓名,年份

  2. 我的项目有两个视图模型,如下所示:

    public class OurCollege
    {
        public string Name { get; set; }
        public string Contact { get; set; }
        public List<OurCourse> MyCourses { get; set; }
    }
    public class OurCourse
    {
        public string Name { get; set; }
        public int NumberOfYears { get; set; }
    }
    
  3. 这里是我准备的查询查询,但我得到了错误:

    var colleges = db.Colleges
                     .Select(cld => new OurCollege()
                      {
                          Name = cld.Name,
                          Contact = cld.Contact,
                          MyCourses = cld.Course
                                         .Select(crs => new OurCourse()
                                         {
                                             Name = crs.Name,
                                             NumberOfYears = crs.Years
                                         }).ToList()
                       }).ToList();
    

获取Model1的列表,其中包含Model2的列表LINQ-MVC 4 EntityFramework 5

这样做怎么样:

MyCourses = from crs in cld.Course
            select new OurCourse
            {
               Name = crs.Name,
               NumberOfYears = crs.Years
            }      

您的完整查询现在将显示:

var colleges = db.Colleges
                 .Select(cld => new OurCollege()
                  {
                      Name = cld.Name,
                      Contact = cld.Contact,
                      MyCourses = (from crs in cld.Course
                                  select new OurCourse
                                  {
                                    Name = crs.Name,
                                    NumberOfYears = crs.Years
                                  }).ToList()    
                   }).ToList();   

实际上,LINQ to Entities将您的查询转换为SQL。它不知道如何在SQL中转换ToList()

另一种选择是将List<T>更改为IEnumerable<T>,并从原始代码中删除ToList()

public IEnumerable<OurCourse> MyCourses { get; set; }

并且在查询中:

var colleges = db.Colleges
                 .Select(cld => new OurCollege()
                  {
                      Name = cld.Name,
                      Contact = cld.Contact,
                      MyCourses = cld.Course
                                     .Select(crs => new OurCourse()
                                     {
                                         Name = crs.Name,
                                         NumberOfYears = crs.Years
                                     })
                   }).ToList();

有关更多详细信息,请访问嵌套类型的实体框架ToList()(LINQ to Entities不识别该方法)

foreach循环将起作用,您可以编写类似的查询

var colleges =   db.Colleges
                   .Select(x => new OurCollege()
                   {
                        CollegeId = x.CollegeId,
                        Name = x.Name,
                        Contact = x.Contact
                   }).ToList();
foreach (var college in colleges)
{
        college.MyCourse =  db.Course.Where(x => x.CollegeId == college.CollegeId)
                              .Select(x => new OurCourse()
                              {
                                 Name = x.Name,
                                 NumberOfYears = x.Years
                              }).ToList()
}