【问题标题】:linq compiled query error "parameteres cannot be sequences"linq 编译查询错误“参数不能是序列”
【发布时间】:2010-12-06 13:31:54
【问题描述】:

我有下面的代码:

var catRoots = CatalogContext.CatalogRoots.Where(cr => cr.Visible);
var catChapter = CatalogContext.CatalogChapters.Where(cch => cch.Visible);
var catThemes = CatalogContext.CatalogThemes.Where(cth => cth.Visible);
var catCompanies = CatalogContext.CatalogCompanies.Where(cc => cc.Visible);
var catRelations = CatalogContext.CatalogCompanyThemeRelations.Where(cctr => cctr.Visible && cctr.OwnerVisible);
var regions = CatalogContext.Regions.AsQueryable();

var compChapters = catRelations.Where(cctr => cctr.Location == CatalogCompanyLocations.Chapter)
            .Join(catChapter, cctr => cctr.ParentID, cch => cch.ID, (cctr, cch) => new { Relation = cctr, Chapter = cch })
            .Join(catRoots, cch => cch.Chapter.CatalogRootID, cr => cr.ID, (cch, cr) => new { CatalogRoot = cr, CatalogChapter = cch.Chapter, CatalogRelation = cch.Relation })
            .Join(catCompanies, cr => cr.CatalogRelation.CompanyID, cc => cc.ID, (cr, cc) => new { Root = cr.CatalogRoot, Chapter = cr.CatalogChapter, Theme = default(CatalogTheme), Company = cc })
            .Join(regions, cc => cc.Company.RegionID, r => r.ID, (cc, r) => new { Root = cc.Root, Chapter = cc.Chapter, Theme = cc.Theme, Company = cc })
            .GroupBy(gr => new { Chapter = gr.Chapter, Name = gr.Root.Name, ID = gr.Root.ID, Icon = gr.Root.Icon, Rewrite = gr.Root.Rewrite, Sort = gr.Root.Sort })
            .Select(gr => new { Chapter = gr.Key.Chapter, ID = gr.Key.ID, Name = gr.Key.Name, Icon = gr.Key.Icon, Rewrite = gr.Key.Rewrite, Sort = gr.Key.Sort, Count = gr.Count() });

var compThemes = catRelations.Where(cctr => cctr.Location == CatalogCompanyLocations.Theme)
            .Join(catThemes, cctr => cctr.ParentID, cth => cth.ID, (cctr, cth) => new { Relation = cctr, Theme = cth })
            .Join(catChapter, cth => cth.Theme.CatalogChapterID, cch => cch.ID, (cth, cch) => new { Relation = cth.Relation, Theme = cth.Theme, Chapter = cch })
            .Join(catRoots, cch => cch.Chapter.CatalogRootID, cr => cr.ID, (cch, cr) => new { Relation = cch.Relation, Theme = cch.Theme, Chapter = cch.Chapter, Root = cr })
            .Join(catCompanies, cr => cr.Relation.CompanyID, cc => cc.ID, (cr, cc) => new { Root = cr.Root, Chapter = cr.Chapter, Theme = cr.Theme, Company = cc })
            .Join(regions, cc => cc.Company.RegionID, r => r.ID, (cc, r) => new { Root = cc.Root, Chapter = cc.Chapter, Theme = cc.Theme, Company = cc.Company })
            .GroupBy(gr => new { Chapter = gr.Chapter, Name = gr.Root.Name, ID = gr.Root.ID, Icon = gr.Root.Icon, Rewrite = gr.Root.Rewrite, Sort = gr.Root.Sort })
            .Select(gr => new { Chapter = gr.Key.Chapter, ID = gr.Key.ID, Name = gr.Key.Name, Icon = gr.Key.Icon, Rewrite = gr.Key.Rewrite, Sort = gr.Key.Sort, Count = gr.Count() });

var source = compChapters.Union(compThemes);

var chapters = source.Select(r => new { Chapter = r.Chapter, Count = r.Count }).Cast<object>().Distinct();

public static Func<DataContext, IQueryable<object>, IEnumerable<object>> filteredFunc = 
CompiledQuery.Compile<DataContext, IQueryable<object>, IEnumerable<object>>
(
      (DataContext db, IQueryable<object> q) => q.Distinct().ToList()
);

filtredChapters = filteredFunc(CatalogContext, chapters);

我在运行filteredFunc 时收到错误“参数不能是序列”,这很奇怪,因为“章节”对象是IQueryable,而不是IEnumerable,那么为什么我会收到错误消息?

下面的代码可以正常工作,但对我不好。

filtredChapters = chapters.Distinct().Cast<object>().ToList();

【问题讨论】:

  • 你想编译这其中的哪一部分?看起来您正在查询数据库以填充 chapters,然后尝试将结果传递回数据库以使用 distinct。你知道数据库读取实际发生在哪一行吗?我怀疑这是var chapters 行,这不是你的意思。我只使用了 CompiledQueries 从从 DataContext 中获取的集合开始,而不是使用传入的集合。
  • 尝试使用 toList 函数从您的 IQueriable 创建 IEnumerable。请注意它将通过数据库查询
  • Rup,有一系列相互连接的查询,我用这些查询更新了我的帖子。看起来你是对的,不应该将查询传递给 CompiledQuery,无论如何我认为它会减少只评估一个查询的时间,而不是所有查询,但奇怪的是我们不能传递查询。将重新考虑我的查询。
  • ASpirin,这将花费大量时间来评估对 IEnumerable 的查询,因为此查询有超过 40 000 行数据,并且每次页面加载大约需要 4 秒来评估。这就是我尝试实现 CompiledQuery 的原因。

标签: c# linq-to-sql


【解决方案1】:

您不能像这样使用带有 IEnumerable 的编译查询。枚举中的项目数可能会有所不同,因此查询的查询计划将根据其大小而有所不同。只需删除已编译的查询并按原样使用该函数即可。

【讨论】:

    猜你喜欢
    • 2010-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多