2

I just need to SELECT Distinct values from columnA then ADD the values that has joined to columnB

Sample:

columnA    |    columnB
A                3
B                4
A                3
A                2 
B                1
C                3

Result:

columnA        |    columnB
A                    8
B                    5
C                    3

i found this but it just have the array of column names as parameter.

is there any other way or a sample using DataTable.Select()

Thanks in advance

Community
  • 1
  • 1
Vincent Dagpin
  • 3,581
  • 13
  • 55
  • 85

1 Answers1

7

You can use LINQ-to-DataSet and Enumerable.GroupBy:

var colAGroups = tbl.AsEnumerable()
                .GroupBy(row => row.Field<String>("ColumnA"))
                .Select(grp => new
                {
                    Value = grp.Key,
                    Sum = grp.Sum(row => row.Field<int>("ColumnB"))
                });

foreach (var colAGroup in colAGroups)
{
    Console.WriteLine(String.Format("{0} {1}", colAGroup.Value, colAGroup.Sum));
}
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939