C# how do I implement inheritance using linq?
I have two lists based on the same class. I would like on开发者_Python百科e list to inherit the values of the other list's class properties.
class feature {
Guid id;
string featureName;
string featureType;
.....
}
List <feature> companyFeatures;
List <feature> divisionFeatures;
The list of features is set at the company level.
When a division is created it should inherit all of the features from the company feature list. (Not copy)If the division has a different property value in a specific feature than that of the company , the user wants to "save" this property's value in the division's feature list.
This gives the user the ability to add features at a later date at the company level and see these new features at the division level.
Usually there will not be many items in the division list as they are the same as the company list.
I have tried to join both lists using linq (an outer join). It seems to fail when there is no entry for a list element at the division level.
I guess I am doing something wrong (I might not understand outer joins correctly) Any help will be appreciated.
By the way is there a design pattern recommended for implementing inheritance.
Be happy and have a wonderful day
================================================================================
Adding an example (Hopefully this will clarify my objective)
List <feature> companyFeatures = new List <feature>
{
new <feature> { Guid1 , "Color" , "Text" , .... },
new <feature> { Guid2 , "address" , "Link" , .... },
new <feature> { Guid3 , "logo" , "Picture" , .... }
}
List <feature> divisionFeatures = new List <feature>
{
new <feature> { Guid2 , "address" , "text" , .... },
}
The feature list I am looking for after the "inheritance" should be:
{
{Guid1, "Color" , "Text" , ...},
{Guid2, "address" , "text" , ...},
{Guid3, "logo" , "Picture" , ...}
}
Note that Guid2 now has a value of text for the type property. Hope this clarifies.
This isn't, technically, inheritance -
If the goal here is to return a collection of all features in the companyFeatures
and divisionFeatures
lists, then you can just do:
IEnumerable<feature> totalFeatures = companyFeatures.Concat(divisionFeatures);
The Enumerable.Concat method will return an IEnumerable<T>
which contains the elements from both lists.
Correct me if I misunderstood. You have a Company
class and a Division
class and they both have a List<Feature>
field? Something like...
public class Company {
// ...
public List<Feature> CompanyFeatures;
// ...
}
public class Division {
public Company Company; // The company to which the division belongs
// ...
public List<Feature> DivisionFeatures;
// ...
}
If this is the case, then I would suggest to turn the feature list into a read-only property and have it automatically take care of the “inheriting”. For exapmle...
public class Division {
public Company Company; // The company to which the division belongs
// ...
private List<Feature> _divisionFeatures; // private now
public IEnumerable<Feature> DivisionFeatures {
get {
// Take all the features for the divison...
return _divisionFeatures.Concat(
// ... and add all the company features except for those that
// have the same name as one of the division features.
Company.CompanyFeatures.Where(cf => !_divisionFeatures
.Any(df => df.Name == cf.Name))
);
}
}
// ...
}
Alternatively, of course you can still make the List<Feature>
public (so that you can still manipulate it from outside) and call the property something like DivisionFeaturesWithInheritance
.
精彩评论