Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8591567
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T23:33:26+00:00 2026-06-11T23:33:26+00:00

I have a table like this (Groups): ID Name ParentID 1 Group 1 null

  • 0

I have a table like this (Groups):

ID       Name           ParentID
1        Group 1        null
2        Group 2        null
3        SubGr 1-1      1
4        SubGr 1-2      1
5        SubGr 2-1      2
6        Group 3        null
7        SubGr 1-2-1    4
..... and so on

I want to serialize this to JSON looking like this:

[{"id":1,
  "name":"Group 1",
  "children": [
    {
     "id":3,
     "name":"SubGr 1-1",
     "children":null
    },{
     "id":4,
     "name":"SubGr 1-2",
     "children": [
       {
        "id":7,
        "name":"SubGr 1-2-1", 
        "children": null
       }
      ]
    }
   ]
  },
 {"id":2,
  "name":"Group 2",
  "children": [
    {
     "id":5,
     "name":"SubGr 2-1",
     "children":null
    }
   ]
 },
 {"id":6,
  "name": "Group 3",
  "children": null
 }
]

As you can see, you can have indefinite subgroups.

How can I make such a query in LINQ and output it in JSON like the example above?

I have no problem outputting the JSON as seperated elements, with ParentID, but I need to have the structure as mentioned above.

This is the code that I am currently working with, after trying different things around, but with no luck still (this version only gives two levels):

    public ActionResult GetGroups()
    {
        var groupobjs = db.GroupObjs.ToList();

        var items = groupobjs.Where(p => p.ParentID == null).Select(p => new
        {
            id = p.ID,
            name = p.Name,
            children = groupobjs.Where(c => c.ParentID == p.ID).Select(c => new {
                id = c.ID,
                name = c.Name
            })
        });



        return Json(items, JsonRequestBehavior.AllowGet);
    }      
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T23:33:27+00:00Added an answer on June 11, 2026 at 11:33 pm

    I was working on some code similar to what @Hunter-974 recommended.

    public class Group
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public int? ParentId { get; set; }
        public List<Group> Subgroups { get; set; }
    
        public Group()
        {
            this.Subgroups = new List<Group>();
        }
    }
    
    class Program
    {
        static void Main()
        {
            Group[] groups = new Group[]
            {
                new Group { Id = 1, Name = "Group 1", ParentId = null },
                new Group { Id = 2, Name = "Group 2", ParentId = null },
                new Group { Id = 3, Name = "SubGr 1-1", ParentId = 1 },
                new Group { Id = 4, Name = "SubGr 1-2", ParentId = 1 },
                new Group { Id = 5, Name = "SubGr 2-1", ParentId = 2 },
                new Group { Id = 6, Name = "Group 3", ParentId = null },
                new Group { Id = 7, Name = "SubGr 1-2-1", ParentId = 4 }
            };
    
            foreach (Group g in groups)
                if (g.ParentId.HasValue)
                    groups.Single(group => group.Id == g.ParentId.Value).Subgroups.Add(g);
    
            var rootgroups = groups.Where(g => g.ParentId == null);
    
            JavaScriptSerializer js = new JavaScriptSerializer();
            Console.WriteLine(js.Serialize(rootgroups));
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table like this: name code group john 12 smith 15 how
I have a table like this ID Name IsDeleted 1 Yogesh Null 2 Goldy
I have a table that looks like this Group Recipe Priority 0 A 400
I have a table like this one: id group value 1 GROUP A 0.641028
I have a database application in which a group is modeled like this: TABLE
I have a table like this : +-------+------------+------+-----+---------+-------+ | Field | Type | Null
I have a table like this: CREATE TABLE book_info ( book_id VARCHAR(32) not null,
I have this table in my DB: Group - ID-Name - 1 -abc -
I have table like this : ID Name_1 Name_2 Name_3 1 Egon Spengler Ives
I have a table like this: ID country ------------- 1 US 2 Japan 3

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.