Last active
February 12, 2016 22:43
-
-
Save vendettamit/01b523386d89d6d5e6a8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Program | |
{ | |
class TeamHistory | |
{ | |
public DateTime Date { get; set; } | |
public string Event { get; set; } | |
public string TeamName { get; set; } | |
public override string ToString() | |
{ | |
StringBuilder sb = new StringBuilder(); | |
sb.AppendFormat("TeamName:{0}, Date: {1}, Event:{2}", this.TeamName, this.Date.ToString("MM/dd/yyyy"), this.Event); | |
return sb.ToString(); | |
} | |
} | |
static void Main(string[] args) | |
{ | |
string json = ""; | |
List<TeamHistory> teamHistories = new List<TeamHistory>(); | |
teamHistories.Add(new TeamHistory { TeamName = "xxx", Date = DateTime.Now.AddMonths(1), Event = "Sport" }); | |
teamHistories.Add(new TeamHistory { TeamName = "xxx", Date = DateTime.Now.AddMonths(2), Event = "Dance" }); | |
teamHistories.Add(new TeamHistory { TeamName = "xxx", Date = DateTime.Now.AddMonths(2), Event = "Sport" }); | |
teamHistories.Add(new TeamHistory { TeamName = "yyy", Date = DateTime.Now.AddMonths(1), Event = "Sport" }); | |
teamHistories.Add(new TeamHistory { TeamName = "yyy", Date = DateTime.Now.AddMonths(2), Event = "Sport" }); | |
teamHistories.Add(new TeamHistory { TeamName = "xxx", Date = DateTime.Now.AddMonths(1), Event = "Dance" }); | |
teamHistories.Add(new TeamHistory { TeamName = "xxx", Date = DateTime.Now.AddMonths(2), Event = "Sport" }); | |
Console.WriteLine("========= data ==========="); | |
teamHistories.OrderBy(x => x.Date).ToList().ForEach(x => Console.WriteLine(x)); | |
var res = from th in teamHistories | |
where th.TeamName.ToLower() == "xxx" | |
group th by th.Date into grp1 | |
from grp2 in | |
(from th in grp1 group th by th.Event) | |
group grp2 by grp1.Key; | |
Console.WriteLine("========= actual grouping ==========="); | |
res.Dump(); // Here's the visualizer code - https://gist.github.com/vendettamit/9ee03f693c41e2536433 | |
// Worked but requires additional loop. why? | |
var res2 = teamHistories.GroupBy(x => x.Date) | |
.Select(grp1 => grp1.GroupBy(e => e.Event).GroupBy(grp2 => grp1.Key)); | |
Console.WriteLine("\n\n =========== with lambda ================"); | |
foreach (var item in res2) | |
{ | |
item.Dump(); | |
} | |
// My first attempt, which was in- correct. | |
// var res1 = teamHistories.GroupBy(x => x.Date, (key, g1) => | |
// g1.Select(x => new { key, x }) | |
// .GroupBy(x => x.x.Event, g => g)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment