Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.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# 系统可为空<;系统时间跨度>';不包含';总分钟数';_C#_C# 4.0_Datetime_Nullable_Timespan - Fatal编程技术网

C# 系统可为空<;系统时间跨度>';不包含';总分钟数';

C# 系统可为空<;系统时间跨度>';不包含';总分钟数';,c#,c#-4.0,datetime,nullable,timespan,C#,C# 4.0,Datetime,Nullable,Timespan,是否有一种优雅的方法来解决此错误,而不是在if()中检查.HasValue,然后添加诸如DateTimeUtcNow.Value之类的值 // Compare nullable date time entity.End with utcnow but only compare up to minute precision // for time. // if ((int)(entity.End - DateTime.UtcNow).TotalMinutes < 0) { // AS

是否有一种优雅的方法来解决此错误,而不是在if()中检查.HasValue,然后添加诸如DateTimeUtcNow.Value之类的值

// Compare nullable date time entity.End with utcnow but only compare up to minute precision
// for time.
//
if ((int)(entity.End - DateTime.UtcNow).TotalMinutes < 0) 
{
   // ASSERT: entity.End < UTCNow (i.e. 12/4/2012 3:56 PM < 12/4/2012 3:57 PM) 
}
//比较可为空的日期时间实体。以utcnow结尾,但仅比较最新精度
//时间。
//
if((int)(entity.End-DateTime.UtcNow).TotalMinutes<0)
{
//断言:实体.End
错误:System.Nullable'不包含 “总分钟数”的定义


我个人认为:

if (entity.End > DateTime.Now.AddMinutes(1))
{
    ...
}
DateTime now = DateTime.UtcNow;
DateTime lowerBound = new DateTime(now.Year, now.Month, now.Hour, now.Minute,
                                   0, 0, DateTimeKind.Utc);
DateTime upperBound = now.AddMinutes(1);
if (entity.End >= lowerBound && entity.End < upperBound)
{
    ...
}
如果
entity.End
为null,则该条件将为false

我经常发现,与其将一个日期/时间与另一个日期/时间进行比较,并将其与
TimeSpan
(或您使用的任何类型)进行比较,不如计算下限或上限,并将其与变量日期/时间进行比较。在这种情况下,它的效果肯定很好

编辑:好的,现在问题已经清楚了一点,我将这样写:

if (entity.End > DateTime.Now.AddMinutes(1))
{
    ...
}
DateTime now = DateTime.UtcNow;
DateTime lowerBound = new DateTime(now.Year, now.Month, now.Hour, now.Minute,
                                   0, 0, DateTimeKind.Utc);
DateTime upperBound = now.AddMinutes(1);
if (entity.End >= lowerBound && entity.End < upperBound)
{
    ...
}
DateTime now=DateTime.UtcNow;
DateTime lowerBound=新的日期时间(now.Year,now.Month,now.Hour,now.Minute,
0,0,DateTimeKind.Utc);
DateTime上限=now.AddMinutes(1);
if(entity.End>=下限和&entity.End<上限)
{
...
}

我怀疑我可能仍然误解了…

一个选项是创建一个扩展方法来访问可为null的值并传播null:

public static TOut? Select<T, TOut>(this T? nullable, Func<T, TOut> func) 
    where T : struct 
    where TOut : struct
{
    return nullable.HasValue ? (TOut?)func(nullable.Value) : null;
}

非常好(这里填充字符限制)@Jon Skeet:Thx!绝对干净多了。然而,我试图用这个可为空的日期时间(entity.End)将其与现在进行比较,但仅限于最新的精度。Addminutes(1)似乎不能产生正确的结果。还是想看看我能做些什么来让它发挥作用。@JaJ:老实说,很难说你想要实现什么。如果你能在问题中给出更多的细节和例子,那将非常有帮助。@JaJ
If(entity.End>newdatetime(DateTime.Now.Year,DateTime.Now.Month,DateTime.Now.Day,DateTime.Now.Hour,DateTime.Now.Minute,0)。AddMinutes(1)){}
@jonsket Hi Jon:Jon。我更新了原始的ask代码。基本上,我需要将可为空的datetime与now进行比较,但时间精度仅为分钟(即忽略秒和毫秒)。