assumed i have a class hierarchy where basically every class consists of a property item (like string) and a property list item of the nested class:
public class Master
{
public String myName;
public List<Detail> myDetails;
}
public class Detail
{
public String myDetailDescription;
public List<DetailParts> parts;
}
public class DetailParts
{
public int PartNumber;
public String PartDescription;
}
Now i would like to have a linq query that gives me an list of objects that consists of these elements
public class Result
{
public String myName; // from Master
public String myDetailDescription; // from Detail
public int PartNumber; // from DetailParts
}
It is a silly question, I know, but i cant find useful links...
[Edit #1]: Better code sample:
public class Master
{
public String myName;
public List<Detail> myDetails = new List<Detail>();
}
public class Detail
{
public String myDetailDescription;
public List<DetailParts> parts = new List<DetailParts>();
}
public class DetailParts
{
public int PartNumber;
public String PartDescription;
}
// List of Masters
List<Master> master = new List<Master>();
// Two detail parts for the Details
DetailParts dp1 = new DetailParts { PartDescription = "dp1" };
DetailParts dp2 = new DetailParts { PartDescription = "dp2" };
// once again two details for the Master
Detail d1 = new Detail { myDetailDescription = "d1" };
Detail d2 = new Detail{ myDetailDescription = "d2"};
// Assign the Parts
d1.parts.Add(dp1);
d1.parts.Add(dp2);
d2.parts.Add(dp1);
d2.parts.Add(dp2);
Master m1 = new Master { myName = "m1" };
Master m2 = new Master { myName = "m2" };
m1.myDetails.Add(d1);
m1.myDetails.Add(d2);
m2.myDetails.Add(d1);
m2.myDetails.Add(d2);
master.Add(m1);
master.Add(m2);
// given a value for `myName` and `myDetailDescription` i
// would be able to get a list with the `DetailParts` objects:
var t = master.Where(a => a.myName == "m2")
.Select(a => a.myDetails).FirstOrDefault()
.Where(b => b.myDetailDescription == "d1")
.Select(c => c.parts).FirstOrDefault();