Asdfg
Asdfg

Reputation: 12203

Nested groups using Linq

This is how my Model looks like:

public class ServerData
{
    public string ServerName { get; set; }
    public DateTime DataDate { get; set; }
}

This is how i am populating the data. which looks like this

    List<CapacityData> capData = new List<CapacityData>();
    capData.Add(new CapacityData { ServerName = "Server1", DataDate = DateTime.Today });
    capData.Add(new CapacityData { ServerName = "Server1", DataDate = DateTime.Today.AddDays(-1) });
    capData.Add(new CapacityData { ServerName = "Server1", DataDate = DateTime.Today.AddDays(-2) });

    capData.Add(new CapacityData { ServerName = "Server2", DataDate = DateTime.Today });
    capData.Add(new CapacityData { ServerName = "Server2", DataDate = DateTime.Today.AddDays(-1) });
    capData.Add(new CapacityData { ServerName = "Server2", DataDate = DateTime.Today.AddDays(-2) });

I am trying to group this resultset into two groups (Servers and DataDate) so that i can get the data like this:

Server1
       01/01/2013
                 Data for 01012013
       01/02/2013
                 Data for 01022013
       01/03/2013
                 Data for 01032013

Server2
       01/01/2013
                 Data for 01012013
       01/02/2013
                 Data for 01022013
       01/03/2013
                 Data for 01032013

I am able to group this data data for ServerName but i am not able to figure out how do i group it further so that the data is grouped by DataDate inside ServerName group.

This is how my LINQ expression looks like:

var results = capData.GroupBy(d => d.ServerName,
           (serverNamekey, groupedData) => new { ServerName = serverNamekey, DailyData = groupedData.ToList() });

Upvotes: 0

Views: 52

Answers (2)

D Stanley
D Stanley

Reputation: 152501

You can either group by a composite key:

var results = capData.GroupBy(d => new {d.ServerName, d.DataDate},
           (key, g) => new { ServerName = key.ServerName, 
                             DataDate = k.DataDate, 
                             DailyData = g.ToList() });

and let the UI deal with repeated servers, or do another GroupBy:

var results = capData.GroupBy(d => d.ServerName)
                     .Select(g => new { ServerName = g.Key, 
                                        DataGroup = g.GroupBy(d => d.DataDate)
                                                     .Select(gg => new { DataDate = gg.Key, 
                                                                         DailyData = gg.ToList()
                                                                       }
                                      });

Upvotes: 1

Servy
Servy

Reputation: 203804

Within each severname group's data you want to also group on data, so take your group data, and group it by data:

var results = capData.GroupBy(d => d.ServerName,
    (serverNamekey, groupedData) => new
    {
        ServerName = serverNamekey,
        DailyData = groupedData.GroupBy(capacity => capacity.DataDate)
    });

Upvotes: 1

Related Questions