DataTable Group By Sum and Count using Linq

Hi.

Normally, if you are processing the data by employee name (in your case), you would want to filter your data set to the items for each employee. Then, you can simply use .Sum() on each employee as you process it. This simple method would look something like this:

For each empl In Transactions.AsEnumerable.Select(Function(r) r("EmployeeName").ToString ).ToArray.Distinct
    sumTrx = Transactions.AsEnumerable.Where(Function(r) r("EmployeeName").ToString = empl ).ToArray.Sum(Function(r) If(IsNumeric(r("Transaction").ToString.Trim), CDbl(r("Transaction").ToString.Trim, 0) )
    countTrx = Transactions.AsEnumerable.Where(Function(r) r("EmployeeName").ToString = empl ).ToArray.Count

However, if you would like to return this information using Group By, then I would say check out the information here:

Or there might be other posts out there. - If you need further help getting GroupBy to work, then let us know; I’m “not” really an expert using GroupBy.

Regards.

1 Like