2010-12-06 56 views
1

我有下面的代碼:LINQ編譯的查詢錯誤「parameteres不能序列」

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

我得到一個錯誤「parameteres不能序列」當我運行filteredFunc,這是不可思議的,因爲「章節」對象是IQueryable,而不是IEnumerable,那麼爲什麼我會得到錯誤?

下面的代碼工作正常,但它對我沒有好處。

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

這是你想要編譯的部分?看起來您正在查詢數據庫來填充`章節',然後嘗試將後面的結果傳遞給數據庫以使用不同的結果。你知道數據庫讀取的是哪一行嗎?我懷疑這是`var chapters`行,這不是你的意思。我只使用CompiledQueries從一個從DataContext獲取的集合開始,而不是使用傳入的集合。 – Rup 2010-12-06 13:38:40

+0

嘗試使用toList函數從您的IQueriable創建IEnumerable。要知道它會通過數據庫查詢 – ASpirin 2010-12-06 13:42:04

回答

1

您不能像這樣使用IEnumerable編譯查詢。枚舉中的項目數量可能會有所不同,因此查詢的查詢計劃將根據其大小而有所不同。只需刪除已編譯的查詢並按原樣使用該函數。