Domanda

I Have DataTable Similar Like this.enter image description here

If the adults value and child value are same. I need to Remove it and count that. I need a output similar like this.

enter image description here

Can anyone please help me on this???.

Thank you,

È stato utile?

Soluzione

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.

Altri suggerimenti

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

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top