0
嗨,我剛剛發現我的web應用程序很慢可能是因爲linq。用linq查詢編譯
我有點迷失於這些編譯查詢,你能幫我編譯一個查詢,並仍然使它可用來查詢查詢嗎? (是可以理解的:P) 例如此查詢(在vb.net):
Dim query = (From p In db.ProductCategories _
Group Join t In db.Translate_ProductCategories On p.ID_Category Equals t.Category_ID Into res = Group From r1 In res.DefaultIfEmpty _
Where r1.Language_ID = langID And p.CategoryActive = True _
Select New With {.name = r1.Name, .idcat = p.ID_Category, .level = p.CategoryLevel, .index = p.CategoryIndex, .parentID = p.CategoryParent_ID})
然後我想一定要還是能做出這樣的事情:
Dim level0 = (From l In query Where l.level = 0 Order By l.index Ascending Select l)
謝謝爲幫助
編輯:
我試着這樣做:
Dim myquery = CompiledQuery.Compile(_
Function(db As EshopDataContext) _
(From p In db.ProductCategories _
Group Join t In db.Translate_ProductCategories On p.ID_Category Equals t.Category_ID Into res = Group From r1 In res.DefaultIfEmpty _
Where r1.Language_ID = langID And p.CategoryActive = True _
Select New With {.name = r1.Name, .idcat = p.ID_Category, .level = p.CategoryLevel, .index = p.CategoryIndex, .parentID = p.CategoryParent_ID}))
Dim query = myquery.Invoke(db)
Dim level0 = (From l In query Where l.level = 0 Order By l.index Ascending Select l)
和我有錯誤「查詢結果不能被枚舉超過一次。」在這條線上
cptCat1 = level1.Where(Function(l1) l1.parentID = parentId1).Count
是什麼讓你認爲這是因爲LINQ的慢?什麼是你的數據源(數據庫,對象集合,其他?) – BlackICE
因爲這個:http://peterkellner.net/2009/05/06/linq-to-sql-slow-performance-compilequery-critical/: ) – blop
您是否嘗試使用該文章中的語法來編譯?你不能得出結論,你查詢是因爲編譯查詢很慢,直到你編譯和比較。這是一個你需要執行多次的查詢 - 如果不編譯將無濟於事。使用日誌從您的LINQ獲取TSQL,並在SSMS中顯示預計的執行計劃。 – Paparazzi