0

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();

1 Answer 1

4

You are looking for SelectMany:

List<Result> results = masterList
    .SelectMany(m => m.myDetails
        .SelectMany(d => d.parts
            .Select(dp => new Result
            {
                PartNumber = dp.PartNumber,
                myDetailDescription = d.myDetailDescription,
                myName = m.myName
            })))
    .ToList();

A more readable version with LINQ's query syntax:

var resultQuery = from m in masterList
                  from d in m.myDetails
                  from dp in d.parts
                  select new Result
                  {
                    PartNumber = dp.PartNumber,
                    myDetailDescription = d.myDetailDescription,
                    myName = m.myName
                  };
List<Result> results = resultQuery.ToList();
Sign up to request clarification or add additional context in comments.

2 Comments

Whoha... you are quite fast :-) Will try this with my real class space.
I tried this with my classes and it worked - even though I forgot to mention, that i need some where clauses ;-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.