score:1

Accepted answer

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]

score:1

I don't have a direct answer to your whole question, but to the "problems with this approach" section I have a suggestion.

Be careful chaining statements after a SingleOrDefault() as it could potentially return null. If you are absolutely sure it will always have a single value, maybe just call Single() and deal with that missed expectation should it ever happen instead of a more generic NullReferenceException.

EDIT While writing the above post you made the same changes. Carry on...

score:1

Have you considered building an Extension Method of DataTree?

like

public static class DataTreeExtensions
   {
      public static string FetchByAttribute(this DataTree d, string Attribute)
      {
         string AttributeValue = d
                         .Codes[Attribute]
                         .Attributes
                         .Single(x => x.EqualsCodeIndex(parentAttribute.CodeIndex))
                         .Value.Trim();

            return AttributeValue

      }
   }

This will allow you to reuse "FetchByAttribute" at will as:

string myValue = myTree.FetchByAttribute(@"R-1\CHE");

Edited: changed from DataNode to DataTree...


Related Articles