1

I am trying to display and group json data and group the components and results of the entire array by one unique ID and then by date (for all the components and results in the array).

I already took the data from the json file, now i am stuck with grouping the data..

Any help is appreciated, Thanks!

Patients.cs:

namespace ConsoleApp { class Patient { public int ID{ get; set; } public string component { get; set; } public string result { get; set; } public string date { get; set; } } class Patients { public List<Patient> patients; } } 

UPDATE:

TABLE

4
  • Likely LINQ GroupBy will be involved. Show some code. Commented Dec 12, 2018 at 12:47
  • @Rotem Updated! Commented Dec 12, 2018 at 12:53
  • 1
    If you're already able to deserialize the data from JSON And populate the patients list then you can omit the entire JSON and first code block from your question, it will make it easier to answer. Also there are plenty of questions on SO dealing with GroupBy, what exactly are you having trouble with? Commented Dec 12, 2018 at 13:03
  • @Rotem I am having trouble with the logic behind this: I need each component and result (from all separate objects) shown for one ID. Commented Dec 12, 2018 at 13:48

1 Answer 1

3

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 
Sign up to request clarification or add additional context in comments.

12 Comments

Wow, Great! This is almost what i needed, see my updated code to see what the result should look like, I need each component and result shown for one ID (the logic behind this is what confuses me :( ).
Actually, this works great! I just removed the ID and Date keys from Console.Writeline(); and i got what I wanted, sorry for complicating, and thank you ! :)
Can I ask one more question? Can I show this data in two rows in console app (just like the update in my post)?
@DevShieldMaiden, answer updated. kindly view Edit section in answer with output :)
Thank you a lo! This has been bugging me for a few days now :)
|