Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
要在linq查询中列出的IEnumerable_Linq_Ienumerable - Fatal编程技术网

要在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();