1

I have an instance object of Division class.

Division object holds a list of Branch objects.
Each branch object holds a list of Department objects.
Each Department object holds list of Team objects.

just to make it clear I can do that:

int MakeItClear = DivisionObject.ListOfBranches[5]
                                .ListOfDepartments[4]
                                .ListOfTeam[3]
                                .SomeIntegerProperty;

Each object has ID and Name properties

My wish is create a function that will return a list of team names by passing a parameter of Branch.ID using LINQ.

basically I want to do that using LINQ:

public static List<string> getBranchTeamNames(ref Sales.Division obj, int BranchID)
{
    List<string> result = new List<string>();
    foreach (Sales.Branch b in obj.allBranches)
    {
        if (b.branchID == BranchID)
        {
            foreach (Sales.Department d in b.allDepartmentsManagers)
            {
                foreach (Sales.Team t in d.allTeams)

                {
                    result.Add(t.teamName);
                }
            }

             break;
        }          
    }
    return result;
}

i will be happy for some guidance, does not matter if its c# or vb.net.

thank you for your time and consideration.

1
  • 4
    You want SelectMany Commented Sep 2, 2016 at 20:09

2 Answers 2

4

Try this:

return obj.allBranches
    .Where(x => x.branchID == BranchID)
    .SelectMany(x => x.allDepartmentsManagers)
    .SelectMany(x => x.allTeams)
    .Select(x => x.teamName)
    .ToList()
Sign up to request clarification or add additional context in comments.

Comments

0

You can use SelectMany

var teamNames = division.Branches.First(branch => 
branch.Id.Equals(branchId)).Departments.SelectMany(department => 
department.Teams.Select(team => team.Name));

Comments

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.