C# 不能隐式转换类型“System.DateTime?” 到'System.DateTime'。存在显式转换

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19972965/
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 16:16:35  来源:igfitidea点击:

Cannot implicitly convert type 'System.DateTime?' to 'System.DateTime'. An explicit conversion exists

c#linq

提问by

I am trying to convert datetime? todatetime but I get this Error:

我想转换日期时间?todatetime 但我收到此错误:

Error 7 Cannot implicitly convert type 'System.DateTime?' to 'System.DateTime'. An explicit conversion exists

错误 7 无法隐式转换类型“System.DateTime?” 到'System.DateTime'。存在显式转换

Here is my code:

这是我的代码:

public string ConvertToPersianToShow(DateTime?  datetime)
{
  DateTime dt;
  string date;
  dt = datetime;

  string year = Convert.ToString(persian_date.GetYear(dt));
  string month = Convert.ToString(persian_date.GetMonth(dt));
  string day = Convert.ToString(persian_date.GetDayOfMonth(dt));

  if (month.Length == 1)
  {
     month = "0" + Convert.ToString(persian_date.GetMonth(dt));
  }
  if (day.Length == 1)
  {
     day = "0" + Convert.ToString(persian_date.GetDayOfMonth(dt));
  }

  //date = Convert.ToString(persian_date.GetYear(dt)) + "/" + 
  Convert.ToString(persian_date.GetMonth(dt)) + "/" +
  //Convert.ToString(persian_date.GetDayOfMonth(dt));
  date = year + "/" + month + "/" + day+"("+dt.Hour+":"+dt.Minute+")";

  return date;
}

采纳答案by Kamil Budziewski

You have 3 options:

您有 3 个选择:

1) Get default value

1) 获取默认值

dt = datetime??DateTime.Now;

it will assign DateTime.Now(or any other value which you want) if datetimeis null

DateTime.Now如果datetime为空,它将分配(或您想要的任何其他值)

2) Check if datetime contains value and if not return empty string

2) 检查日期时间是否包含值,如果不包含则返回空字符串

if(!datetime.HasValue) return "";
dt = datetime.Value;

3) Change signature of method to

3)将方法的签名更改为

public string ConvertToPersianToShow(DateTime  datetime)

It's all because DateTime?means it's nullable DateTimeso before assigning it to DateTimeyou need to check if it contains value and only then assign.

这一切都是因为DateTime?意味着它可以为空,DateTime因此在将其分配给DateTime您之前需要检查它是否包含值,然后才分配。

回答by David Pilkington

dtis nullableyou need to access its Value

dtnullable你需要访问它的Value

if (datetime.HasValue)
    dt = datetime.Value;

It is important to remember that it can be NULL. That is why the nullablestruct has the HasValueproperty that tells you if it is NULLor not.

重要的是要记住它可以NULL。这就是为什么nullable结构具有HasValue告诉您它是否存在的属性的原因NULL

You can also use the null-coalescing operator??to assign a default value

您还可以使用来分配默认值null-coalescing operator??

dt = datetime ?? DateTime.Now;

This will assign the value on the right if the value on the left is NULL

如果左侧的值是 NULL

回答by Ahmed ilyas

you should be using the .Value of the datetime parameter. All Nullable structs have a value property which returns the concrete type of the object. but you must check to see if it is null beforehand otherwise you will get a runtime error.

您应该使用 datetime 参数的 .Value 。所有 Nullable 结构都有一个 value 属性,它返回对象的具体类型。但是您必须事先检查它是否为空,否则您将收到运行时错误。

i.e:

IE:

datetime.Value

but check to see if it has a value first!

但首先检查它是否有值!

if (datetime.HasValue)
{
   // work with datetime.Value
}

回答by dr.Crow

The problem is that you are passing a nullable typeto a non-nullable type.

问题是您将可空类型传递给不可空类型。

You can do any of the following solution:

您可以执行以下任一解决方案:

A. Declare your dtas nullable

A. 声明你dt为可空

DateTime? dt = dateTime;

DateTime? dt = dateTime;

B. Use Valueproperty of the the DateTime? datetime

B. 使用Value属性DateTime? datetime

DateTime dt = datetime.Value;

DateTime dt = datetime.Value;

C. Cast it

C. 投射

DateTime dt = (DateTime) datetime;

DateTime dt = (DateTime) datetime;