【问题标题】:IEnumerable to list inside linq queryIEnumerable 在 linq 查询中列出
【发布时间】:2013-01-29 08:12:48
【问题描述】:

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

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 方法的地方使用了此字段,因此我必须将所有添加方法替换为@987654329 @,所以这可能很乏味。

是否有任何解决方法可以仅从 linq 查询中直接获取连续子类别列表?

编辑:

当我使用此查询时(在 ContinuumSubCategories 的查询中使用 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.

【问题讨论】:

  • “因为 linq 无法识别 ToList 方法,所以我什至无法在我的查询中使用它” 为什么,你有异常吗?
  • @TimSchmelter - 我已经用更多信息编辑了问题,请检查。

标签: linq ienumerable


【解决方案1】:

不要在内部查询中调用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();

有效吗?

【讨论】:

  • 抛出此异常“已经有一个打开的 DataReader 与此 Connection 关联,必须先关闭。” :(
  • 我正要发评论说 ToList() 成功了,看到你的评论:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-06-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多