Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 处理datetime的空值_C#_Asp.net_Linq - Fatal编程技术网

C# 处理datetime的空值

C# 处理datetime的空值,c#,asp.net,linq,C#,Asp.net,Linq,我有一个Linq查询,它由我从数据库中提取的datetime字段组成 TerminationDate = base.ConvertFromUtcToCentral(r.TerminationDate.ToString()) 使用的函数是这样设计的 public DateTime ConvertFromUtcToCentral(string UtcTime) { if (UtcTime != "") {

我有一个Linq查询,它由我从数据库中提取的datetime字段组成

           TerminationDate = base.ConvertFromUtcToCentral(r.TerminationDate.ToString())
使用的函数是这样设计的

           public DateTime ConvertFromUtcToCentral(string UtcTime)
    {

        if (UtcTime != "")
        {
            DateTime?  retValue = TimeZoneInfo.ConvertTimeFromUtc(Convert.ToDateTime
                                                       (UtcTime), USTimeZones.Central);

            return Convert.ToDateTime(retValue);
        }
        else
            return Convert.ToDateTime(null);

    }
public static DateTime? WrapperConvertFromUtcToCentral(string UtcTime)
{
    DateTime value = ConvertFromUtcToCentral(string UtcTime);

    if (value == default(DateTime))
         return (DateTime?)null;
    else
         return (DateTime?)value;
}

TerminationDate = WrapperConvertFromUtcToCentral(r.TerminationDate.ToString())        
但每当我返回null时,它就会在我分配该查询的网格中显示最小日期。但是,当它为null时,它应该只显示null而不是min date。你能告诉我如何处理这个问题吗?同时告诉我使用三元运算符是否更好


限制:无法更改函数retrun类型,因为它在多个位置使用

,因为您返回的是值类型DateTime,所以不能返回null。您可以更改函数以返回可为空的DateTime(DateTime?),引发异常,或者返回DateTime的默认值default(DateTime)。IMHO我会抛出System.ArgumentException,因为要转换的空字符串显然不会产生输出。

也许您可以尝试返回
null
。 将函数设置为return
DateTime?
, 因此,不是
返回Convert.ToDateTime(null)您可以执行
返回null


对不起,英语不好

在不能更改函数的限制下,可以使用terinery运算符:

TerminationDate = ( base.ConvertFromUtcToCentral(r.TerminationDate.ToString()) == default(DateTime) ? (DateTime?) null : (DateTime ?) base.ConvertFromUtcToCentral(r.TerminationDate.ToString()) );
我不喜欢这个,因为它调用函数两次。我可能会编写一个包装器方法来实现类似的功能

           public DateTime ConvertFromUtcToCentral(string UtcTime)
    {

        if (UtcTime != "")
        {
            DateTime?  retValue = TimeZoneInfo.ConvertTimeFromUtc(Convert.ToDateTime
                                                       (UtcTime), USTimeZones.Central);

            return Convert.ToDateTime(retValue);
        }
        else
            return Convert.ToDateTime(null);

    }
public static DateTime? WrapperConvertFromUtcToCentral(string UtcTime)
{
    DateTime value = ConvertFromUtcToCentral(string UtcTime);

    if (value == default(DateTime))
         return (DateTime?)null;
    else
         return (DateTime?)value;
}

TerminationDate = WrapperConvertFromUtcToCentral(r.TerminationDate.ToString())        

这样,您只能调用原始函数一次。

最近的编辑和注释表明
TerminationDate
可能是
DateTime?
。可能添加一个新的重载,显式处理
DateTime
DateTime
值,并正确返回
DateTime
DateTime?
?我发现您无法轻松更改此方法的返回类型,因此添加这些新的重载并在应用程序中逐步使用它们可能是使用更好的数据类型的好方法

public DateTime ConvertFromUtcToCentral(string UtcTime)
{
    return !string.IsNullOrEmpty(UtcTime)
        ? this.ConvertFromUtcToCentral(Convert.ToDateTime(UtcTime))
        : default(DateTime);
}

public DateTime? ConvertFromUtcToCentral(DateTime? UtcTime)
{
    return UtcTime.HasValue
        ? this.ConvertFromUtcToCentral(UtcTime.Value)
        : (DateTime?)null;
}

public DateTime ConvertFromUtcToCentral(DateTime UtcTime)
{
    return TimeZoneInfo.ConvertTimeFromUtc(UtcTime, USTimeZones.Central);
}
如果不更改返回类型,则始终需要从方法中获取非空的
DateTime
值,除非-如user957902所建议的-开始抛出
异常
s:)如果无法使用此选项,您还可以修改消费应用程序,将
DateTime.MinValue
null
类似,并且不显示任何内容


是否使用三元运算符可能是一个有趣的问题——在像这样的简单情况下,我确实喜欢使用它(我的示例中的换行符纯粹是为了让它更容易阅读),因为它非常容易阅读,而且只有两条路径可以使用。

不能为DateTime指定null值,因为它不是可为null的类型
在将其发回数据库时,尝试分配DBNull.Value而不是null

如果DateTime实际为null,则传递什么作为字符串UtcTime?如果UtcTime在DBNull中实际为null,则传递空字符串“”。
r.TerminationDate的类型是什么,为什么你要把它转换成一个
字符串
,然后再从
字符串
转换成
日期时间
?据我所知,没有办法让它显示为空,因为日期时间空值与最小值相同,我相信是1900年1月1日凌晨12:00或类似的时间。是否有特定的原因需要它返回null而不是min date?如果是这样,那么在哪里有必要使用空值?在数据库中还是在代码中?如果需要的话,一个函数在多个地方使用不应该阻止您重构它。在这种情况下,这似乎是最合理的解决方法。该函数用于不同查询中的多个位置。因此,现在很难修改函数+1@Nishanth,你能用你的限制更新你的问题吗?到目前为止,改变返回类型是唯一合理的方法,因为DateTime值类型没有特殊的“null”值(最小值是没有可为null的类型时可以得到的最接近的值)。如果函数在多个地方使用,为什么不调用函数并传入参数?函数的全部要点是为了代码重用,因此您只需更改函数一次。仅供参考:
Convert.ToDateTime(null)
确实返回
DateTime.MinValue
,它也恰好是
default(DateTime)
@hangy Oops my bad的值。下次需要把问题读得慢一点。:)无论如何,抛出异常实际上会改变方法的行为,并在某种程度上改变类的公共接口。像这样的破坏性更改可能会导致比某个GUI中出现的
DateTime.MinValue
更多的问题(用户错误报告)。我首先想到的是包装器——我同意这是一个条件允许的优雅解决方案