简体   繁体   English

将DateTime属性设置为null

[英]Setting DateTime property to null

So I have this property and would like to either set the value to what is coming back from the db or to null if it is empty. 因此,我具有此属性,并希望将值设置为从数据库返回的值,如果为空,则将其设置为null。 It is possible to do this with an if-else, but for cleaner code, I would like to use the ternary operator. 可以使用if-else来做到这一点,但是对于更简洁的代码,我想使用三元运算符。 Could someone point out the mistake I am making. 有人可以指出我正在犯的错误。 Thanks!!! 谢谢!!!

public DateTime? OptionExpiration {get;set;}
 //actually sets the value to null               
 if(String.IsNullOrEmpty(dr["OPTION_EXPIRATION"].ToString())){
      OptionExpiration = null;
}else{
      OptionExpiration = DateTime.Parse(dr["OPTION_EXPIRATION"].ToString());
}

//so I check the to see if the string is empty or null, then try to set the value but recieve this error: Error 2 Operator '|' //因此我检查了一下以查看字符串是否为空或空,然后尝试设置该值,但收到此错误:错误2运算符'|' cannot be applied to operands of type '' and 'System.DateTime?' 无法应用于类型为''和'System.DateTime?'的操作数

String.IsNullOrEmpty(dr["OPTION_EXPIRATION"].ToString())
? OptionExpiration =  null
| OptionExpiration = DateTime.Parse(dr["OPTION_EXPIRATION"].ToString())
;

You are using the ternary operator wrong. 您使用的三元运算符错误。

It should be: 它应该是:

OptionExpiration = String.IsNullOrEmpty(Convert.ToString(dr["OPTION_EXPIRATION"]))
                   ? (DateTime?)null
                   : DateTime.Parse(dr["OPTION_EXPIRATION"].ToString())
                   ;

So: 所以:

assignment = condition ? trueExpression : falseExpression;

If the field is a date in your database, it might be better to do this: 如果该字段是数据库中的date ,则最好这样做:

OptionExpiration = Convert.IsDBNull(dr["OPTION_EXPIRATION"])
                   ? (DateTime?)null
                   : (DateTime)dr["OPTION_EXPIRATION"]
                   ;

I would use an extension method like this: 我将使用如下扩展方法:

            public static Nullable<DateTime> AsNullableDateTime(this object item, Nullable<DateTime> defaultDateTime = null)
            {
                if (item == null || string.IsNullOrEmpty(item.ToString()))
                    return defaultDateTime;

                DateTime result;
                if (!DateTime.TryParse(item.ToString(), out result))
                    return defaultDateTime;

                return result;
            }

You can pass anything in to this and it will attempt to give you back a date. 您可以将任何内容传递给它,它将尝试给您回一个日期。 If it fails for whatever reason (this also checks to make sure the object you send through is not null) you will get a null back; 如果由于某种原因失败(这还会检查以确保通过您发送的对象不为空),您将得到一个空值。 which is fine because you're mapping to a nullable datetime. 这很好,因为您正在映射到可为空的日期时间。

To use this you would do something like: 要使用此功能,您需要执行以下操作:

OptionExpiration = dr["OPTION_EXPIRATION"].AsNullableDateTime();

No mess, easy to understand what is happening, abstracting away the clutter, and highly reusable on other parts of your solution. 毫无混乱,易于理解正在发生的事情,使混乱变得抽象,并且在解决方案的其他部分具有高度可重用性。

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

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