2011-03-08 93 views
1

任何方式使這更高效?有什麼辦法可以提高效率?它運行速度很快,只是想改善,如果可用

internal Func<enntities, IQueryable<CategoryList>> GetCategoryListWithPostingCount = 
     CompiledQuery.Compile((entities entities) => 
      from c in entities.Categories.Include("Postings_Categories") 
      where c.ParentCategoryID == null 
      orderby c.DisplayOrder 
      select new CategoryList 
      { 
       ParentCategoryName = c.CategoryName, 
       ParentCategoryID = c.CategoryID, 
       SubCategories = (
       from s in entities.Categories 
       where s.ParentCategoryID == c.CategoryID 
       select new SubCategoryList 
       { 
        PostingCount = s.Postings_Categories.Count, 
        SubCategoryName = s.CategoryName, 
        SubCategoryID = s.CategoryID 
       }) 
      }); 
+0

它產生什麼SQL?你看到什麼查詢計劃?你有什麼指數?在這個細節層面上,這個問題很難回答。 – spender 2011-03-08 00:15:58

+1

如果它已經運行得很快,那麼我認爲這可能是[過早優化](http://en.wikipedia.org/wiki/Program_optimization#When_to_optimize)的例子。 – srgerg 2011-03-08 00:17:04

回答

1

任何改進建議都取決於我們知道的不僅僅是LINQ查詢。例如,如果每個類別有很多SubCategoryLists,並且Category有大量標量數據(大描述文本等),則可以更快地在單獨的數據庫往返中提取類別列表。但它可能不會。

看看連接了哪些表格並添加適當的索引也會產生影響。

但總而言之,我會說這是一個過早優化的情況。代碼看起來很乾淨,我可以告訴你想用它做什麼,所以現在就調用它。如果你發現它在你的程序中是一個慢點,那麼就擔心它。

PS - 問題是標籤的LINQ到SQL,但是這看起來像LINQ到實體,我...

+0

我對帖子傢伙的錯誤標籤表示歉意。我以爲我把它放在實體中。 – CrazyCoderz 2011-03-08 12:43:34

相關問題