【问题标题】:how to get expiration date from an item in C#如何从C#中的项目获取到期日期
【发布时间】:2020-10-11 18:53:15
【问题描述】:
  1. 我需要删除用户列表中已过期的任何用户。如果 User 类的 expiredDate 属性为空或大于今天,则应将用户添加到列表中。

  2. 用户列表是从 SQL 数据库中读取的。如果数据库中的属性 expiredDate 为空,则它将采用默认值,例如 ({0001-01-01 12:00:00 AM}) 或 {{0001/01/01 12:00:00 AM} 取决于用户区域设置。

  3. 我下面的当前代码将删除所有过期的用户以及 expiredDate 为空的用户。但我只想删除过期用户。

    private User[] removeExpireUserFromList(User[] List)
    {
        User[] result = null;
        int i = 0;
        foreach (var item in List)
        {
            if (item.ExpirationDate < DateTime.Today)
            {
                result[i] = item;
                i++;
            }
        }
        return result;
    }
    

【问题讨论】:

标签: c# date time


【解决方案1】:

如果 ExpirationDate 的 db 列可以为空,则您需要在 C# 代码中将 ExpirationDate 类型定义为可以为空:“DateTime?”。之后,它将从 db 中读取为 null,您需要按如下方式对其进行检查:

if (item.ExpirationDate == null || item.ExpirationDate < DateTime.Today)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-16
    相关资源
    最近更新 更多