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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T10:42:55+00:00 2026-06-16T10:42:55+00:00

I know this must be real easy but I just can’t get it to

  • 0

I know this must be real easy but I just can’t get it to work… I am trying to make a List for extjs Tree by comparing two columns from differnt rows and putting them as a Node or a leaf accordingly. this is my sample data

ListA  ListB  labelName
 NY           Parent1
        NY    Leaf1
 HI           Parent2
 AK           Parent3

and this is my c# end… so when I match NY, I am supposed to have Parent1 as node and Leaf1 as its leaf and not for HI or AK… but doing this throws me all data as Parent.. even the leaf.

            SqlCommand cmd = con.CreateCommand();

            comd.CommandText = "SELECT * FROM myTable";
            con.Open();
            SqlDataReader reader = comd.ExecuteReader();
            while (reader.Read())
            {
                City MyData = new City();

                MyData.ListA = reader["ListA"].ToString().Trim();
                MyData.ListB = reader["ListB"].ToString().Trim();
                MyData.labelName = reader["labelName"].ToString().Trim();
                giveData.Add(MyData);
            }

            int count = 1;

            List<TreeNode> myNode = new List<TreeNode>();
            foreach (City MyData in giveData)
            {
                // 1st foreach
                    if (MyData.ListA != "")
                    {

                        TreeNode treeNode = new TreeNode();
                        treeNode.id = count++;
                        treeNode.name = MyData.labelName;
                        treeNode.leaf = false;

                        List<TreeNode> Level1 = new List<TreeNode>();
                        foreach (City labelName  in giveData)
                        {
                            if (labelName.ListA == labelName.ListB)
                            {// 2nd foreach
                                TreeNode node1 = new TreeNode();
                                node1.id = count++;
                                node1.name = labelName.labelName;
                                node1.leaf = true;

                                Level1.Add(node1);
                            }
                        }

                        treeNode.children = Level1;
                        myNode.Add(treeNode);
                }
            }
            return JsonConvert.SerializeObject(myNode);

Should I use array to store each record and compare them instead… I am out of ideas… I am sure there is a better way to accomplish this… Please help

  • 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-16T10:42:56+00:00Added an answer on June 16, 2026 at 10:42 am

    Assuming the data is the way you state and the “parents” will come before any leaves, here’s a single pass way I came up with to create the tree:

    [Irrelevant Code Snipped]

    Update: LINQ using Dictionary<string, List<TreeNode>>

    I created a new class TreeNode and some sample data for testing:

    var MyData = new List<City>
                      {
                         new City {ListA = "AK", ListB = "", labelName = "Alaska"},
                         new City {ListA = "HI", ListB = "", labelName = "Hawaii"},
                         new City {ListA = "", ListB = "HI", labelName = "Hawaii Leaf 1"},
                         new City {ListA = "", ListB = "HI", labelName = "Hawaii Leaf 2"},
                         new City {ListA = "NY", ListB = "", labelName = "New York"},
                         new City {ListA = "", ListB = "NY", labelName = "New York Leaf 1"},
                         new City {ListA = "", ListB = "NY", labelName = "New York Leaf 2"}
                      };
    

    Here’s the new method that basically creates 2 lists, 1 for the parents and 1 for the leaves. I then loop through the leaves to find any matching parents and add the leaves to it:

    var index = 0;
    var parents = (from p in MyData
                   where p.ListB == ""
                   select p).ToDictionary(p => p.ListA, p => new TreeNode { id = index++, name = p.labelName, leaf = false });
    
    var leaves = (from l in MyData
                  where l.ListA == ""
                  group l by l.ListB into stateGroup
                  select stateGroup).ToDictionary(g => g.Key, g => g.ToList());
    
    foreach (var leaf in leaves.Where(leaf => parents.ContainsKey(leaf.Key)))
    {
        parents[leaf.Key].children =
            leaf.Value.Select(l => new TreeNode {id = index++, name = l.labelName, leaf = true}).ToList();
    }
    
    var myNode = parents.Select(p => p.Value).ToList();
    
    return JsonConvert.SerializeObject(myNode);
    

    I think this should be more efficient than using lists and the List.Find()

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

Sidebar

Related Questions

I know this is partially subjective, but hopefully with enough specifics I can get
I know this must be asked a millions times and can't be easy to
This must be real easy but apparently its not! I was able to successfully
I know this must be a n00b question, but I have to implement a
I know this code use to work, I must have accidentally changed something when
I know there must be a really simple answer to this question, but I
I know SQL well but I must be missing something really dumb here. This
I tried searching for information on this, but must not know the right terms
I'm just to figure out what does this method do, I know there must
I know that this question must have been asked and answered a million times,

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.