2014-10-10 48 views
0

我有一个需求,我需要使用Linq获取Model1(List)的列表,Model1中有Model2(List)列表和我也需要取回。为此,我创建了一个LINQ但m到处以下错误:获取Model1的列表,其中包含Model2的列表LINQ - MVC 4 EntityFramework 5

LINQ to Entities does not recognize the method 'System.Collections.Generic.List 1 [OurCourse] ToList[OurCourse](System.Collections.Generic.IEnumerable 1 [OurCourse])' method, and this method cannot be translated into a store expression.

下面请询问细节:

  1. 我有两个表院校和课程,以下列: 学院:ID,姓名,联系方式,城市,地址

    五言:ID,CollegeID,名称,年份

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

如果去掉'ToList()'调用的课程,会发生什么? – elolos 2014-10-10 11:15:44

+0

我得到这个错误:“不能隐式转换类型'System.Collection.Genetic.IEnumrable '到'不能隐式转换类型'System.Collection.Genetic.List '。存在一个明确的对话(你是否缺少一个强制转换)” 当我做“MyCourses =(列表)cld.Course ....”然后我得到这个错误:“无法强制转换类型'System.Collections.Generic.IEnumerable'1'键入'System.Collections .Generic.List'1'。LINQ to Entities只支持投射EDM原始类型或枚举类型。“ – NMathur 2014-10-10 11:25:20

回答

0

如何做这样的:

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到实体转换查询到SQL.It不知道怎么翻译ToList()SQL

另一种是List<T>改变你IEnumerable<T>和删除ToList() FRIM你原来的代码:

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

欲了解更多详情,请访问该Entity Framework ToList() in nested type (LINQ to Entities does not recognize the method)

+0

尝试此,获取“无法隐式转换类型'System.Collection.Genetic.IEnumrable '到'不能隐式转换类型'System.Collection.Genetic.List '。存在一个明确的会话(你是否缺少演员)” – NMathur 2014-10-10 11:26:58

+0

请参阅编辑忘记转换为列表 – 2014-10-10 11:27:40

+0

也尝试过:(在我的问题中提到的错误..我明白了。 ..我指的是你分享的链接 – NMathur 2014-10-10 11:46:10

0

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