IEnumerable to list inside linq query



我正在尝试运行此查询,我尝试在其中获取类别并有子类别

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()方法)

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()

将查询拆分为两部分,中间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();

行得通吗?

相关内容

  • 没有找到相关文章

最新更新