例如,我有DateTime
这样的列表;
List<DateTime> timeList;
我使用转发器创建如下表格;
YEAR MONTH DATES
2012 SEPTEMBER 08 15 22 29
2012 OCTOBER 06 20 27
2012 NOVEMBER 10
2012 DECEMBER 08
2013 MAY 04 18
2013 JUNE 01 15 29
2013 JULY 13 27
2013 AUGUST 10 24
2013 SEPTEMBER 07 21
2013 OCTOBER 05 19
但我无法将日期分组。如何使用转发器实现此目的?
答案 0 :(得分:3)
首先,创建一个保存数据的类:
public class DateInfo
{
public int Year { get; set; }
public string Month { get; set; }
public IEnumerable<int> Days { get; set; }
public string DisplayDayList
{
get
{
return string.Join(" ", Days.Select(x=>x.ToString()).ToArray()); //sorry, i'm doing .net 3.5
}
}
}
然后,您可以按年/月分组以使列表绑定。
List<DateTime> dtList = new List<DateTime>();
List<DateInfo> dateInfoList =
(from dt in dtList
group dt by new { dt.Year, dt.Month } into g
select new DateInfo()
{
Year = g.Key.Year,
Month = CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(g.Key.Month),
Days = g.Select(x => x.Day)
}).ToList();
现在,使用新的列表对象进行绑定,并仅绑定Year
,Month
和DisplayDayList
列。
答案 1 :(得分:2)
您可以使用此查询进行分组
var query = from t in timeList
group t by new { t.Year, t.Month } into g
select new { Month = CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(g.Key.Month),
Year = g.Key.Year,
Dates = g.Select(t => t.Day) };