EntityFramework sum嵌套
一个查询中 用到了 sum , 可是返回结果的小数有很多位 , 都不准确了..类似js中的小数运算一样...不太熟悉C#,不知道这问题是因为double的关系 , 还是因为代码写的问题 ,
通过 sql server profile 获取的sql语句执行后都是没有问题的
这是我的代码:
var query = db.SupDataGreenAccount1 .Where(lambda) .GroupBy( p => new{ p.ObjPos , p.District , p.Feature }) .Select( p => new {
//area是 double , 数据库中是 float sums = p.Sum(c => (double?)c.Area ?? 0), objpos = p.Key.ObjPos , district = p.Key.District , feature = p.Key.Feature }); var ss = query.ToArray(); var list = query.ToList();
这是调试中获取的数据:
这是 sql server profile 执行的sql:
SELECT 1 AS [C1], [GroupBy1].[A1] AS [C2], [GroupBy1].[K1] AS [ObjPos], [GroupBy1].[K3] AS [DISTRICT], [GroupBy1].[K2] AS [FEATURE] FROM ( SELECT [Extent1].[ObjPos] AS [K1], [Extent1].[FEATURE] AS [K2], [Extent1].[DISTRICT] AS [K3], SUM([Extent1].[AREA]) AS [A1] FROM [dbo].[Sup_Data_GreenAccount_1] AS [Extent1] WHERE (1 = 1) AND (( CAST(CHARINDEX([Extent1].[DISTRICT], N'保税区') AS int)) > 0) GROUP BY [Extent1].[ObjPos], [Extent1].[FEATURE], [Extent1].[DISTRICT] ) AS [GroupBy1]
这是执行上面的SQL返回的结果: