Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# LINQ查询问题日期时间转换_C#_Linq_Ado.net - Fatal编程技术网

C# LINQ查询问题日期时间转换

C# LINQ查询问题日期时间转换,c#,linq,ado.net,C#,Linq,Ado.net,在执行LINQ时,我遇到了这个错误。 无法将类型“System.DateTime”隐式转换为“System.DateTime”。存在显式转换(是否缺少转换?) 我知道这是因为数据类型,但转换不起作用。是否有其他方法可以做到这一点。 这是我的密码 {var tvr = from t in ce.tbl_TVRinfo where t.TVRID == fTVRid select new TVRDetails

在执行LINQ时,我遇到了这个错误。 无法将类型“System.DateTime”隐式转换为“System.DateTime”。存在显式转换(是否缺少转换?)

我知道这是因为数据类型,但转换不起作用。是否有其他方法可以做到这一点。 这是我的密码

        {var tvr = from t in ce.tbl_TVRinfo
                  where t.TVRID == fTVRid
                  select new TVRDetails
                  {
                      TVRID = t.TVRID,
                      Ename = t.Ename,
                      Esdw = t.Esdw,
                      Edob =t.Edob, //this field is causing date conversion error
                      Epob = t.Epob,
                      Equalification = t.Equalification,
                      NIC = t.NIC,
                      EAddress = t.EAddress
                  }
        return tvr.ToList();
         }
你可以

Edob = (DateTime)t.Edob
但如果t.Edob为null,则会引发异常。 如果Edob为null,最好提供一个默认的datetime。空合并运算符在这里很有用

Edob = t.Edob ?? DateTime.Now // Or DateTime.MinValue, or whatever makes sense in your situation

您正在尝试将可为空的日期时间分配给标准日期时间。如果您确信可为空的DateTime包含一个值,则可以执行以下操作:

Edob =t.Edob.Value
或者,如果您不确定它是否有值:

Edob = t.Edob.HasValue ? t.Edob.Value : SomeOtherValidDateTimeValue

Naveed,正如我所提到的,如果t.Edob.Value为null,那么它将失败。因此,你可能需要解决这个问题,除非你100%相信它永远不会发生,尽管这种情况很少发生。它确实有效,但@Darren的答案更优雅一些。请注意,正如我们都说过的,如果t.Edob为null,除非使用null合并版本,否则您将得到一个rull-time NullReferenceException。