Skip to content

Instantly share code, notes, and snippets.

@vendettamit
Last active February 12, 2016 22:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vendettamit/01b523386d89d6d5e6a8 to your computer and use it in GitHub Desktop.
Save vendettamit/01b523386d89d6d5e6a8 to your computer and use it in GitHub Desktop.
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