【发布时间】:2014-04-29 12:52:38
【问题描述】:
我在 TSQL 中有一个查询,我正在尝试将其转换为 LINQ 以在我们的 Web 应用程序中使用,但我真的很努力解决这个问题。它是带有 EF6 的 MVC5,数据库是 SQL Server 2008 R2。任何帮助表示赞赏!
SQL 查询
select MAX(ShipFromCompanyName) as Supplier, COUNT(*) as AllSupplierCount,
SUM(isnull(cast(TransportationCharges as decimal(18,2)),0)) as AllFreightCharges,
SUM(isnull(cast(TransportationCharges as decimal(18,2)),0)) * .45 as FreightSavings
from table
group by ShipFromCompanyName
order by ShipFromCompanyName
ShipFromCompanyName 和TransportationCharges 都作为varchar 存储在数据库中,不幸的是我无法将TransportationCharge 的数据类型更改为小数
LINQ
var Scorecard = (from upsid in _db.table select upsid).GroupBy(x => new { x.ShipFromCompanyName, x.TransportationCharges })
.Select(x => new
{
x.Key.ShipFromCompanyName,
SupplierCount = x.Count(),
FreightCharges = x.Key.TransportationCharges.Cast<decimal>().Sum(),
}).ToList();
【问题讨论】:
-
你试过
decimal.Parse()吗? -
我会把它放在哪里?我要挂断的部分是普通 SQL 查询的
SUM()和SUM() * .45部分。 -
您收到什么错误信息?
标签: c# sql-server asp.net-mvc linq entity-framework