简体   繁体   English

如何使用具有可空日期的null-coalescing运算符

[英]How to use null-coalescing operator with nullable date

My view model has a nullable date property such as... 我的视图模型具有可以为空的日期属性,例如...

[DataType(DataType.Date)]
public DateTime? SanctionExpires { get; set; }

But when I try to use the null-coalescing operator such as... 但是当我尝试使用null-coalescing运算符时......

var test = model.SanctionExpires.Value.ToUniversalTime() ?? model.SanctionExpires;

I get the following error... 我收到以下错误...

operator '??' 算'''' cannot be applied to operands of type 'datetime' and 'datetime' 不能应用于'datetime'和'datetime'类型的操作数

I thought this should work because I set my date property to nullable and this post suggests it should work too. 我认为这应该工作,因为我将我的日期属性设置为可空, 这篇文章表明它应该也可以工作。 What am I doing wrong? 我究竟做错了什么?

UPDATE UPDATE

To give more clarity I want to use null-coaslescing to assign a Dapper parameter which will update a DateTime field in my database. 为了更清晰,我想使用null-coaslescing来分配一个Dapper参数,该参数将更新数据库中的DateTime字段。 The field can accept null values or datetime values. 该字段可以接受空值或日期时间值。 Basically if a user specifies a date then assign the provided date to the parameter otherwise assign null... 基本上,如果用户指定日期,则将提供的日期分配给参数,否则指定null ...

p.Add("@SanctionExpires", model.SanctionExpires.Value.ToUniversalTime() ?? model.SanctionExpires);

Note that I have to do the UTC conversion because i'm using SQL Azure which uses UTC for dates otherwise my time is out by 11 hours (i'm in Australia) if i just pass a data from my local system. 请注意,我必须进行UTC转换,因为我使用的是使用UTC作为日期的SQL Azure,否则如果我只是从本地系统传递数据,那么我的时间将超过11小时(我在澳大利亚)。 In sql i'm using a function to offset the date to my timezone before updating the table. 在sql中我使用函数在更新表之前将日期偏移到我的时区。

My current solution to get it working is below but not sure if this is the cleanest way. 我目前的解决方案是让它工作,但不确定这是否是最干净的方式。 Would be nice to get in one line... 能够排成一列真是太好了......

if (model.SanctionExpires == null)
    p.Add("@SanctionExpires", model.SanctionExpires);
else
    p.Add("@SanctionExpires", model.SanctionExpires.Value.ToUniversalTime());

.Value on a nullable assumes it can't be null. .Value可为空的假设它不能为空。 Hence is applying the null-coalescing operator on it useless and forbidden. 因此,对它使用null-coalescing运算符是无用的和禁止的。

You could use the operator like this, but going on your code I am not sure if this is what you need: 您可以像这样使用运算符,但是继续使用您的代码我不确定这是否是您需要的:

DateTime test = (model.SanctionExpires ?? DateTime.Now).ToUniversalTime();

For your update: you can shortcut that evaluation like this: 对于您的更新:您可以像这样快捷地进行评估:

DateTime? test = model.SanctionExpires?.ToUniversalTime();

You can also use the GetValueOrDefault method to return default value if your DateTime object is null. 如果DateTime对象为null,也可以使用GetValueOrDefault方法返回默认值。 In case you want to use the actual date, you should use DateTime.Now . 如果您想使用实际日期,您应该使用DateTime.Now

For example following code snippet can help you: 例如,以下代码段可以帮助您:

var defaultDateTime = DateTime.Now;
var dateTime = model.SanctionExpires.GetValueOrDefault(defaultDateTime).ToUniversalTime();

If you call GetValueOrDefault without params it will use default(DateTime) if your DateTime is null 如果你没有参数调用GetValueOrDefault ,如果你的DateTime为null,它将使用default(DateTime)

var dateTime = model.SanctionExpires.GetValueOrDefault().ToUniversalTime(); //dateTime == default(DateTime) in case !model.SanctionExpires.HasValue

Update 更新

If you are using C#5.0 or below you can use following code snippet because NULL conditional operator is available not before C#6.0. 如果您使用的是C#5.0或更低版本,则可以使用以下代码片段,因为在C#6.0之前不能使用NULL条件运算符

p.Add("@SanctionExpires", !model.SanctionExpires.HasValue ? null : (DateTime?)model.SanctionExpires.Value.ToUniversalTime());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM