是否可以阻止Linq在SQL的where子句中转换值?

时间:2017-10-17 17:26:24

标签: .net vb.net linq

我正在尝试使用LINQ复制以下查询:

select p_id, sum(pct_ownership)
from owner 
where PCT_OWNERSHIP is not null
  and status = N'O'
group by p_id 
having sum(pct_ownership) > 100

我创建的Linq表达式如下:

From p In context.Owner
Where p.Status = "O" And p.Pct_Ownership IsNot Nothing
Group By pid = p.P_Id Into pct = Sum(p.Pct_Ownership)
Where pct > 100
Select pct, pid

这是LINQ表达式生成的SQL:

SELECT 
    [GroupBy1].[K1] AS [P_Id], 
    CAST( [GroupBy1].[A1] AS real) AS [C1]
FROM 
    (SELECT 
         [Extent1].[P_Id] AS [K1], 
         SUM([Extent1].[Pct_Ownership]) AS [A1]
     FROM 
         [dbo].[Owner] AS [Extent1]
     WHERE 
         (N'O' = [Extent1].[Status]) AND ([Extent1].[Pct_Ownership] IS NOT NULL)
     GROUP BY 
         [Extent1].[P_Id]) AS [GroupBy1]
WHERE  
    CAST( [GroupBy1].[A1] AS real) > cast(100 as real)

当我将原始查询结果与linq表达式生成的结果进行比较时,看起来铸件正在删除一些结果。

我的猜测是,由于某些精度问题,演员正在舍入Sum(Pct_Ownership)值(这很奇怪,因为原始值存储为真实值)。

有没有办法阻止linq在查询的where子句中转换值?

0 个答案:

没有答案