Cast for LINQ decimal?

Asked

Viewed 70 times

1

Since there is no Convert.ToDecimal() in language SQL, how could I transcribe a as decimal(18,2) in LINQ?

I tried through the decimal.Round() but it’s not working.

I have the following query on 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]

And my query in LINQ is like this

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);

Transforming my query LINQ in SQL gets like this:

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]

The field vl_curva_total in my query LINQ is returning 3000.0000000000000000000 while the right is to return 30000000.00

  • Your column in the bank (18,2)?

  • This yes @Gabrielcoletta

  • It is not the best solution, but you can make this decimal.round out of the query, when the values are already in memory and before you make the result return.

  • If I use the Round inside or out the result won’t be the same?

  • What is the return of your method?

  • What exactly is your problem, just displaying the value?

  • I noticed now that a parenthesis is missing in my Query LINQ, the correct is (x.vl_baseline / ( x.pc_baseline / 100))

  • @Leandroangelo Sim

Show 3 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.