このクエリを実行しようとしています。ここで、カテゴリとサブカテゴリを取得しようとしています
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いるため、すべての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.