我有一个简单的清单
T.Date = 11/04/2014,T.Title =“ Book1”,T.Tipology =“ Book”
T.Date = 14/04/2014,T.Title =“ Book2”,T.Tipology =“ Book”
T.Date = 02/05/2015,T.Title =“ Spot1”,T.Tipology =“ Spot”
T.Date = 21/06/2015,T.Title =“ Newspaper1”,T.Tipology =“ Newspaper”
我需要按年,月和日对列表进行分组,如下所示:
2014年
四月
T.Date = 11/04/2014,T.Title =“ Book1”,T.Tipology =“ Book”
T.Date = 14/04/2014,T.Title =“ Book2”,T.Tipology =“ Book”
2015年
可以
T.Date = 02/05/2015,T.Title =“ Spot1”,T.Tipology =“ Spot”
六月
T.Date = 21/06/2016,T.Title =“ Newspaper1”,T.Tipology =“ Newspaper”
所以我将能够在foreach函数中处理数据,例如:
foreach(var year in Year)
{
foreach(var month in Month)
{
foreach(var day in Day)
{
Console.WriteLine(day.Item.Title);
Console.WriteLine(day.Item.Tipology);
}
Console.WriteLine(month.ToString()); // With culture
}
Console.WriteLine(year.ToString());
}
如何使用(c#)LINQ做到这一点?
最佳答案
您可以使用嵌套组。
var groups = list
.GroupBy(
d => d.Date.Year,
(key, g) => g.GroupBy(
d => d.Date.Month,
(key2, g2) => g2.GroupBy(d => d.Date)
)
);
关于c# - LINQ-组列表<T>到年,月,日,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31026388/