我有以下SQL查询:
SELECT
[nu_ano],
[nu_mes],
[id_projeto],
[id_fase],
'Financial Progress ("Competência")' as ds_categoria,
'Baseline' as ds_curva,
vl_baseline as vl_curva,
cast((vl_baseline / (pc_baseline / 100)) as decimal(18,2)) as vl_curva_total
FROM [Alvarez_Marsal].[dbo].[Schedule_Status]
我的LINQ查询是这样的:
var result = (from l in db.Schedule_Status
.Where(x => x.nu_mes == 12)
.Select(x => new Retorno
{
nu_ano = x.nu_ano,
nu_mes = x.nu_mes,
id_projeto = x.id_projeto,
id_fase = x.id_fase,
ds_categoria = "Financial Progress ('Competência')",
ds_curva = "Baseline",
vl_curva = x.vl_baseline,
vl_curva_total = decimal.Round((decimal)(x.vl_baseline / x.pc_baseline / 100), 2)
})
select l);
在SQL查询中转换我的LINQ查询,结果是:
SELECT
[Extent1].[nu_ano] AS [nu_ano],
[Extent1].[nu_mes] AS [nu_mes],
[Extent1].[id_projeto] AS [id_projeto],
[Extent1].[id_fase] AS [id_fase],
N'Financial Progress (''Competência'')' AS [C1],
N'Baseline' AS [C2],
[Extent1].[vl_baseline] AS [vl_baseline],
ROUND(([Extent1].[vl_baseline] / [Extent1].[pc_baseline]) / cast(100 as decimal(18)), 2) AS [C3]
FROM [dbo].[Schedule_Status] AS [Extent1]
WHERE 12 = [Extent1].[nu_mes]
Class Retorno
class Retorno
{
public int nu_ano { get; set; }
public short nu_mes { get; set; }
public int id_projeto { get; set; }
public int id_fase { get; set; }
public string ds_categoria { get; set; }
public string ds_curva { get; set; }
public decimal? vl_curva { get; set; }
public decimal? vl_curva_total { get; set; }
}
鉴于SQL语言中没有Convert.ToDecimal(),我如何在LINQ中将转录为十进制(18,2)?可能吗?我已经尝试过使用decimal.Round()但它没有用。
我的LINQ查询中的字段vl_curva_total正在返回 3000.0000000000000000000正确将返回30000000.00
答案 0 :(得分:4)
您的操作顺序在两个查询之间发生了变化。
SQL查询
(vl_baseline / (pc_baseline / 100))
LINQ查询
(x.vl_baseline / x.pc_baseline / 100)
这解释了两个值的差异(3000 vs 30000000)。只需将括号添加到LINQ
查询中即可解决此问题。
(x.vl_baseline / (x.pc_baseline / 100))