一张表里的数据想根据日期分组,然后再根据班级分组,
list.GroupBy(g=>g.Date) .GroupBy(g=>g.Class)
这样是错的,请教怎么写?
public class DemoEntity { public int Date { get; set; } public string Class { get; set; } public override string ToString() { return String.Format("{0}-{1}", Date, Class); } } class Program { static void Main(string[] args) { List<DemoEntity> myList = new List<DemoEntity>(); myList.Add(new DemoEntity() { Date = 1, Class = "C1" }); myList.Add(new DemoEntity() { Date = 1, Class = "C2" }); myList.Add(new DemoEntity() { Date = 1, Class = "C1" }); myList.Add(new DemoEntity() { Date = 2, Class = "C1" }); myList.Add(new DemoEntity() { Date = 2, Class = "C2" }); myList.Add(new DemoEntity() { Date = 2, Class = "C3" }); myList.Add(new DemoEntity() { Date = 2, Class = "C2" }); var result = myList.GroupBy(g => new { g.Class, g.Date }); result.ToList().ForEach(i => i.ToList().ForEach(a => Console.WriteLine(a.ToString()))); Console.ReadKey(); }