【发布时间】:2013-01-29 08:12:48
【问题描述】:
我正在尝试运行此查询,我尝试在其中获取类别和子类别
studentCont.ContinuumCategories = db.continuumcategorymasters
.Where(x => x.AssessmentId == assessmentId && x.ContinuumCategory != "other")
.Select(x => new ContinuumCategory()
{
AssessmentId = x.AssessmentId,
ContinuumCategoryId = x.ContinuumCategoryId,
NativeAppid = x.NativeAppCategoryId,
score = 0,
ContinuumCategoryName = x.ContinuumCategory,
ContinuumSubCategories = x.continuumsubcategorymasters
.Select(csc => new ContinuumSubCategory
{
ContinuumSubCategoryId = csc.ContinuumSubCategotyId,
ContinuumSubCategoryName = csc.ContinuumSubCategotyName,
NativeAppsubid = csc.NativAppSubCategotyId
})
})
.ToList();
目前ContinuumCategory.ContinuumSubCategories 字段的类型为List,所以这个查询给我一个编译时错误,它不能将IEnumerable 转换为列表,当然不能。
由于 linq 无法识别 ToList 方法,所以我什至无法在查询中使用它。
我可以通过将ContinuumCategory.ContinuumSubCategories 的类型更改为IEnumerable 来解决我的问题,但是我已经在许多使用List.Add 方法的地方使用了此字段,因此我必须将所有添加方法替换为@987654329 @,所以这可能很乏味。
是否有任何解决方法可以仅从 linq 查询中直接获取连续子类别列表?
编辑:
当我使用此查询时(在 ContinuumSubCategories 的查询中使用 ToList() 方法)
studentCont.ContinuumCategories = db.continuumcategorymasters
.Where(x => x.AssessmentId == assessmentId && x.ContinuumCategory != "other")
.Select(x => new ContinuumCategory()
{
AssessmentId = x.AssessmentId,
ContinuumCategoryId = x.ContinuumCategoryId,
NativeAppid = x.NativeAppCategoryId,
score = 0,
ContinuumCategoryName = x.ContinuumCategory,
ContinuumSubCategories = x.continuumsubcategorymasters
.Select(csc => new ContinuumSubCategory
{
ContinuumSubCategoryId = csc.ContinuumSubCategotyId,
ContinuumSubCategoryName = csc.ContinuumSubCategotyName,
NativeAppsubid = csc.NativAppSubCategotyId
}).ToList()
})
.ToList();
我得到了这个异常
LINQ to Entities does not recognize the method 'System.Collections.Generic.List1[BusinessObjects.ContinuumSubCategory] ToList[ContinuumSubCategory]
(System.Collections.Generic.IEnumerable1[BusinessObjects.ContinuumSubCategory])' method, and this method cannot be translated into a store expression.
【问题讨论】:
-
“因为 linq 无法识别 ToList 方法,所以我什至无法在我的查询中使用它” 为什么,你有异常吗?
-
@TimSchmelter - 我已经用更多信息编辑了问题,请检查。
标签: linq ienumerable