gpt4 book ai didi

c# - 如何安排日期时间列表的特殊表格格式

转载 作者:太空宇宙 更新时间:2023-11-03 20:16:28 24 4
gpt4 key购买 nike

例如,我有这样的 DateTime 列表;

List<DateTime> timeList;

我使用 repeater 创建了一个如下所示的表格;

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

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

最佳答案

首先,创建一个类来保存你的数据:

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
}
}
}

然后,您可以按年/月分组以绑定(bind)您的列表。

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

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

关于c# - 如何安排日期时间列表的特殊表格格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16362984/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com