Reputation: 621
I Have DataTable Similar Like this.
If the adults value and child value are same. I need to Remove it and count that. I need a output similar like this.
Can anyone please help me on this???.
Thank you,
Upvotes: 0
Views: 2776
Reputation: 460158
You want to group by adults+child:
var groups = tblRoooms.AsEnumerable()
.GroupBy(r => new{ Adults = r.Field<int>("Adults"), Child = r.Field<int>("Child") });
var tblRooomsCopy = tblRoooms.Clone(); // creates an empty clone of the table
foreach(var grp in groups)
{
int roomCount = grp.Sum(r => r.Field<int>("Roomcount"));
DataRow row = tblRooomsCopy.Rows.Add();
row.SetField("RoomNo", grp.First().Field<int>("RoomNo"));
row.SetField("Roomcount", roomCount);
row.SetField("Adults", grp.Key.Adults);
row.SetField("Child", grp.Key.Child);
}
Now you have your desired result in tblRooomsCopy
.
Upvotes: 2
Reputation: 648
I won't write the complete code for you but I will describe a suggested way: first order the datatable by adults and child, that will cause same rows to be consecutive, create a list that you will fill rows to be deleted then use foreach to compare each row with the previous one, if it has the same value then add it to the list of rows to be removed, finally you will delete the rows in the list
Upvotes: 0