0

I am fairly new to .Net and c# and I have come across a situation where I have to convert a flat list of objects into a nested list of objects. I am stuck and can't move any further.

I have this

[
    {
        id: 1,
        name: "abc",
        date: "8/8/20"
    },
    {
        id: 2,
        name: "xyz",
        date: "9/8/20"
    },
    {
        id: 1,
        name: "def",
        date: "9/8/20"
    }
]

and I need to convert it into this

[
    {
        id: 1,
        nameList: [
            {
                name: "abc",
                date: "8/8/20"
            },
            {
                name: "def",
                date: "9/8/20"
            }
        ]
    },
    {
        id: 2,
        nameList: [
            {
                name: "abc",
                date: "8/8/20"
            }
        ]
    }
]

Can someone please help me out?

vsrp demo
  • 15
  • 5

1 Answers1

0

You can use Linq's GroupBy to achieve this:

List<Record> records =...

IEnumerable<IGrouping<int, Record>> groups = records.GroupBy(r => r.id);

Note that the result is not a list, if you really want the output you wrote:

var nested = records.GroupBy(r => r.id).Select(g =>
    new
    {
       id = g.Key,
       children = g.Select(cr =>
       new 
       {
         name = cr.name,
         date = cr.date
       }).ToList()
    }).ToList());

Link to Fiddle

using System;
using System.Collections.Generic;
using System.Linq;

public class Program
{
    class Record
    {
        public int id {get; set;}
        public string name {get; set;}
        public DateTime date {get; set;}
    }

    public static void Main()
    {
        var records = new List<Record>()
        {
            new Record()
            {
                id = 1,
                name = "abc",
                date = new DateTime(2020, 8, 8)
            },
            new Record()
            {
                id = 2,
                name = "def",
                date = new DateTime(2020, 8, 9)
            },
            new Record()
            {
                id = 1,
                name = "def",
                date = new DateTime(2020, 8, 9)
            }
        };

        var nested = records.GroupBy(r => r.id).Select(g =>
            new
            {
               id = g.Key,
               children = g.Select(cr =>
               new 
               {
                 name = cr.name,
                 date = cr.date
               }).ToList()
            }).ToList();

        foreach (var parent in nested)
        {
            Console.WriteLine($"Parent #{parent.id}");

            foreach (var child in parent.children)
            {
                Console.WriteLine($"\tChild {child.name} {child.date}");
            }
        }
    }
}
Parent #1 Child abc 8/8/2020 12:00:00 AM Child def 8/9/2020 12:00:00 AM

Parent #2 Child def 8/9/2020 12:00:00 AM

vc 74
  • 37,131
  • 7
  • 73
  • 89
  • This worked for me! Why is this answer getting down voted? – vsrp demo Aug 20 '20 at 16:43
  • @vsrpdemo I have no idea but quite regularly on SO people pull the 'Close/Downvote' trigger without properly reading questions/answers. Glad I could help anyway. – vc 74 Aug 20 '20 at 16:45
  • @vsrpdemo I've asked to reopen you should be able to accept if needed – vc 74 Aug 20 '20 at 16:53