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 8000597
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T15:50:55+00:00 2026-06-04T15:50:55+00:00

I am trying to dynamically re-structure some data to be shown in a treeview

  • 0

I am trying to dynamically re-structure some data to be shown in a treeview which will allows the user to select up to three of the following dimensions to group the data by:

Organisation
Company
Site
Division
Department

So for example, if the user were to select that they wanted to group by Company then Site then Division…the following code would perform the required groupings.

var entities = orgEntities
// Grouping Level 1
.GroupBy(o => new { o.CompanyID, o.CompanyName })
.Select(grp1 => new TreeViewItem
        {
        CompanyID = grp1.Key.CompanyID,
        DisplayName = grp1.Key.CompanyName,
        ItemTypeEnum = TreeViewItemType.Company,
        SubItems = grp1
               // Grouping Level 2
               .GroupBy(o => new { o.SiteID, o.SiteName })
               .Select(grp2 => new TreeViewItem
               {
               SiteID = grp2.Key.SiteID,
               DisplayName = grp2.Key.SiteName,
               ItemTypeEnum = TreeViewItemType.Site,
               SubItems = grp2
                  // Grouping Level 3
                  .GroupBy(o => new { o.Division })
                  .Select(grp3 => new TreeViewItem
                  {
                      DisplayName = grp3.Key.Division,
                      ItemTypeEnum = TreeViewItemType.Division,
                  }).ToList()
               }).ToList()
        })
.ToList();

This would give a structre like this:

+ Company A
  + Site A
    + Division 1
    + Division 2
  + Site B
    + Division 1
+ Company B
  + Site C
    + Division 2
+ Company C
  + Site D

However, this only provides me with on of a large number of combinations.

How would I go about converting this into something that could create the equivalent expression dynamically based on the three dimensions that the user has chosen and so I don’t have to create one of each of these expressions for each combination!!?

Thanks guys.

  • 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-04T15:50:57+00:00Added an answer on June 4, 2026 at 3:50 pm

    An intriguing problem. Choosing a single type for grouping keys and another type for results… makes it is very possible to get what you’re asking for.

    public struct EntityGroupKey
    {
      public int ID {get;set;}
      public string Name {get;set;}
    }
    
    public class EntityGrouper
    {
      public Func<Entity, EntityGroupKey> KeySelector {get;set;}
      public Func<EntityGroupKey, TreeViewItem> ResultSelector {get;set;}
      public EntityGrouper NextGrouping {get;set;} //null indicates leaf level
    
      public List<TreeViewItem> GetItems(IEnumerable<Entity> source)
      {
        var query =
          from x in source
          group x by KeySelector(x) into g
          let subItems = NextGrouping == null ?
            new List<TreeViewItem>() :
            NextGrouping.GetItems(g)
          select new { Item = ResultSelector(g.Key), SubItems = subItems };
    
        List<TreeViewItem> result = new List<TreeViewItem>();
        foreach(var queryResult in query)
        {
              // wire up the subitems
          queryResult.Item.SubItems = queryResult.SubItems 
          result.Add(queryResult.Item);
        }
        return result;
      }
    
    }
    

    Used in this way:

    EntityGrouper companyGrouper = new EntityGrouper()
    {
      KeySelector = o => new EntityGroupKey() {ID = o.CompanyID, Name = o.CompanyName},
      ResultSelector = key => new TreeViewItem
      {
        CompanyID = key.ID,
        DisplayName = key.Name,
        ItemTypeEnum = TreeViewItemType.Company
      }
    }
    
    EntityGrouper divisionGrouper = new EntityGrouper()
    {
      KeySelector = o => new EntityGroupKey() {ID = 0, Name = o.Division},
      ResultSelector = key => new TreeViewItem
      {
        DisplayName = key.Name,
        ItemTypeEnum = TreeViewItemType.Division
      } 
    }
    
    companyGrouper.NextGrouping = divisionGrouper;
    
    List<TreeViewItem> oneWay = companyGrouper.GetItems(source);
    
    companyGrouper.NextGrouping = null;
    divisionGrouper.NextGrouping = companyGrouper;
    
    List<TreeViewItem> otherWay = divisionGrouper.GetItems(source);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to have some kind of dynamically growing array/data structure in C.
I'm trying to define some data in a fixed-size structure in assembler. I'd like
I'm trying to dynamically load values into a select in a jqGrid. It almost
I'm trying to create a matrix data structure in C. I have a struct
I am trying to build a dynamically generated unordered list in the following format
I am trying to dynamically add some content to a list of checkboxes in
I am trying to load some data from a text file into a vector
I am trying to create a structure that can be dynamically added to with
I'm trying to dynamically get a databases Table structure using only C# code as
I am trying to load assemblies dynamically through Reflection. i have folder structure like

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.