我可以使用已编译的查询作为第二个查询的源吗?

发布于 2024-10-31 06:38:06 字数 782 浏览 1 评论 0原文

我有一个编译好的查询,效果很好。我向它传递了一个product_id,它返回该产品的产品评论信息。

是否可以使用此编译查询作为子查询的源?示例:

from cat in ctx.cat_table 
join prod in ctx.prod_table on cat.category_id equals prod.category_id
select new
{
    cat_id = cat.category_id,
    prod_id = prod.product_id,
    name = prod.product_name,
    descript = prod.product_description,
    price = prod.price,
    reviews = (from mcq in mycompiledquery(ctx, prod.product_id)
               select new 
               {
                   rating = mcq.review_rating,
                   review = mcq.review_text
               }    
}

我早期尝试做这样的事情会引发错误:

LINQ to Entities 不支持 LINQ 表达式节点类型“Invoke”

我考虑过的一种替代方案是用 SQL 视图替换已编译的查询,但我担心性能会受到负面影响。

非常感谢您提供的任何建议。

I have a compiled query that works great. I pass it a product_id and it returns the product review information for that product.

Is it possible to use this compiled query as a source for a sub-query? Example:

from cat in ctx.cat_table 
join prod in ctx.prod_table on cat.category_id equals prod.category_id
select new
{
    cat_id = cat.category_id,
    prod_id = prod.product_id,
    name = prod.product_name,
    descript = prod.product_description,
    price = prod.price,
    reviews = (from mcq in mycompiledquery(ctx, prod.product_id)
               select new 
               {
                   rating = mcq.review_rating,
                   review = mcq.review_text
               }    
}

My early attempts at doing something like this raises an error:

The LINQ expression node type 'Invoke' is not supported in LINQ to Entities

One alternative I've thought about is to replace my compiled query with a SQL view, but I'm concerned about a negative performance hit.

Many thanks for any suggestions you can offer.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

美煞众生 2024-11-07 06:38:06

您可以在其他查​​询中使用编译的查询,但不能使其依赖于外部查询。示例

// You can do 
var someParams = 10;
var dataQuery = from x in ctx.SomeData
                join y in myCompiledQuery.Invoke(ctx, someParams) 
                    on x.Id equals y.Id
                where x.Name = "ABC"
                select new { x, y };

// You can't do - this example will not compile but let's use it for description
var dataQuery = from x in ctx.SomeData
                join y in myCompiledQuery.Invoke(ctx, x.SomeParams) 
                    on x.Id equals y.Id
                where x.Name = "ABC"
                select new { x, y };

不同之处在于第一个示例仅执行委托(编译后的查询是委托)并返回IQueryable。第二个示例无法执行委托,因为它依赖于外部查询数据,因此它将其视为必须添加到表达式树并在查询执行期间进行评估的内容。此操作失败,因为 EF 提供程序无法转换委托调用。

You can use compiled query in other query but you can't make it dependent on the outer query. Examples

// You can do 
var someParams = 10;
var dataQuery = from x in ctx.SomeData
                join y in myCompiledQuery.Invoke(ctx, someParams) 
                    on x.Id equals y.Id
                where x.Name = "ABC"
                select new { x, y };

// You can't do - this example will not compile but let's use it for description
var dataQuery = from x in ctx.SomeData
                join y in myCompiledQuery.Invoke(ctx, x.SomeParams) 
                    on x.Id equals y.Id
                where x.Name = "ABC"
                select new { x, y };

The difference is that first example just executes delegate (compiled query is a delegate) and returns IQueryable. The second example can't execute delegate because it is dependent on outer query data so it takes it as something that must be added to expression tree and eveluated during query execution. This fails because EF provider is not able to translate delegate invocation.

香橙ぽ 2024-11-07 06:38:06

您可以组合和嵌入查询,但我认为您不能使用编译的查询。不过,这实际上并不重要,因为 EF 只会编译组合查询一次,然后缓存它(并且数据库后端应该缓存关联的查询计划)。

因此,您可以使用以下内容:

var reviewQuery = from mcq in reviews
                  select new 
                  {
                      prod_id = mcq.prod_id
                      rating = mcq.review_rating,
                      review = mcq.review_text
                  };

 from cat in ctx.cat_table 
 join prod in ctx.prod_table on cat.category_id equals prod.category_id
 select new
 {
      cat_id = cat.category_id,
      prod_id = prod.product_id,
      name = prod.product_name,
      descript = prod.product_description,
      price = prod.price,
      reviews = from r in reviewQuery where r.prod_id == prod_id select r
 }

You can combine and embed queries, but I do not think you can use compiled queries. It really shouldn't matter much though, because EF will only compile the combined query once and then cache it (and the database backend should cache the associated query plan).

You could therefore use something along these lines:

var reviewQuery = from mcq in reviews
                  select new 
                  {
                      prod_id = mcq.prod_id
                      rating = mcq.review_rating,
                      review = mcq.review_text
                  };

 from cat in ctx.cat_table 
 join prod in ctx.prod_table on cat.category_id equals prod.category_id
 select new
 {
      cat_id = cat.category_id,
      prod_id = prod.product_id,
      name = prod.product_name,
      descript = prod.product_description,
      price = prod.price,
      reviews = from r in reviewQuery where r.prod_id == prod_id select r
 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文