要在linq查询中列出的IEnumerable
我正在尝试运行这个查询,在这里我尝试获取类别,并且没有子类别要在linq查询中列出的IEnumerable,linq,ienumerable,Linq,Ienumerable,我正在尝试运行这个查询,在这里我尝试获取类别,并且没有子类别 studentCont.ContinuumCategories = db.continuumcategorymasters .Where(x => x.AssessmentId == assessmentId && x.ContinuumCategory != "other") .Select(x => new ContinuumCategory() { Assess
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
转换为List,当然不能
由于linq不识别ToList
方法,所以我甚至不能在查询中使用它
我可以通过将ContinuumCategory.ContinuumSubCategories
的类型更改为IEnumerable
来解决我的问题,但是我已经在许多地方使用了这个字段,它使用了List.Add
方法,因此我必须将所有Add方法替换为IEnumerable.Concat
,因此这可能会很乏味
是否存在仅从linq查询直接获取连续子类别列表的解决方法
编辑:
当我使用此查询时(在连续子类别的查询中使用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.
不要对内部查询调用ToList()
(用于ContinuumSubCategories
的查询)
使用ToList()
将查询分为两部分:
studentCont.ContinuumCategories = db.continuumcategorymasters
.Where(x => x.AssessmentId == assessmentId && x.ContinuumCategory != "other")
.ToList() // Fetch all the data from the db
.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不识别ToList方法,所以我甚至不能在我的查询中使用该方法”为什么,你会遇到异常?@TimSchmelter-我已经编辑了问题,并提供了更多信息,请检查。抛出此异常“已经有一个与此连接关联的打开的DataReader,必须先关闭它。”:(我正要发布ToList()成功的评论,并看到了您的评论:)
studentCont.ContinuumCategories = db.continuumcategorymasters
.Where(x => x.AssessmentId == assessmentId && x.ContinuumCategory != "other")
.ToList() // Fetch all the data from the db
.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();