0

嗨,我刚刚发现我的 Web 应用程序很慢,可能是因为 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
4

1 回答 1

1

首先阅读:http: //www.codinghorror.com/blog/2010/03/compiled-or-bust.html

如果您真的认为在阅读后必须这样做,这里是 MS 的关于已编译查询的 How-To。

http://msdn.microsoft.com/en-us/library/bb399335.aspx#Y0(用于 linq2sql)

http://msdn.microsoft.com/en-us/library/bb896297.aspx#Y300(用于 linq2EF)

于 2012-09-07T14:34:52.597 回答