Ed *_*ess 3 c# linq-to-entities notsupportedexception
为什么会抛出System.NotSupportedException?
string foo(string f) { return f; }
string bar = "";
var item = (from f in myEntities.Beer
where f.BeerName == foo(bar)
select f).FirstOrDefault();
Run Code Online (Sandbox Code Playgroud)
编辑:这是一个MSDN参考(有点)解释事情......
LINQ to Entities查询中未显式映射到规范函数的任何方法调用都将导致抛出运行时NotSupportedException异常.有关映射到规范函数的CLR方法列表,请参阅CLR方法到规范函数映射.
另见http://mosesofegypt.net/post/LINQ-to-Entities-what-is-not-supported.aspx
编辑:好的,代码爆炸,因为它不知道如何处理调用foo().该查询构建为表达式树,然后转换为SQL.
表达式树翻译器知道各种各样的事情 - 比如字符串相等和各种其他方法(例如string.StartsWith),但它不知道你的foo方法做什么- foo()就其而言是一个黑盒子.因此它无法将其转换为SQL.