Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to restructure an Object in C# using LINQ?

I have a data set as follows:

[
    {
        "Id": 1,
        "Country": "Uruguay",
        "Name": "Foo",
        "Status": "Completed",
    },
    {
        "Id": 2,
        "Country": "Uruguay",
        "Name": "Foo",
        "Status": "Completed",
    },
    {
        "Id": 3,
        "Country": "Germany",
        "Name": "Foo",
        "Status": "Completed",
    },
]

I want to transform and sort it by Country so that it looks as follows:

[
    {
        "Country": "Uruguay",
        "Details": [
         {
            "Id": 1,
            "Name": "Foo",
            "Status": "Completed",
         },
         {
            "Id": 2,
            "Name": "Foo",
            "Status": "Completed",
         },
        ],
    },
    {
        "Country": "Germany",
        "Details": [
         {
            "Id": 3,
            "Name": "Foo",
            "Status": "Completed",
         },
        ],
    },
],

These are the classes in C#:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

public class Countries {
    public int Id { get; set; }
    public string Country { get; set; }
    public string Name { get; set; }
    public string Status { get; set; }
}

public class Details {
    public int Id { get; set; }
    public string Name { get; set; }
    public string Status { get; set; }
}

public class CountryList {
    public string Country { get; set; }
    public List<Details> Details { get; set; }
}

Some of what I have tried looks as followed:

var foo = countries
    .GroupBy(x => new Details { Id = x.Id, Name = x.Name, Status = x.Status })
    .Select( y => new CountryList 
     {
         // Country = y.Key. 
     }

var foo = countries
    .GroupBy(x => x.Country)
    .Select( y => new CountryList 
     {
         // Country = y.Key.
         Details = y.GroupBy(a => new Details 
         { 
            Id = a.Id, 
            Name = a.Name, 
            Status = a.Status  
         }).ToList() 
     }

I am having trouble working out how to use LINQ to solve this. I have done a handful of GroupBy operations in the past, but I wasn’t able to work this one out. How do I transform my dataset into the desired result?

>Solution :

You do not need second GroupBy

var foo = countries
    .GroupBy(x => x.Country)
    .Select(y => new CountryList 
     {
         Country = y.Key,
         Details = y.Select(a => new Details 
         { 
            Id = a.Id, 
            Name = a.Name, 
            Status = a.Status  
         }).ToList() 
     };
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading