1

例如,我有这样的列表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  

但我无法对日期进行分组。如何使用中继器实现这一目标?

4

2 回答 2

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();

现在,使用您的新列表对象进行绑定,并且只绑定YearMonthDisplayDayList列。

于 2013-05-03T16:38:55.303 回答
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) };
于 2013-05-03T16:08:26.180 回答