C# 如何将日期时间设置为空

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16769233/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 07:44:24  来源:igfitidea点击:

How to set DateTime to null

c#entity-framework

提问by GibboK

Using C#. I have a string dateTimeEnd.

使用 C#。我有一个字符串dateTimeEnd

If the string is in right format, I wish to generate a DateTimeand assign it to eventCustom.DateTimeEnd of type

如果字符串格式正确,我希望生成一个DateTime并将其分配给 eventCustom.DateTimeEnd 类型

public Nullable<System.DateTime> DateTimeEnd { get; set; }

If dateTimeEndis null or empty I need eventCustom.DateTimeEndset to null.

如果dateTimeEnd为空或空,我需要eventCustom.DateTimeEnd设置为空。

I am trying to achieve this using the following code but I get always null for eventCustom.DateTimeEnd.

我正在尝试使用以下代码实现此目的,但对于eventCustom.DateTimeEnd.

Could you please help me out to define what is wrong in my code?

你能帮我定义一下我的代码有什么问题吗?

   DateTime? dateTimeEndResult;
     if (!string.IsNullOrWhiteSpace(dateTimeEnd))
        dateTimeEndResult = DateTime.Parse(dateTimeEnd);


eventCustom.DateTimeEnd = dateTimeEndResult = true ? (DateTime?)null : dateTimeEndResult;

采纳答案by Jon Skeet

It looks like you just want:

看起来你只是想要:

eventCustom.DateTimeEnd = string.IsNullOrWhiteSpace(dateTimeEnd)
    ? (DateTime?) null
    : DateTime.Parse(dateTimeEnd);

Note that this will throw an exception if dateTimeEndisn't a valid date.

请注意,如果dateTimeEnd不是有效日期,这将引发异常。

An alternative would be:

另一种选择是:

DateTime validValue;
eventCustom.DateTimeEnd = DateTime.TryParse(dateTimeEnd, out validValue)
    ? validValue
    : (DateTime?) null;

That will now set the result to nullif dateTimeEndisn't valid. Note that TryParsehandles nullas an input with no problems.

现在将结果设置为nullifdateTimeEnd无效。请注意,TryParse处理null作为输入没有问题。

回答by Matthew Watson

This should work:

这应该有效:

if (!string.IsNullOrWhiteSpace(dateTimeEnd))
    eventCustom.DateTimeEnd = DateTime.Parse(dateTimeEnd);
else
    eventCustom.DateTimeEnd = null;

Note that this will throw an exception if the string is not in the correct format.

请注意,如果字符串格式不正确,这将引发异常。

回答by Rahul

DateTimeis a non-nullable value type

DateTime是不可为空的值类型

DateTime? newdate = null;

You can use a Nullable<DateTime>

你可以使用一个 Nullable<DateTime>

c# Nullable Datetime

c#可为空的日期时间

回答by Subhash Jakhar

You can write DateTime? newdate = null;

你可以写DateTime吗?新日期 = 空;

回答by Fung

This line:

这一行:

eventCustom.DateTimeEnd = dateTimeEndResult = true ? (DateTime?)null : dateTimeEndResult;

is same as:

与:

eventCustom.DateTimeEnd = dateTimeEndResult = (true ? (DateTime?)null : dateTimeEndResult);

because the conditional operator ?has a higher precedence than the assignment operator =. That's why you always get null for eventCustom.DateTimeEnd. (MSDN Ref)

因为条件运算符?的优先级高于赋值运算符=。这就是为什么你总是为eventCustom.DateTimeEnd. ( MSDN 参考)

回答by Fox V?nh Tam

Now, I can't use DateTime?, I am using DBNull.Valuefor all data types. It works great.

现在,我不能使用DateTime?,我正在使用DBNull.Value所有数据类型。它工作得很好。

eventCustom.DateTimeEnd = string.IsNullOrWhiteSpace(dateTimeEnd)
  ? DBNull.Value
  : DateTime.Parse(dateTimeEnd);