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

  • Home
  • SEARCH
  • 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 6001341
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T00:49:15+00:00 2026-05-23T00:49:15+00:00

I have a hierarchical data tree containing objects of a class called DataNode .

  • 0

I have a hierarchical data tree containing objects of a class called DataNode.

Each DataNode contains a collection of Attribute objects. Each Attribute is essentially a key/value pair, with some helper methods attached. For example, there is a helper method called EqualsCodeIndex(x) that matches a small collection x of int values to this attribute and returns true or false. All keys and values are strings, because the whole thing is based on a key/value store contained in a text file.

To simplify access to a particular DataNode, there is a dictionary in the DataTree class that maps all of the nodes in the tree to a unique code:

Dictionary<string, DataNode> Codes;

The resulting Linq statement to get to a specific Attribute value looks like this:

string AttributeValue = dataTree
    .Codes[@"R-1\CHE"]
    .Attributes
    .Single(x => x.EqualsCodeIndex(parentAttribute.CodeIndex))
    .Value.Trim();

This is not too bad if I only have to retrieve one or two attributes by code and code index, but it’s not so good if I have to retrieve ten or more.

To attempt to simplify the statement, and allow for the possibility of EqualsCodeIndex returning false for all attributes in the collection, I added an extension method:

public static string AttributeValueMatching
    (this KeyValuePair<string, DataNode> pair, List<int> codeIndex)
{
    var attribute = pair.Value.Attributes
        .Single(x => x.EqualsCodeIndex(codeIndex))

    return attribute == null ? string.Empty : value;
}

This simplifies the original linq statement down to:

string attributeValue
    = dataTree.Codes[@"R-1\CHE"].AttributeValueMatching(codeIndex);

…which is better, but I have a feeling I’m missing something.


Are there problems with this approach? Is there a better, cleaner approach I haven’t thought of, maybe making better use of indexers, or perhaps a fluent interface?

  • 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-23T00:49:16+00:00Added an answer on May 23, 2026 at 12:49 am

    I think that making it into one method with two parameters would look slightly better:

    Codes.AttributeValueMatching(@"R-1\CHE", codeIndex)
    

    Or you could create a wrapper with an indexer:

    CodesWrapper[@"R-1\CHE", codeIndex]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some hierarchical data - each entry has an id and a (nullable)
I have a C++ class representing a hierarchically organised data tree which is very
I have an issue. I have hierarchical XML data such as: <Tree> <Node Text=Stuff
Let's assume we have a method handling operations in a tree hierarchical data structure
I have hierarchical ordered data saved using the modified preorder tree traversal algorithm. Here's
I have got an array that contains data in hierarchical form such as: Level
The following scenario came up in our project: We have hierarchical business objects. The
I have a hierarchical data structure which I'm displaying in a webpage as a
I'm working on a project where I have some hierarchical data that I want
I have a MySQL database holding hierarchical data using the Closure Table method. A

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.