我正在寻找一种按日期获取分组列表的方法。
我成功地按日期对我的列表进行了排序,但我知道如何在新列表中获取包含所有分组对象的列表。
List<PersonneWithDate> personneList = GetPersonnelsByDay ();
var groups = from p in personneList
group p by new { day = p.Date.Day, month = p.Date.Month , year= p.Date.Year } into d
select new { dt = string.Format("{0}/{1}/{2}", d.Key.day,d.Key.month,d.Key.year), count = d.Count()};
给我一个列表,其中包含一个具有 dt 和 count 的对象,但我想要所有的 PersonneWithDate 也被分组。
我该怎么做?
最佳答案
我不确定我是否理解你的问题,但这就是你想要的吗?
List<PersonneWithDate> personneList = GetPersonnelsByDay();
var groups =
(
from p in personneList
group p by p.Date.Date into d
select new
{
dt = d.Key.ToShortDateString(),
count = d.Count(),
personnel = d.ToList(),
}
).ToList();
(我也稍微简化了查询分组。)
关于c# - Linq Group 按日期和对象列表列出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34701506/