我有一个编译好的查询,效果很好。我将一个 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
}
}
我早期尝试做这样的事情会引发错误:
The LINQ expression node type 'Invoke' is not supported in LINQ to Entities
我考虑过的一种替代方法是用 SQL View 替换我的编译查询,但我担心会对性能造成负面影响。
非常感谢您提供的任何建议。
最佳答案
您可以在其他查询中使用编译查询,但不能使其依赖于外部查询。例子
// 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 };
区别在于第一个示例只执行委托(delegate)(编译查询是委托(delegate))并返回 IQueryable
。第二个示例无法执行委托(delegate),因为它依赖于外部查询数据,因此它将其视为必须添加到表达式树并在查询执行期间评估的内容。这会失败,因为 EF 提供程序无法转换委托(delegate)调用。
关于c# - 我可以在第二个查询中使用已编译的查询作为源吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5600504/