【问题标题】:LINQ to SQL different output when convert SQL query to LINQ将 SQL 查询转换为 LINQ 时,LINQ to SQL 的输出不同
【发布时间】:2016-12-14 07:20:14
【问题描述】:

我想知道将 SQL 查询转换为 LINQ 时的输出:

select coalesce(SUM(cdin_ActMortgageAmnt),0)  
from CRM.dbo.CDIndex,CRM.dbo.company  
where comp_companyid=cdin_companyid 
  and comp_idcust like '%10319%' 
  and cdin_goodscategory ='Financed' 
  and cdin_Deleted is null
  and cdin_startunstufdate is not null 
  and cdin_Status='InProgress'

我实现的 LINQ 查询是:

 var ACTMORTGAGE= (from com in db.Companies
                       join cd in db.CDIndexes on com.Comp_CompanyId
                       equals cd.cdin_CompanyId
                    where
                       cd.cdin_Status == "InProgress" &&
                       com.Comp_IdCust==c.Comp_IdCust&&
                       cd.cdin_startunstufdate != null &&
                       cd.cdin_Deleted == null
                    select new { ActAmnt=  cd.cdin_ActMortgageAmnt });

然后我这样总结:

decimal ACT = (decimal)ACTMORTGAGE.AsEnumerable().Sum(o =>o.ActAmnt);

本机 SQL 查询的输出是:0.000000

而在 LINQ 查询生成的decimal ACT 中是:0 出现时没有小数点。

我确信我的 LINQ 查询没有任何问题,但为什么 LINQ 查询给我的输出与本机 SQL 不同?

【问题讨论】:

    标签: c# asp.net sql-server linq


    【解决方案1】:

    IMO 它们实际上是相同的。 如果您确定 ActAmnt 是小数,那么您可以使用

    查看尽可能多的小数

    Decimal.ToString("0.0000000")

    您的 SQL 查询返回最大精度,因为您可能已在表中将列声明为 DECIMAL(12,5)

    【讨论】:

    • 我发现 SQL server 我发现 cdin_ActMortgageAmnt 被声明为 numaric(24,6) 但在 Visual Studio 中它是:public Nullable cdin_ActMortgageAmnt { get;放; }
    • @faresAyyad 我们在谈论同样的事情。 :)
    • @faresAyyad 是的,所以你可以接受我的回答,因为它是正确的。
    猜你喜欢
    • 2017-10-06
    • 2020-08-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-15
    • 2010-12-02
    相关资源
    最近更新 更多