Improve Linq query group by and sum

Hello,

I need help regarding my Linq query

I group by “Name” and sum the “Montant HT” but i need also to sum the “REGUL HT” (when there is a regul).

Which means that my client’s excel file, there are amounts in “Regul HT” and sometimes rows are empty.

The linq was perfectly working, but when i have added the “Let sr =…”
see below
(From d In dtData.AsEnumerable
Group d By k1= d.item(“Name”).ToString Into grp= Group
Let s = grp.Sum(Function (x) CDbl(x(“Montant HT”).toString.Trim))
Let sr = grp.Sum(Function (x) CDbl(x(“REGUL HT”).toString.Trim))
Let ra = New Object(){grp.First()(0),grp.First()(1),grp.First()(2),grp.First()(3),k1,s, grp.First()(6),sr,grp.First()(8)}
Select dt_Asset.Rows.Add(ra)).CopyToDataTable

I have a error message : Assign: Conversion from string “” to type ‘Double’ is not valid.
Because on my Excel file, in the column “Regul HT” Some rows are empty.

How can I do pay attention only if “Regul HT” is fill in, please ?

@MAHESH1
@AkshaySandhu
@GBK

@najoua.abbaci
give a try on:
Let sr = grp.Sum(Function (x) CDbl(“0” & x(“REGUL HT”).toString.Trim))

2 Likes

Thank you very much @ppr

it’s working !

1 Like

@ppr I personally find this a very clever solution.

2 Likes

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.