Reputation: 519
How do I create an object that has properties with multiple related properties that have multiple related properties and so on?
I'm creating a C# class that will describe toe disease on a various colonies of tigers.
One colony has many tigers. 1 tiger has 4 paws. 1 paw has 5 toes. Each toes is "infected" or "not infected".
How can I construct an object that will retrieve diseased status for the colony 4's tiger named Eddy's front-right paw's second toe? e.g.
Tiger t = new Tiger;
string toeStatus = t.colony(4).tiger("Eddy").paw("front-right").toe(2)
I've only every had to create objects that have properties unto themselves, not have properties that relate to properties that relate to properties.
I'd also like to re-use this object to get the names of all the names of all the tigers for a given colony...
string allTigersNames = t.colony(4).tiger(*)
//Eddy, Glennis, Barry, Toby, Punter
I've asked in an analogous similar post and have been given a response that I don't understand at my present experience level. Particularly the approach to use strongly typed properties.
Upvotes: 0
Views: 73
Reputation: 1803
public class Colony
{
public int ColonyId {get; set;}
public string ColonyName {get; set;}
public List<Tiger> Tigers {get; set;}
}
public class Tiger
{
public int TigerId {get; set;}
public string TigerName {get; set;}
public Colony Colony {get; set;}
public List<Paw> Paws {get; set;}
}
public class Paw
{
public int PawId {get; set;}
public string PawDescription {get; set;}
public Tiger Tiger {get; set;}
public List<Toe> Toes {get; set;}
}
public class Toe
{
public int ToeId {get; set;}
public Paw Paw {get; set;}
public int ToeFinger {get; set;}
}
this would be my hierarchy if you will ask me
Colony > Multiple Tigers
Tiger > Multiple Paws
Paw > Multiple Toes
then adding would look like these
var primaryColony = new Colony
{
ColonyId = 1,
ColonyName = "First Colony",
Tigers = new List<Tigers>();
};
var colonies = new List<Colonies>();
//adding primary colony
colonies.Add(primaryColony);
//adding tiger to primary colony via name or you could use ColonyId == 1
colonies.First(c => c.ColonyName == "First Colony")
.Tigers.Add(new Tiger
{
TigerId = 1,
TigerName = "Eddy",
Paws = new List<Paw>();,
Colony = primaryColony
});
//adding paws to tiger named eddy in colony named primary colony
colonies.First(e => e.ColonyName == "First Colony")
.Tigers.First(r => r.TigerName == "Eddy")
.Paws.Add(new Paw
{
PawId = 1,
PawDescription = "right-front",
Toes = new List<Toes>();,
Tiger = colonies.First(e => e.ColonyName == "First Colony")
.Tigers.First(r => r.TigerName == "Eddy")
});
//adding toes to right front paw of tiger named eddy in colony named first colony
colonies.First(e => e.ColonyName == "First Colony")
.Tigers.First(r => r.TigerName == "Eddy")
.Paws.First(q => q.PawDescription == "right-front")
.First().Toes.Add(new Toe
{
ToeId = 1,
ToeFinger = 1,
Paw = colonies.First(e => e.ColonyName == "First Colony")
.Tigers.First(r => r.TigerName == "Eddy")
.Paws.First(q => q.PawDescription == "right-front")
});
or if you have a filter, like you want paws to add to TigerName = Eddy
var tigerEddy = colonies.Select(y => new Tiger
{
TigerId = y.Tigers.FirstOrDefault?.(c => c.TigerName == "Eddy").Select(e => e.TigerId),
TigerName = y.Tigers.FirstOrDefault?.(c => c.TigerName == "Eddy").Select(e => e.TigerName),
Colony = y.Tigers.FirstOrDefault?.(c => c.TigerName == "Eddy").Select(e => e.Colony)
Paws = y.Tigers.FirstOrDefault?.(c => c.TigerName == "Eddy").Select(e => e.Paws)
}.Any(x => x.Tigers.Containes("Eddy").FirstOrDefault();
tigerEddy.Paws.Add(new Paws.....);
then pass it back to colonies with TigerId
as the reference
Upvotes: 1