我正在尝试运行此查询,我尝试在其中获取类别和子类别
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
我可以通过更改 to 的类型来解决我的问题,IEnumerable
但是我已经在许多使用方法的地方使用了这个字段,List.Add
所以我必须将所有 Add 方法替换为IEnumerable.Concat
,所以这可能很乏味。
是否有任何解决方法可以仅从 linq 查询中直接获取连续子类别列表?
编辑:
当我使用此查询时(ToList()
ContinuumSubCategories 查询中使用的方法)
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.