Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# + EntityFramework: Convert multiple group by query to nested JSON

I made the following linq statement

C#

var list = from row in repository.GetAllEntities()
                       group row by new { row.RegionString, row.SubRegionString, row.CountryString } into g
                       select new { g.Key.RegionString, g.Key.SubRegionString, g.Key.CountryString, Count = g.Count() };

return Json(list, JsonRequestBehavior.AllowGet);

That returns

[
  {
    "RegionString":"Americas",
    "SubRegionString":"",
    "CountryString":"",
    "Count":2
  },
  {
    "RegionString":"Americas",
    "SubRegionString":"NorthAmerica",
    "CountryString":"Canada",
    "Count":5
  },
  {
    "RegionString":"Americas",
    "SubRegionString":"NorthAmerica",
    "CountryString":"US",
    "Count":3
  },
  {
    "RegionString":"Americas",
    "SubRegionString":"SouthAmerica",
    "CountryString":"Chile",
    "Count":3
  },
  {
    "RegionString":"EMEA",
    "SubRegionString":"AsiaPacific",
    "CountryString":"Australia",
    "Count":2
  },
  {
    "RegionString":"EMEA",
    "SubRegionString":"AsiaPacific",
    "CountryString":"Japan",
    "Count":1
  },
  {
    "RegionString":"EMEA",
    "SubRegionString":"SouthernEurope",
    "CountryString":"Turkey",
    "Count":1
  },
  {
    "RegionString":"EMEA",
    "SubRegionString":"WesternEurope",
    "CountryString":"",
    "Count":1
  }
]

But I am trying to make it into this format

[{
    name: "Americas",
    children: [
     {
         name: "NorthAmerica",
         children: [{ "name": "Canada", "count": 5 },
                    { "name": "US", "count": 3 }]

     },
     {
         name: "SouthAmerica",
         children: [{ "name": "Chile", "count": 1 }]
     },
    ],
},
{
    name: "EMA",
    children: [
     {
         name: "AsiaPacific",
         children: [{ "name": "Australia", "count": 2 },
                    { "name": "Japan", "count": 1 }]

     },
     {
         name: "SouthernEurope",
         children: [{ "name": "Turkey", "count": 1 }]
     },
    ],
}]

How could I modify the statement to get the result I'm looking for? Non linq answers are acceptable too.

EDIT: Region is the parent of SubRegion, SubRegion is the parent of Country and Count is the unique number of items that belongs to Country

like image 900
tcigrand Avatar asked Jun 25 '15 20:06

tcigrand


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr. Stroustroupe.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

How old is the letter C?

The letter c was applied by French orthographists in the 12th century to represent the sound ts in English, and this sound developed into the simpler sibilant s.


1 Answers

Here is the linq query you want (I've removed the -String postfix for readability):

var list =
    from entity in repository.GetAllEntities()
    group entity by entity.Region into regions
    let childrenOfRegions =
        from region in regions
        group region by region.SubRegion into subregions
        let countriesOfSubRegions =
            from subregion in subregions
            group subregion by subregion.Country into countries
            select new { Name = countries.Key }
        select new { Name = subregions.Key, Children = countriesOfSubRegions }
    select new { Name = regions.Key, Children = childrenOfRegions };  

There is no Count in this query, since I don't really understand what you are counting.

What I'm doing here is grouping the rows into Regions and in the last line you can see the
select new { Name = regions.Key, ... } part where I'm returning the Regions.
To get the children, you need to group the Regions into SubRegions (the same way with Regions). You repeat this all the way down to the Countries and you're done.

like image 191
QuantumHive Avatar answered Nov 15 '22 01:11

QuantumHive