You have to group your patient data with ID and Date so you can do this by using linq.
... JsonResponse = objReader.ReadLine(); Patients patients = JsonConvert.DeserializeObject<Patients>(JsonResponse); var result = (from p in patients.patients group p by new { p.ID, p.date } into grp select new { Key = grp.Key, Items = grp.ToList() }).ToList(); foreach (var item in result) { Console.WriteLine(item.Key.ID); Console.WriteLine(item.Key.date); Console.WriteLine(); item.Items.ForEach(x => Console.WriteLine($"ID: {x.ID}, date: {x.date}, result: {x.result}, component: {x.component}")); Console.WriteLine(); } Console.ReadLine();
Output:
1030 10/19/2018 ID: 1030, date: 10/19/2018, result: 1, component: A ID: 1030, date: 10/19/2018, result: 2, component: B ID: 1030, date: 10/19/2018, result: 3, component: C ID: 1030, date: 10/19/2018, result: 4, component: D ID: 1030, date: 10/19/2018, result: 5, component: E 1031 12/12/2019 ID: 1031, date: 12/12/2019, result: 6, component: A ID: 1031, date: 12/12/2019, result: 7, component: B ID: 1031, date: 12/12/2019, result: 8, component: C ID: 1031, date: 12/12/2019, result: 9, component: D ID: 1031, date: 12/12/2019, result: 10, component: E
Edit:
For your custom output that you shown in your screenshot.
... var result = (from p in patients.patients group p by new { p.ID, p.date } into grp select new { Key = grp.Key, Items = grp.ToList() }).ToList(); DataTable dt = new DataTable(); dt.Columns.Add("ID"); dt.Columns.Add("DATUM"); foreach (var item in result.Select(x => x.Items).FirstOrDefault()) { dt.Columns.Add(item.component); } foreach (var item in result) { DataRow dr = dt.NewRow(); dr["ID"] = item.Key.ID; dr["DATUM"] = item.Key.date; foreach (var innerItem in item.Items) { dr[innerItem.component] = innerItem.result; } dt.Rows.Add(dr); } foreach (DataRow row in dt.Rows) { Console.WriteLine($"ID: {row["ID"]}, DATUM: {row["DATUM"]}, A: {row["A"]}, B: {row["B"]}, C: {row["C"]}, D: {row["D"]}, E: {row["E"]}"); }
Output:
ID: 1030, DATUM: 10/19/2018, A: 1, B: 2, C: 3, D: 4, E: 5 ID: 1030980, DATUM: 12/12/2019, A: 6, B: 7, C: 8, D: 9, E: 10
GroupBywill be involved. Show some code.GroupBy, what exactly are you having trouble with?