score:1
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...
Source: stackoverflow.com
Related Articles
- How can I improve the readability of these linq statements?
- LINQ Source Code Available
- Why do these three pieces of LINQ code produce different (or erroneous) results?
- Opportunities to use Func<> to improve code readability
- How to rewrite these LINQ statements into one query
- Is there a way to combine these two LINQ statements into one
- creating Linq to sqlite dbml from DbLinq source code
- Code Rewite for tuple and if else statements by using LINQ
- How to assign multiple LINQ Include() statements to a variable for code re-use?
- LINQ Where(): are these two LINQ statements the same?
- source code for LINQ 101 samples
- Is there a way to simplify these linq statements using an .Include()?
- c# Linq or code to extract groups from a single list of source data
- Convert string[] to int[] in one line of code using LINQ
- Code equivalent to the 'let' keyword in chained LINQ extension method calls
- Linq code to select one item
- FindAsync and Include LINQ statements
- How are people unit testing code that uses Linq to SQL
- C# - LINQ Statements with OR clauses
- Why are these linq outputs different?
- Is multiple .Where() statements in LINQ a performance issue?
- Best practices for dealing with LINQ statements that result in empty sequences and the like?
- Which LINQ statements force Entity Framework to return from the DB?
- LINQ query to perform a projection, skipping or wrapping exceptions where source throws on IEnumerable.GetNext()
- Implementing conditional 'if' statements within a LINQ queries 'where' statement
- Use reflection to make dynamic LINQ statements in C#
- Syntax to execute code block inside Linq query?
- How do I load these LINQ results into my ViewModel class?
- Enumerable.Empty<T>().AsQueryable(); This method supports the LINQ to Entities infrastructure and is not intended to be used directly from your code
- Can someone explain why these two linq queries return different results?
- switch case inside linq query
- Multiple Where Linq Query
- Linq to SQL The best Overloaded method Match error
- C#/Linq get sets with adjacent
- Return items in one list based upon their matching index in another using Linq
- Ordering a list based on multiple factors
- Linq query that involves doing group by with two tables that are not directly connected
- C# Linq list with list in datagrid
- SelectMany - project to new type?
- LINQ to XML how do I get child element value with multiple namespace
- Object mapping with LINQ and SubSonic
- How to use linq include function in generic type
- Find possible variations of one item
- Putting two lists together in C# using LINQ
- Use or in entity framework
- Extracting data packet out of byte buffer
- How do you perform a left outer join with a where clause using linq lambda extension methods
- Edit and save element to XML file using Linq
- LINQ to Entities does not recognize the method 'System.DateTime ConvertShamsiToMiladi(System.String)' method
- C# Linq-to-SQL multiple columns