How to sum of specific value in column by using linq query

How to sum of specific value in column by using linq query

input:

expected
output:

Hi @robert_singval

Try with this expression to sum the Price column as you share in the SS

(From d in dtData.AsEnumerable Where Not (isNothing(d("Price")) OrElse String.IsNullorEmpty(d("Price").toString.Trim)) Select v = CDbl(d("Price").toString.Trim)).Sum(Function (x) x)

Reghards
Gokul

If you need to group the Category and need to sum the group by value

(From d In DtBuild.AsEnumerable
Group d By k=d("Category").toString.Trim Into grp = Group
Let nj =  grp.Sum(Function (x) CDbl(x("Price").toString.Trim))
Let ra = New Object(){k,nj}
Select r = DtOutput.Rows.Add(ra)).CopyToDataTable

Regards
Gokul

Hii
@robert_singval

Use this Linq Query

(
From row In DT
Group row By a = row(“Category”) Into grp = Group
Let Sum = grp.Sum(Function(x) CDbl(x(“Price”).tostring.trim))
Select DT1.Rows.Add({a,sum})
).CopyToDataTable

Cheers…!

4 Likes

Hi,

How about the following sample?

 dt.AsEnumerable.GroupBy(Function(r) r("Category").ToString).Select(Function(g) dt.Clone.LoadDataRow({0,g.Key,g.Sum(Function(r) CDbl(r("Price").ToString.Replace(",","")))},False)).CopyToDataTable.DefaultView.ToTable(False,{"Category","Price"})

Sample
Sample20231011-3L.zip (9.0 KB)

Regards,

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