IEnumerable在LINQ查询中列出 [英] IEnumerable to list inside linq query
问题描述
我正在尝试运行此查询,在这里我尝试获取类别以及那里的子类别
I am trying to run this query, where I try to fetch categories and there sub categories
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
转换为列表,当然不能.
Currently the field ContinuumCategory.ContinuumSubCategories
is of type List
so this query gives me a compile time error that it cannot convert IEnumerable
to list, ofcourse it cannot.
由于linq无法识别ToList
方法,因此我什至无法在查询中使用该方法.
And as linq does not recognize ToList
method so I cannot even use that inside my query.
我可以通过将ContinuumCategory.ContinuumSubCategories
的类型更改为IEnumerable
来解决我的问题,但是我已经在许多使用List.Add
方法的地方使用了此字段,因此我将不得不将所有Add方法替换为
I can solve my problem by changing the type of ContinuumCategory.ContinuumSubCategories
to IEnumerable
but I have already used this field in many places where it uses List.Add
method so I will have to replace all the Add methods to IEnumerable.Concat
, so this can be tedious.
是否有任何解决方法可直接从linq查询中直接获取连续体子类别列表?
Is there any workaround to directly get the list of continuum sub categories from linq query only?
当我使用此查询时(用于ContinuumSubCategories的查询中使用了ToList()
方法)
when I use this query(used ToList()
method inside query for 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.
推荐答案
不要在内部查询(用于ContinuumSubCategories
的查询)上调用ToList()
Don't call ToList()
on the inner query (the one for ContinuumSubCategories
)
使用ToList()
将查询分为两部分:
Split your query into two parts with ToList()
in-between:
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();
行得通吗?
这篇关于IEnumerable在LINQ查询中列出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!