How to get the average of the same product

i want to get the average value of each product using the same data table,
Like the example in SS below


How can I do it?

please check this one.
Test_LINQ_Group_Sum_Count.xaml (7.4 KB)

It is handling Int TYpe. If you want floating values then you can put "Covert.todouble instead of Cint.


First get unique values from “Product” columns and then filter “Product” column one by one, by using unique values. You will get filtered data tables.

then use below logic:

sum = 0
count = dt.rows.count
for each row in Dt:
sum = sum+ Convert.ToInt32(row.Item(“Value”).ToString)

Average = sum/count

Mark it as solution it solves your issue.

1 Like


Can you try the following sample?

dt = dt.AsEnumerable.GroupBy(Function(r) r("Product").ToString).SelectMany(Function(g) g.Select(Function(r,i) dt.Clone.LoadDataRow({r("Product"),r("Value"),if(i=0,g.Average(Function(r2) CDbl(r2("Value"))).ToString,"")},False))).CopyToDataTable (3.0 KB)


Thanks All

1 Like

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