【问题标题】:How to replace invalid datetime string with a correct format of datetime string when using DateTime.ParseExact()?使用 DateTime.ParseExact() 时如何用正确格式的日期时间字符串替换无效的日期时间字符串?
【发布时间】:2017-06-22 13:11:12
【问题描述】:

我想根据包含 DateTime 的字符串类型属性对我的列表进行排序。为此,我将字符串属性转换为 OrderBy()OrderByDescending() 内的 DateTime。所以这是我的代码:

var orderDirection = "ASC";

//AList already contains data.
AList = (orderDirection.Equals("ASC", StringComparison.OrdinalIgnoreCase)) 
   ? AList
      .OrderBy(x => DateTime.ParseExact(x.LastUpdate, "g", WebCulture))
      .ToList() 
   : AList
      .OrderByDescending(x => DateTime.ParseExact(x.LastUpdate, "g", WebCulture))
      .ToList();

不幸的是,只有当x.LastUpdate 包含有效的日期时间格式时,它才能正常工作。否则会报错。

因此,有什么方法可以让我们说用ParseExact() 中的有效日期时间字符串更改无效的日期时间字符串?或者有没有其他功能可以提供?

例如,“将字符串转换为日期时间,但对于无效字符串,将其替换为“1-1-1990 00:00:00.000”?

【问题讨论】:

  • 例如,您可以使用TryParseExact。甚至不将日期存储为字符串,而是立即将您的 LastUpdate 设为 DateTime 类型。
  • 对于使用TryParseExact,您能告诉我如何在OrderBy() 中声明它吗?因为基本上,对于TryParseExact,它具有布尔值,不是吗?
  • @Evk 不幸的是,属性类型必须是字符串。

标签: c# string linq datetime


【解决方案1】:

OrderBy() 中的一个简单条件呢

AList.OrderBy(x =>
{
    DateTime result;
    if (DateTime.TryParse(x.LastUpdate, out result))
        return result;
    return new DateTime(1990, 1, 1);
});

【讨论】:

    【解决方案2】:

    尝试使用DateTime.TryParseExact 代替DateTime.ParseExact

      var records = AList
        .Select(x => {
           bool valid = DateTime.TryParseExact(x.LastUpdate, "g", WebCulture, out date);
    
           return {
             valid = valid,
             date = date,
             value = x;
           }});
    
       AList = (orderDirection.Equals("ASC", StringComparison.OrdinalIgnoreCase)) 
         ? records
             .OrderBy(x => x.valid)
             .ThenBy(x => x.date)
             .Select(x => x.value)
             .ToList()
         : records
             .OrderByDescending(x => x.valid)
             .ThenByDescending(x => x.date)
             .Select(x => x.value)
             .ToList();
    

    【讨论】:

      【解决方案3】:

      如果格式无效,您可以提供一个返回 DateTime? 的扩展方法:

      public static DateTime? TryGetDateTime(this string str, IFormatProvider provider, string format = null)
      {
          DateTime dt;
          bool valid = format == null 
              ? DateTime.TryParse(str, provider, DateTimeStyles.None, out dt) 
              : DateTime.TryParseExact(str, format, provider, DateTimeStyles.None, out dt);
      
          if (valid)
              return dt;
          else
              return null;
      }
      

      那么查询就直接了:

      AList = AList
          .Select(s => new { s, dt = s.TryGetDateTime(WebCulture) })
          .OrderBy(x => x.dt.GetValueOrDefault())
          .Select(x => x.s)
          .ToList();
      

      (对于 asc/desc 逻辑,只需使用 if...else 并将 Orderby 替换为 OrderByDescending

      【讨论】:

        猜你喜欢
        • 2015-07-23
        • 2012-05-18
        • 2018-06-24
        • 1970-01-01
        • 2014-08-10
        • 1970-01-01
        • 1970-01-01
        • 2020-10-04
        • 1970-01-01
        相关资源
        最近更新 更多