使用PredicateBuilder,LINQPad和运算符ANY生成的SQL [英] Generated SQL with PredicateBuilder, LINQPad and operator ANY
问题描述
我先前提出的问题有关中链条件的问题Linq To实体. 现在,我使用 LinqKit ,一切正常. 我想查看生成的SQL,并在阅读此答案后,使用
I previously asked a question about chaining conditions in Linq To Entities. Now I use LinqKit and everything works fine. I want to see the generated SQL and after reading this answer, I use LinqPad.
这是我的声明:
var predProduct = PredicateBuilder.True<Product>();
var predColorLanguage = PredicateBuilder.True<ColorLanguage>();
predProduct = predProduct.And(p => p.IsComplete);
predColorLanguage = predColorLanguage.And(c => c.IdColorEntity.Products.AsQueryable().Any(expr));
ColorLanguages.Where(predColorLanguage).Dump();
该代码可在VS2008中运行,编译并生成正确的结果集,但在LinqPad中,出现以下错误:
The code works in VS2008, compile and produce the correct result set, but in LinqPad, I've the following error:
NotSupportedException: The overload query operator 'Any' used is not Supported.
如果LINQPad失败,如何查看生成的SQL?
How can I see the generated SQL if LINQPad fails?
编辑
如果我写
var predColorLanguage = PredicateBuilder.True<ColorLanguage>();
predColorLanguage = predColorLanguage.And(c => c.IdColorEntity.Products.Any((p => p.IsComplete));
ColorLanguages.Where(predColorLanguage).Dump();
有效... WTF吗?
works... WTF?
推荐答案
在使用LINQKit时,可以通过在提供EntitySet的表达式上调用Compile(),然后在LINQKit上调用AsExpandable()来完成此工作.主要查询:
As you're using LINQKit, you can make this work by calling Compile() on the expression that feeds the EntitySet, and then calling AsExpandable() on the main query:
var predProduct = PredicateBuilder.True<Product>();
var predColorLanguage = PredicateBuilder.True<ColorLanguage>();
predProduct = predProduct.And(p => p.IsComplete);
predColorLanguage = predColorLanguage.And (
c => c.IdColorEntity.Products.Any(predProduct.Compile()));
ColorLanguages.AsExpandable().Where(predColorLanguage).Dump();
正如LINQKit文章中解释的 所述,Compile方法实际上从未运行:AsExpandable删除它并修改表达式树,以便它与LINQ to SQL一起使用.
As explained in the LINQKit article, the Compile method never actually runs: AsExpandable strips it out and modifies the expression tree so that it works with LINQ to SQL.
这篇关于使用PredicateBuilder,LINQPad和运算符ANY生成的SQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!