我可以使用已编译的查询作为第二个查询的源吗?
我有一个编译好的查询,效果很好。我向它传递了一个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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以在其他查询中使用编译的查询,但不能使其依赖于外部查询。示例
不同之处在于第一个示例仅执行委托(编译后的查询是委托)并返回
IQueryable
。第二个示例无法执行委托,因为它依赖于外部查询数据,因此它将其视为必须添加到表达式树并在查询执行期间进行评估的内容。此操作失败,因为 EF 提供程序无法转换委托调用。You can use compiled query in other query but you can't make it dependent on the outer query. Examples
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.您可以组合和嵌入查询,但我认为您不能使用编译的查询。不过,这实际上并不重要,因为 EF 只会编译组合查询一次,然后缓存它(并且数据库后端应该缓存关联的查询计划)。
因此,您可以使用以下内容:
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: