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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T02:56:23+00:00 2026-05-17T02:56:23+00:00

I am trying to populate a tree view based on a list of staff

  • 0

I am trying to populate a tree view based on a list of staff from various deparments (lstStaff).

public class Staff
{
    public int StaffID { get; set; }
    public string StaffName { get; set; }
    public int DeptID { get; set; }
    public string DepartmentName { get; set; }
}

The result should be like this:

Department A

  • John
  • Dave

Department B

  • Andy
  • Leon

I have written the following code. However, I think the code should be refactored further.

RadTreeNode deptNode;
RadTreeNode staffNode;
var departments = (from d in lstStaff
                   where !string.IsNullOrEmpty(d.DepartmentName) 
                select new { d.DeptId, d.DepartmentName })
                .Distinct();

foreach (var d in departments)
{
    //add department node
    deptNode = new RadTreeNode()
    {
        Value = d.DeptId,
        Text = d.DepartmentName
    };
    tvwDepartmentCases.Nodes.Add(deptNode);

    //add staff nodes to department node
    var staffs = lstStaff
                    .ToList()
                    .Where(x => x.DeptId == d.DeptId);

    foreach (var s in staffs)
    {
        staffNode = new RadTreeNode()
        {
            Value = s.StaffID,
            Text = s.StaffName
        };
        deptNode.Nodes.Add(staffNode);
    }
}

Any comments are welcomed. Thanks!

  • 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-05-17T02:56:24+00:00Added an answer on May 17, 2026 at 2:56 am

    Use the GroupBy operator, it’s easy and looks so much nicer.

    var departmentGroups = lstStaff.GroupBy(staff => 
        new { staff.DeptID, staff.DepartmentName });
    
    foreach (var department in departmentGroups)
    {
        var deptNode = new RadTreeNode()
        {
            Value = department.Key.DeptID,
            Text = department.Key.DepartmentName
        };
    
        tvwDepartmentCases.Nodes.Add(deptNode);
    
        foreach (var staffMember in department)
        {
            var staffNode = new RadTreeNode()
            {
                Value = staffMember.StaffID,
                Text = staffMember.StaffName
            };
            deptNode.Nodes.Add(staffNode);
        }
    }
    

    It’s also lower complexity because you don’t need to iterate through the entire collection of lstStaff for each department.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to populate the list li from the LoginAction on to the
I'm trying to populate a class object with values from a database table. The
I am trying to prepare the tree view control from folowing strings. US|New York|D.M.Street
I'm trying to populate a mx:tree component with values that I'm getting from BlazeDS.
Im trying to populate a viewmodel based on the value from the querystring. Heres
I trying to populate the dropdown list when page was loaded.But it is not
I am trying to populate a List<Image> by reading the name of the each
I am trying to populate a dropdown list when some other drop down gets
I am trying to populate the textbox inside a usercontrol with value from the
I am trying to implement a tree view in my application. I am using

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.