c# - 如何使用 Entity Framework 6 将数学函数发送到 Sql Server

标签 c# sql-server entity-framework linq

我正在尝试使用 Entity Framework 6 将数学函数发送到 sql server。

我有一个简单的查询:

using(var db = databaseContext)
{
    var query = db.Foo.Select(x => Math.Sin(x.bar));
}

但是这给了我一个异常(exception)。

An exception of type 'System.NotSupportedException' occurred in EntityFramework.dll but was not handled in user code

Additional information: LINQ to Entities does not recognize the method 'Double Sin(Double)' method, and this method cannot be translated into a store expression.

问题是 Entity Framework 不知道如何将 Math.sin 翻译成 Sql Server 等价物。我可以使用任何其他有效的类吗?

最佳答案

我会先从数据库中加载数据,然后在内存中进行这样的计算

using(var db = databaseContext)
{
    var listofFoo= db.Foo.toList();
    var listofFooSin = listofFoo.Select(x => Math.Sin(x.bar));
}

关于c# - 如何使用 Entity Framework 6 将数学函数发送到 Sql Server,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39274589/

相关文章:

entity-framework - EF 代码第一个最终版本中的鉴别器列

entity-framework - 安装 EF 4.4.0.0

c# - 如何在 C# 中将数据源绑定(bind)到 .rdlc 报告

C# Windows 窗体用户控件控件设计器支持

c# - 为什么大多数序列化程序使用流而不是字节数组?

c# - 哪一种是在 SQL Server 中复制数据库的最佳方法?

sql-server - 存储过程更改未反射(reflect)在 dacpac 脚本中

c# - 如何自动增加表中的列?

asp.net-mvc - 在 Asp.Net MVC 应用程序中调用 UnitOfWork.Commit() 的位置

c# - 二进制序列化与应用程序设置 (user.config)