Marnus Steyn
Marnus Steyn

Reputation: 1093

Grouping and nesting list with LINQ

I have a list that consists of objects of the following fixture class:

public class APIFixtureModel
{
    public string HomeTeamName { get; set; }
    public string AwayTeamName { get; set; }
    public string TournamentName { get; set; }
    public string SportType { get; set; }
    public DateTime FixtureDateTime { get; set; }
}

So with my current setup, I get a list of fixtures, which is nice but I need a structure where the list is grouped by sport type, then by tournament. To illustrate:

public class APIExtendedFixtureModel
{
    private List<Sport> lstSports;

    class Sport
    {
        public string SportType { get; set; }
        public List<Tournament> lstTournaments;
    }

    class Tournament
    {
        public string TournamentName { get; set; }
        public List<Fixture> lstFixtures;
    }

    class Fixture
    {
        public string HomeTeamName { get; set; }
        public string AwayTeamName { get; set; }
        public DateTime FixtureDateTime { get; set; }
    }
}

I have tried the following:

 var testgroup = lstFixtures.GroupBy(f => f.SportType,
                                     f => f.TournamentName,
                                        (key,g) => new
                                        {
                                            Sport = key,
                                            Tournament = g.ToList()
                                        }).ToList();

What I get is a list of sports and inside each sport node I get a list tournaments, but that's where it stops, I can't seem to get it right.

Upvotes: 2

Views: 1197

Answers (2)

Andy Briggs
Andy Briggs

Reputation: 485

This returns the list of Sport objects populated with Tournaments and Fixtures:

List<Sport> sports = 
    fixtureList.GroupBy(f => f.SportType).Select(sGroup => new Sport 
        { 
            SportType = sGroup.Key,
            lstTournaments = sGroup.GroupBy(f => f.TournamentName).Select(tGroup => new Tournament 
                {               
                    TournamentName = tGroup.Key,
                    lstFixtures = tGroup.Select(f => new Fixture 
                        {
                            HomeTeamName = f.HomeTeamName,
                            AwayTeamName = f.AwayTeamName,
                            FixtureDateTime = f.FixtureDateTime,
                        }).ToList(),
                }).ToList()
        }).ToList();

Upvotes: 2

Azhar Khorasany
Azhar Khorasany

Reputation: 2709

If you want to group by more than one column then create an anonymous type:

var testgroup = lstFixtures.GroupBy(f => new { f.SportType, f.TournamentName },
                                        (key,g) => new
                                        {
                                            Sport = key.SportType,
                                            Tournament = key.TournamentName,
                                            Result = g.ToList()
                                        });

You can add further columns to this too if you want :)

Upvotes: -1

Related Questions