Girish Mahida
Girish Mahida

Reputation: 85

Split collection into multiple collections

I want to create new list of list on basis of a single list. I have a list like

public class OfficeLocator
{
    public String id{ get; set; }
    public string Geography{ get; set; }
    public string Country{ get; set; }
    public string State{ get; set; }
    public string OfficeName{ get; set; }
}

I am trying to prepare an tree structured list

GeographyName="Asia",
{ 
   Country = "China",
   {
      State = "Hunan",
        {
            {
                OfficeId = "1",
                OfficeName = "Office 1"
            },
            {
                OfficeId = "2",
                OfficeName = "Office 2"
            }
        },
      State = "Hubei"
        {
            {
                OfficeId = "3",
                OfficeName = "Office 3"
            }
        }
    },
    Country = "India",
    {
      State = "Maharashtra",
        {
            {
                OfficeId = "4",
                OfficeName = "Office 4"
            },
            {
                OfficeId = "5",
                OfficeName = "Office 5"
            }
        },
      State = "Punjab"
        {
            {
                OfficeId = "6",
                OfficeName = "Office 6"
            }
        }
    },
},
GeographyName="Europe",
{ 
   Country = "UK",
   {
      State = "York",
        {
            {
                OfficeId = "7",
                OfficeName = "Office 7"
            },
            {
                OfficeId = "8",
                OfficeName = "Office 8"
            }
        }     
    }
}

I tried using some group by on Geography and Country. But I am not getting the required output.
I can use looping logic to get the result, but I want to avoid it and try something with linq.

Upvotes: 0

Views: 959

Answers (3)

Dan Dohotaru
Dan Dohotaru

Reputation: 3079

you could achieve the same result as Tim mentioned but instead of using the method syntax, rely on the query syntax for debatable better readability

var projection = from location in locations
                 group location by location.Geography into geographies
                 select new
                 {
                    Geography = geographies.Key,
                    Countries = from geography in geographies
                                group geography by geography.Country into countries
                                select new
                                {
                                    Country = countries.Key,
                                    States = from country in countries
                                            group country by country.State into states
                                            select new
                                            {
                                                State = states.Key,
                                                Offices = from state in states
                                                        select new
                                                        {
                                                            Id = state.Id,
                                                            Office = state.Office,
                                                        }
                                             }
                                }
                };

and the end result would look like this

enter image description here

Upvotes: 0

Drag and Drop
Drag and Drop

Reputation: 2734

GroupBy, has some overload that comes handy in those case:

var query = locations.GroupBy(
            x => x.Geography,
            (a, aGroup) => new
            {
                A = a,
                Items = aGroup.GroupBy(
                    x => x.Country,
                    (b, bGroup) => new
                    {
                        B = b,
                        Items = bGroup.GroupBy(
                            x => x.State,
                            (c, cGroup) => new
                            {
                                B = c,
                                Items = cGroup.Select(i => new {i.Id, i.OfficeName})
                            })
                    })
            });

It's a little bit clearer to read and write.
The Anonimised object can be replace by your own class if needed.

Upvotes: 0

Tim Schmelter
Tim Schmelter

Reputation: 460148

Something like this?

var allRegionGroups = allOfficeLocators
    .GroupBy(ol => ol.Geography)
    .Select(gGroup => new
    {
        GeographyName = gGroup.Key,
        Countries = gGroup
            .GroupBy(ol => ol.Country)
            .Select(cGroup => new
            {
                Country = cGroup.Key,
                States = cGroup
                  .GroupBy(ol => ol.State)
                  .Select(sGroup => new
                  {
                      State = sGroup.Key,
                      OfficeList = sGroup
                       .Select(ol => new { OfficeId = ol.id, ol.OfficeName })
                       .ToList()
                  })
                 .ToList()
            })
            .ToList()
    })
    .ToList();

How you can access all properties of the anonymous types:

foreach (var region in allRegionGroups)
{
    string geographyName = region.GeographyName;
    var allCountries = region.Countries;
    foreach (var c in allCountries)
    {
        string country = c.Country;
        var allStates = c.States;
        //  and so on...
    }
}

Upvotes: 4

Related Questions