在Linq EF查询中使用SQRT [英] Using SQRT in a Linq EF Query

查看:73
本文介绍了在Linq EF查询中使用SQRT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将SQRT函数用作Linq EF查询中where子句的一部分.我想我可以做到:

I need to use the SQRT function as part of a where clause in a Linq EF query. I figured I could do this:

var qry = context.MyTable.Where("sqrt(it.field) > 1");

但是它返回一个错误,指出'sqrt'无法解析为有效的类型构造函数或函数.在函数,方法或类型构造函数附近,第6行,第5列".

But it returns an error saying "'sqrt' cannot be resolved into a valid type constructor or function., near function, method or type constructor, line 6, column 5."

我一直认为linq从字面上接受where子句中的内容,并将其转换为直接在SQL中执行的语句.似乎并非如此...

I had always assumed that linq literally takes what's in the where clause and translates that into a statement that is executed directly in SQL. That doesn't seem to be the case...

有人知道解决方法吗?

谢谢

推荐答案

我正在使用Linq实体,并且能够做到这一点:

I'm using Linq Entities and was able to do this:

        testEntities entities = new testEntities ();

        ObjectQuery<Fees> fees = entities.Fees;

        return from f in fees 
               let s = Math.Sqrt((double)f.FeeAmount)
               where s > 1.0 
               select f ;

当我检查生成的SQL时,我得到

When I check the generated SQL, I get

SELECT [t1].[TestTriggerID]
FROM (
    SELECT [t0].[TestTriggerID], SQRT(CONVERT(Float,[t0].[TestTriggerID])) AS [value]
    FROM [TestTrigger2] AS [t0]
    ) AS [t1]
WHERE [t1].[value] > @p0

这似乎是合理的.我无法使用.Where字符串格式来重现相同的代码,但是我可能缺少明显的东西.

This seems reasonable. I was unable to use the .Where string format to reproduce the same code, but I'm probably missing something obvious.

这篇关于在Linq EF查询中使用SQRT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆