Linq to EF - 不支持的功能
我正在使用 Linq 进行查询,并由实体框架数据源支持。
我收到以下错误:
LINQ to Entities 无法识别“Double Sqrt(Double)”方法 方法,并且该方法无法转换为存储表达式。
这是我的函数的简化版本(我的版本更复杂,使用 ACos、sin、cos 和其他 C# 数学类函数)。
var objects =
from n in context.Products.Where(p => p.r == r)
let a = Math.Sqrt((double)n.Latitude)
where a < 5
orderby a
select n;
return objects.Take(100).ToList();
我认为这个问题可能与以下情况有关:与 Linq to SQL 相比,Linq to EF(和 SQL 数据源)支持的功能有限。我对此还比较陌生,所以我不是 100% 确定。
谁能给我指出正确的方向?
干杯,
I'm making a query with Linq, backed by an Entity Framework datasource.
I'm getting the following error:
LINQ to Entities does not recognize the method 'Double Sqrt(Double)'
method, and this method cannot be translated into a store expression.
Here's a simplified version of my function (my version is more complex and uses ACos, sin, cos and other C# Math class functions).
var objects =
from n in context.Products.Where(p => p.r == r)
let a = Math.Sqrt((double)n.Latitude)
where a < 5
orderby a
select n;
return objects.Take(100).ToList();
I think the problem may be related to the situation that Linq to EF (and a SQL datasource) has a limited set of supported function compared to Linq to SQL. I'm relatively new to this so I'm not 100% sure.
Can anyone give me a pointer in the right direction?
Cheers,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试在
SqlFunctions
中定义的 SquareRoot 函数Try SquareRoot function defined in
SqlFunctions
如果您开始使用 LINQ-to-objects 学习 LINQ,那么一旦开始使用 LINQ-to-Entities,您就会经常遇到这种情况。
您几乎可以执行任何要在 LINQ-to-objects 中编译的操作,因为 LINQ-to-objects 在编译时会转换为代码。
LINQ-to-Entities(和 LINQ-to-SQL)转换为表达式树。因此,只有特定 LINQ 提供程序允许的语法才有效。在我的第一个“真正的”LINQ-to-Entities 表达式中(编译得很好),我遇到了大约 5 次此错误,因为我逐一删除了 LINQ-to-Entities 未处理的代码。
所以当你看到这个的时候,这很正常,很常见。每次你都需要寻找另一种方式。
您可以使用逻辑等效方法来避免该问题:
您还可以将所有数据提取到客户端,然后使用 LINQ-to-objects 运行代码:
最后,您应该能够在服务器上执行此数学运算。查看System.Data.Objects.SqlClient.SqlFunctions SqlFunctions 类。这些函数将转换为表达式。 这看起来特别像是票。
If you start of learning LINQ with LINQ-to-objects, you'll run into this a lot once you start using LINQ-to-Entities.
You can do pretty much anything that will compile in LINQ-to-objects, because LINQ-to-objects translates into code when compiled.
LINQ-to-Entities (and LINQ-to-SQL) translates into expression trees. So, only the syntax that that specific LINQ provider allowed for is valid. In my first "for real" LINQ-to-Entities expression, which compiled just fine, I ran into this error about 5 times, as one by one I removed code that wasn't handled by LINQ-to-Entities.
So when you see this, it's normal and common. You need to find another way each time.
You could avoid the problem with a logical equivalent:
You could also pull all the data to the client and then run your code using LINQ-to-objects:
Finally, you should be able to do this math on the server. Check out the System.Data.Objects.SqlClient.SqlFunctions SqlFunctions Class. These functions will translate into the expression. This in particular looks like it might be the ticket.
请尝试
Please try