Linq to EF - 不支持的功能

发布于 2025-01-06 18:39:56 字数 615 浏览 3 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

哆啦不做梦 2025-01-13 18:39:56

尝试在 SqlFunctions 中定义的 SquareRoot 函数

    var objects =
        from n in context.Products.Where(p => p.r == r)
        let a = SqlFunctions.SquareRoot((double)n.Latitude)
        where a < 5
        orderby a
        select n;

    return objects.Take(100).ToList();

Try SquareRoot function defined in SqlFunctions

    var objects =
        from n in context.Products.Where(p => p.r == r)
        let a = SqlFunctions.SquareRoot((double)n.Latitude)
        where a < 5
        orderby a
        select n;

    return objects.Take(100).ToList();
慕巷 2025-01-13 18:39:56

如果您开始使用 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 未处理的代码。

所以当你看到这个的时候,这很正常,很常见。每次你都需要寻找另一种方式。

您可以使用逻辑等效方法来避免该问题:

var objects =
    from n in context.Products.Where(p => p.r == r)
    where (double)n.Latitude < 25
    orderby a
    select n;

return objects.Take(100).ToList();

您还可以将所有数据提取到客户端,然后使用 LINQ-to-objects 运行代码:

var objects =
    from n in context.Products.Where(p => p.r == r).ToList()
    let a = Math.Sqrt((double)n.Latitude)
    where a < 5
    orderby a
    select n;

return objects.Take(100).ToList();

最后,您应该能够在服务器上执行此数学运算。查看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:

var objects =
    from n in context.Products.Where(p => p.r == r)
    where (double)n.Latitude < 25
    orderby a
    select n;

return objects.Take(100).ToList();

You could also pull all the data to the client and then run your code using LINQ-to-objects:

var objects =
    from n in context.Products.Where(p => p.r == r).ToList()
    let a = Math.Sqrt((double)n.Latitude)
    where a < 5
    orderby a
    select n;

return objects.Take(100).ToList();

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.

风吹过旳痕迹 2025-01-13 18:39:56

请尝试

var objects =
        from n in context.Products.Where(p => p.r == r)
        let a = Math.Pow((double)n.Latitude, 0.5)
        where a < 5
        orderby a
        select n;

Please try

var objects =
        from n in context.Products.Where(p => p.r == r)
        let a = Math.Pow((double)n.Latitude, 0.5)
        where a < 5
        orderby a
        select n;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文