This should be simple but i am not able to figure this out. I have a Datatable that has Name, Age where there are rows that have student names, but then multiple rows of special students' Name 'Chris's, and John's. the end result should have a table with all the rows except Chris and John to be combined into one row called Other with the sum of their age.
ie.
NAME | AGE
Tom | 20
John | 15
Peter | 5
John | 2
Tom | 33
Chris | 20
End Result:
NAME | AGE
Tom | 20
Peter | 5
Tom | 33
Other | 37
This is what im doing right now:
var others = table.AsEnumerable().Where(x => x["NAME"].ToString().Equals("Chris") || x["NAME"].ToString().Equals("John"));
var tmpOthers = 0;
foreach (DataRow otherRow in others)
tmpOthers += decimal.Parse(otherRow["AGE"].ToString());
table = table.AsEnumerable().Where(x => !x["NAME"].ToString().Equals("Chris") || !x["NAME"].ToString().Equals("John")).CopyToDataTable();
table.Add(//OTHER-ROW-Here)
This works but i know there must be an easier way to do this in a single LiNQ statement. also i could not put the table.Rows.Remove in the same for loop because of iteration change.
namealways starts with upper case?