gpt4 book ai didi

c# - 时间跨度? = DateTime?.Value - DateTime?.Value 结果为空

转载 作者:行者123 更新时间:2023-11-30 16:59:53 25 4
gpt4 key购买 nike

我的 DateTime? 的值减去另一个 DateTime? 的值导致空 TimeSpan?

enter image description here

查看已固定到调试器的日期值。

我无法在控制台应用程序中复制它,因为它使用相同的日期。日期格式为 dd/mm/yyyy

我知道我不需要 .Value 和代码。我试过没有 .Value

后期编辑

TimeSpan ts = DateModified.Value - publishRecord.DatePublished.Value;
TimeSpan ts2 = DateModified.Value.Subtract(publishRecord.DatePublished.Value);
TimeSpan? ts3 = DateModified - publishRecord.DatePublished;

所有尝试都会导致该行出现屏幕截图中的异常。 可为空的对象必须有一个值。

...继续根据建议挖掘并根据要求显示更多信息...

后期编辑 2:

DateModified 和 DatePublished 都是 DateTime 吗?标准。属性是自动获取的;设置;

后期编辑 3 - 总结

我无法解释这个错误。这是一个经过 nunit 测试的系统。因此,当经过良好测试的区域中的某些东西出现这种情况时,令人感到惊讶。这个bug是在接口(interface)开发的时候出现的,一直在通过附加类库和刷新网页的方式调试。始终执行两种解决方案中的构建。

我定期制作数据库快照并保留当前损坏的快照,同时返回问题发生前的快照。该系统工作。回到坏掉的那个又显示了这个bug。

我制作了一个控制台应用程序并使用类库引用编写代码以执行类似的代码跟踪,但无法复制“损坏”快照中的错误。

在工作和不工作之间发生了什么,windows更新,主要针对办公软件。 错过了喝咖啡的时间。

我已经保留了数据库快照,过几天再看看这个问题会不会出现。如果没有,我会在那时删除问题。

感谢大家的关注和建议。

最佳答案

以这种方式在这里使用 Nullable Timespan 是没有意义的。两个 DateTime? 都有一个值,并且您得到一个常规(不可为 null)的 TimeSpan,或者一个或两个 DateTime? 没有值,在这种情况下访问 .Value 属性会导致异常。听起来你应该有更像这样的代码:

TimeSpan? ts = null;
if (DateModified.HasValue && publishRecord.DatePublished.HasValue)
ts = DateModified.Value - publishRecord.DatePublished.Value;

我也注意到了这一点:

I made a console app and wrote code with the class library reference to perform a similar code track and could not replicate the bug in the "broken" snapshot.

基于此,我会在另一个线程中寻找可以改变您的其中一个值的内容。也许尝试在行执行之前记录 DateModifiedpublishRecord.DatePublished 的值,并在抛出异常时再次记录。我希望您会发现其中一个已变为 null

关于c# - 时间跨度? = DateTime?.Value - DateTime?.Value 结果为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23055304/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com