This situation may be far more complicated than I want it to be, but I am bringing it forth anyway. This is for a game-type design.
The values here are hard coded, but that will not be the case in a real environment.
Basically, there are lists of classes known as Interpreter that contain the appropriate information to translate other things across the program engine. There is a default ‘layout’ for these, but then in many special cases, some of them need to be overridden.
One solution is to just have every instance in every list (this is doable, but I really think it’s redundant and I think a cleaner solution exists) Instead I am wanting to ‘combine’ them, but be able to specify a property to use as an “override”. (full source code available on pastie. I would put it here, but I have been told that putting that much code deters people from answering my questions) : http://www.pastie.org/1276064
public class Program
{
static void Main()
{
var traits = new List<Trait>
{
new Trait { Name = "Intellect" },
new Trait { Name = "Strength" },
new Trait { Name = "Constitution" }
};
var scores = new List<Score>
{
new Score { Name = "Beginner", Rank = 1 },
new Score { Name = "Adept", Rank = 2 },
new Score { Name = "Expert", Rank = 3 },
new Score { Name = "Master", Rank = 4 }
};
// one sheet will have defaults
var initial = new Sheet
{
Interpreters = new List<Interpreter>
{
new Interpreter
{
Trait = traits.Single( s => s.Name == "Intellect" ),
Score = scores.Single( s => s.Rank == 1 ),
Requirement = 10
},
new Interpreter
{
Trait = traits.Single( s => s.Name == "Intellect" ),
Score = scores.Single( s => s.Rank == 2 ),
Requirement = 20
},
new Interpreter
{
Trait = traits.Single( s => s.Name == "Intellect" ),
Score = scores.Single( s => s.Rank == 3 ),
Requirement = 30
}
}
};
// other sheets will override some or all of the default
var advanced = new Sheet
{
Interpreters = new List<Interpreter>
{
new Interpreter
{
Trait = traits.Single( s => s.Name == "Intellect" ),
Score = scores.Single( s => s.Rank == 2 ),
Requirement = 15
},
new Interpreter
{
Trait = traits.Single( s => s.Name == "Intellect" ),
Score = scores.Single( s => s.Rank == 4 ),
Requirement = 35
}
}
};
// combined sheet should have values of default, with the appropriately 'overridden' values of the advanced
}
In this situation, the combined list should read like…
[0]
Trait = Intellect,
Score = 1,
Requirement = 10
[1]
Trait = Intellect,
Score = 2,
Requirement = 15
[2]
Trait = Intellect,
Score = 3,
Requirement = 30
[3]
Trait = Intellect,
Score = 4,
Requirement = 35
I do know how I could achieve it with this specific instance of course. I can simply write a method that checks the value of the score, etc. But I want a more convention based approach that I can use in a bit more complicated manner. Does anyone have any ideas?
To get the result you described you could use a method like this:
I may have the logic wrong for the case when one
Sheetcontains differentTraits, but you didn’t specify how do you want to combine them.Also, the design of your code is quite odd. Not every collection has to be a
List<T>. Especially in this case, where usingDictionary<K,V>would be much cleaner (and faster) than usingSingle().EDIT
For your updated version, I like
List-based solution more than a LINQ one:EDIT 2
Here is the LINQ solution you asked for: