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 Query
- How can I improve the readability of these linq statements?
- How can I write the following code more elegantly using LINQ query syntax?
- Optimizing LINQ queries - How can I improve the execution time?
- How can I check the number of calls to the database in LINQ query when using .NET Core and Code First?
- How can I improve the speed of this LINQ query?
- How I can improve the code using lambda expressions?
- How can I get LINQ to return the object which has the max value for a given property?
- How can I filter a dictionary using LINQ and return it to a dictionary from the same type
- How can a LINQ join select only the first record?
- How can you do custom sorting in LINQ with null always on the end?
- How can I trace the query produced by the documentdb Linq provider?
- How to understand the following C# linq code of implementing the algorithm to return all combinations of k elements from n
- How can I access the loop index inside a LINQ select?
- When using a LINQ Where clause on a Dictionary, how can I return a dictionary of the same type?
- How can you see the sql that is causing an error on SubmitChanges in LINQ to SQL?
- How can I get LINQ to return the index of the object which has the max value in a collection?
- How can I view the Entity Framework LINQ query plan cache?
- How does linq actually execute the code to retrieve data from the data source?
- How can I switch that code to use LINQ
- How can I use LINQ to calculate the longest streak?
- How can I make my Linq select return values if the value selected is null?
- How can I group by the difference between rows in a column with linq and c#?
- How can I combine this code into one or two LINQ queries?
- How can i improve the performance of this LINQ?
- Linq - How can a add the contents of two list
- How can I compare two sets of data in linq and return the common data?
- How can I further simplify this piece of LINQ code
- How can I code an outer join using LINQ and EF6?
- How I can pass the LINQ result anonymous type to another method?
- How can I get the highest number in this Linq result?
More Query from same tag
- Why is Entity Framework returning null?
- Build dynamic LINQ queries from a string - Use Reflection?
- Validating Array to Array Exist in Lambda or Linq
- Should I use two "where" clauses or "&&" in my LINQ query?
- Table into Tree Structure c#
- Update Datatable with another
- Linq search several properties of a class for a string match where some of the properties are a list of another class
- Convert LINQ with Model To Array
- Extend a list using last element to match longer list in LINQ zip
- Count of distinct with linq to entities and custom IEqualityComparer
- Why is GroupBy multiple fields not grouping correctly>
- Linq Where condition where there's possibility of a null reference
- Filter the "Includes" table on Entity Framework query
- Remove duplicates from a ListView column cast to List
- Object Initializer syntax to produce correct Json
- Adding new entry to DataContext db Cause null reference
- entity framework linq query, get top N postIDs from favorite's table based on count
- ObjectMaterialize in EF not firing on first level query
- Linq To SQL Select Dynamic Columns
- Convert a Method Syntax to its corresponding version in Linq Syntax
- Linq JS join syntax
- Lambda Expression - is it evaluating correctly?
- c# convert predicate between each other
- Converting SQL to Linq
- Convert DataTable to LINQ: Unable to query multiple fields
- Check if date is this date or bigger
- NotSupportedException: Could not convert constant 1 expression to string
- Mapping Two Classes Properties and Values Linq
- check whether a List<string> contains an element in another List<string> using LINQ
- How to create nested object graphs using Linq when the objects are immutable and reference their parent