1

I am trying to add a list of List of Object and an object inside another list, for example;

public Class TeamRoles
{
    public Team Team{get;set;}
    public IEnumerable<Role> Roles{get;set;}
}

I am looping through another list of type TeamRoles and want to add each of that object after some modifications inside the List of TeamRoles, something like below;

public void SomeMethod()
{
    var teamsRoles = _client.GetAllTeamEmployeeRoles();
    var distinctTeams = teamsRoles.Select(x => x.Team).Distinct();
    var tRoles = new List<TeamRoles>();
    foreach (var team in distinctTeams)
    {
        var t = team;
        var roles = teamsRoles.Where(x => x.TeamId ==team.Id).Select(y => y.Role);
        tRoles.Add(new { t, roles}); //need this
    }
}

Please help me if you understand my question and ask for more info if you don't. Thanks in advance.

1
  • What help you are looking for?? Which piece of the code seems to be not working upto ur expectations Commented Sep 22, 2015 at 1:35

2 Answers 2

4

You need to instantiate the TeamRoles and assign values to it, something like this:

var teamRoles = new TeamRoles
 {
   Roles = roles,
   Team = t
 };

tRoles.Add(teamRoles); //need this
Sign up to request clarification or add additional context in comments.

Comments

2

Not quite sure what your question is (is it that your program doesn't compile?)

You need to instantiate TeamRoles like

    foreach (var team in distinctTeams)
    {

        var t = team;
        var roles = teamsRoles.Where(x => x.TeamId ==team.Id).Select(y => y.Role);
        tRoles.Add(new TeamRoles { Team=t, Roles=roles }); //need this  <====
     }

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.