c# - 按 C# 中的日期属性或使用 jQuery 拆分列表

标签 c# jquery asp.net repeater

我有一个 List<Event>和一个 Event有一个 Date属性(property)。我目前正在将此列表绑定(bind)到 asp:repeater这很好,并会生成如下事件标题列表:

事件1
事件二
事件三
事件四
事件5

但是,现在我希望在不同日期发生的事件之间的列表中添加某种分隔符,以便我的列表如下所示:

2011 年 5 月 10 日,星期二
事件一
事件二
2011 年 5 月 11 日,星期三
事件三
2011 年 5 月 12 日,星期四
事件四
事件5

谁能推荐一个好的方法来做到这一点。我很乐意在服务器端操作我的列表或绑定(bind)我的列表,然后使用 jQuery 在客户端添加分隔符。有什么建议吗?

最佳答案

您可以使用 Linq 的 GroupBy 按日期服务器端分组

class Event
{
    public DateTime EventDate;
    public string EventName;
}

List<Event> events = GetEvents();

var grouped = events.GroupBy(e => e.EventDate.Date).OrderBy(g => g.Key);

foreach (var grouping in grouped)
{
    Console.WriteLine(grouping.Key); // <-- this is the date

    foreach (Event e in grouping) // <-- this is the list of events
    {
        Console.WriteLine(e.EventName); 
    }
}

GroupBy返回 IEnumerable<IGrouping<TKey, TSource>> , 所以在这种情况下你要返回 IEnumerable<IGrouping<DateTime, Event>>

关于c# - 按 C# 中的日期属性或使用 jQuery 拆分列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5950909/

相关文章:

c# - c# 中的 tcp 监听器和 tcp 服务器是否相同?

c# - 将新联系人添加到现有联系人组 ews api

asp.net - 将 WPF 转换为 Web

c# - 使用 TestStack.White 设置 `VirtualizingPanel.IsVirtualizing`

c# - 使用 Handler 跟踪文件

jquery - 滚动到某个点后停止动画

jquery - 基于不在页面加载中的搜索文本框加载jquery剑道网格

javascript - 使用 jquery 合并列表项的文本,同时保持相同的顺序

asp.net - 如何让 Google Prettify 渲染得更像 Visual Studio

asp.net - Web 窗体错误消息 : "This is not scriptlet. Will be output as plain text"