【问题标题】:Cannot cast DBNull.Value to type 'System.DateTime'. Please use a nullable type.-Query SharePoint list data linqtoSQL无法将 DBNull.Value 转换为类型“System.DateTime”。请使用可空类型。-查询 SharePoint 列表数据 linqtoSQL
【发布时间】:2014-07-15 10:55:32
【问题描述】:

我有一个 LinqToSQL 查询,它应该检查 SharePoint 列表中的 ReturnedDate 是否为空。 我写了下面的查询并在运行时返回错误“无法将 DBNull.Value 转换为类型'System.DateTime'。请使用可为空的类型。

IEnumerable<DataRow> x = from student in dt.AsEnumerable()
                                     join d in dt1.AsEnumerable() on student.Field<string>("Name") equals d.Field<string>("LapName")
                                     where d.Field<DateTime>("ReturnedDate") ==Convert.ToDateTime("")
                                     select student;

有什么想法吗?

【问题讨论】:

    标签: linq sharepoint linq-to-sql


    【解决方案1】:

    ReturnedDate 为 Null 时,您不能施放。所以你必须先检查一下。
    我想你可以使用:

    IEnumerable<DataRow> x = from student in dt.AsEnumerable()
                                     join d in dt1.AsEnumerable() on student.Field<string>("Name") equals d.Field<string>("LapName")
                                     where d.Field<DateTime?>("ParentId").HasValue
                                     select student;
    

    【讨论】:

    • 返回日期出错:无法从用法中推断方法“System.Data.DataRowExtensions.Field(System.Data.DataRow, int)”的类型参数。尝试明确指定类型参数。
    • @SivakumarPiratheeban,我更新了我的答案。将其转换为可为空的 DateTime(带有:'?'),并检查它是否具有值。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-22
    • 1970-01-01
    • 1970-01-01
    • 2021-03-26
    • 2017-07-30
    相关资源
    最近更新 更多