【问题标题】:ASP - How to get min and max date from table column [duplicate]ASP - 如何从表列中获取最小和最大日期[重复]
【发布时间】:2016-06-01 13:58:00
【问题描述】:

我想从 columnOne 获取 minDate,从 columnTwo 获取 maxDate。我的解决方案是 SharePoint 的 C# 类库。

这是我的代码:

 SPListItemCollection myItemColForTable = list.GetItems(myQueryForTable);

 List<DateTime> minDate = new List<DateTime>();
 List<DateTime> maxDate = new List<DateTime>();

  foreach (SPListItem item in myItemColForTable)
  {
     minDate.Add(Convert.ToDateTime(item["DepartureDate"].ToString()));
     maxDate.Add(Convert.ToDateTime(item["ReturnDate"].ToString()));
  }

我哪里错了?

【问题讨论】:

  • 还有MaxMin 方法。您不应该对列表进行排序。

标签: c# asp.net datetime


【解决方案1】:

更好地使用 IEnumerable 函数:

DateTime min = minDate.Min();
DateTime max = maxDate.Max();

【讨论】:

    【解决方案2】:

    您可以尝试myItemColForTable.Min(p =&gt; p.DepartureDate)Max() 的保存。这消除了您对 foreach 循环的需要。

    您的代码应该看起来更像这样:

    SPListItemCollection myItemColForTable = list.GetItems(myQueryForTable);
    var myNewList = myItemColForTable.ToList();
    DateTime minDate = myNewList.Min(p => p.DepartureDate);
    DateTime maxDate = myNewList.Max(p => p.ReturnDate);
    

    【讨论】:

    • 错误 4 'Microsoft.SharePoint.SPListItemCollection' 不包含 'Max' 的定义,并且找不到接受类型为 'Microsoft.SharePoint.SPListItemCollection' 的第一个参数的扩展方法 'Max' (您是否缺少 using 指令或程序集引用?)
    • @Gohyu 哎呀。没有真正注意SPListItemCollection 部分哈哈。您应该可以拨打.ToList()。您可能需要考虑将您的问题重新定义为更具体的 SharePoint 而不是 ASP。
    • 更新代码:
    • 错误 1 ​​'Microsoft.SharePoint.SPListItemCollection' 不包含 'ToList' 的定义,并且找不到接受类型为 'Microsoft.SharePoint.SPListItemCollection' 的第一个参数的扩展方法 'ToList' (您是否缺少 using 指令或程序集引用?)
    • @Gohyu 抱歉,我无法提供更多帮助,我对 SharePoint 没有任何经验。
    猜你喜欢
    • 2018-03-12
    • 1970-01-01
    • 1970-01-01
    • 2022-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多