我有一个需求,我需要使用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.
下面请询问细节:
我有两个表院校和课程,以下列: 学院:ID,姓名,联系方式,城市,地址
五言:ID,CollegeID,名称,年份
我的项目对他们有两种查看模式,如如下:
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; } }
在这里,我已经准备好,但我得到了错误的查询查询:
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();
如果去掉'ToList()'调用的课程,会发生什么? – elolos 2014-10-10 11:15:44
我得到这个错误:“不能隐式转换类型'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