无法将DBNull.Value强制转换为“System.DateTime”。请使用可空类型。关于使用Linq查询

时间:2016-05-26 07:52:23

标签: c# asp.net linq datatable

我有两个名为dt1Cloned和dt2Cloned的数据表。我已经实现了linq查询来连接这两个表,如:

var res = (from p in dt1Cloned.AsEnumerable()
               join t in dt2Cloned.AsEnumerable()
            on p.Field<Int64>("WorkId") equals t.Field<Int64>("WorkId")
             select new
               {
                   DivisionId = p.Field<Int64>("DivisionId"),
                   DistrictId = p.Field<Int64>("DistrictId"),
                   MCId = p.Field<Int64>("MCId"),
                   ACId = p.Field<Int64>("ACId"),
                   PCId = p.Field<Int64>("PCId"),
                   DivisionName = p.Field<string>("DivisionName"),
                   DistrictName = p.Field<string>("DistrictName"),
                   MCName = p.Field<string>("MCName"),
                   AssemblyCName = p.Field<string>("AssemblyCName"),
                   ParliamentaryCName = p.Field<string>("ParliamentaryCName"),
                   WorkId = p.Field<Int64>("WorkId"),
                   WorkDetail = p.Field<string>("WorkDetail"),
                   WorkName = p.Field<string>("WorkName"),
                   RoadCode = p.Field<string>("RoadCode"),
                   AdApp = p.Field<string>("AdApp"),
                   //!Convert.IsDBNull(dr.t1.Field<DateTime?>("Step Start Time")) ? dr.t1.Field<DateTime?>("Step Start Time") : DBNull.Value 
                   //AdminAppDate = !Convert.IsDBNull(p.Field<DateTime>("AdminAppDate")) ? p.Field<DateTime>("AdminAppDate") : DBNull.Value,
                   AdminAppDate = p.Field<DateTime>("AdminAppDate"),
                   TargetDate = p.Field<DateTime>("TargetDate"),
                   AppAmt = p.Field<Int64>("AppAmt"),
                   TApp = p.Field<string>("TApp"),
                   TechAppDate = p.Field<DateTime>("TechAppDate"),
                   TAppAmt = p.Field<Int64>("TAppAmt"),
                   SourceOfFunds = p.Field<string>("SourceOfFunds"),
                   AllocatedAmt = p.Field<Int64>("AllocatedAmt"),
                   ReleaseAmt = p.Field<Int64>("ReleaseAmt"),
                   WStatus = p.Field<string>("WStatus "),
                   Expenditure = p.Field<string>("Expenditure"),
                   ExpenditurePer = p.Field<Int64>("ExpenditurePer"),
                   ProgressPercentage = p.Field<Int64>("ProgressPercentage"),
                   Progress = p.Field<string>("Progress"),
                   UserTypeId = p.Field<Int64>("UserTypeId"),
                   UserWorkType = p.Field<string>("UserWorkType"),
                   SchemeId = p.Field<Int64>("SchemeId"),
                   SchemeName = p.Field<string>("SchemeName"),
                   AgencyId = p.Field<Int64>("AgencyId"),
                   AgencyName = p.Field<string>("AgencyName"),
                   TADate = p.Field<DateTime>("TADate"),
                   AADate = p.Field<DateTime>("AADate"),
                   StatusDate = p.Field<DateTime>("StatusDate"),
                   ProgressDate = p.Field<DateTime>("ProgressDate"),
                   ReleaseDate = p.Field<DateTime>("ReleaseDate"),
                   AllocationDate = p.Field<DateTime>("AllocationDate"),
                   ExpDate = p.Field<DateTime>("ExpDate"),
                   TimeLimit = p.Field<Int64>("TimeLimit"),
                   TimeSpent = p.Field<Int64>("TimeSpent"),
                   TimeSpentPer = p.Field<double>("TimeSpentPer"),
                   TimeDiff = p.Field<double>("TimeDiff"),
                   EarthWorkP = p.Field<double>("EarthWorkP"),
                   EarthWorkC = p.Field<double>("EarthWorkC"),
                   SolingP = p.Field<double>("SolingP"),
                   SolingC = p.Field<double>("SolingC"),
                   WearingP = p.Field<double>("WearingP"),
                   WearingC = p.Field<double>("WearingC"),
                   RaisingP = p.Field<double>("RaisingP"),
                   RaisingC = p.Field<double>("RaisingC"),
                   StrengtheningP = p.Field<double>("StrengtheningP"),
                   StrengtheningC = p.Field<double>("StrengtheningC"),
                   PCLaidP = p.Field<double>("PCLaidP"),
                   PCLaidC = p.Field<double>("PCLaidC"),
                   ModifyDate = p.Field<DateTime>("ModifyDate"),
                   VillageName = t.Field<string>("VillageName"),
               }).ToList();

关于上面的查询,我得到异常“无法将DBNull.Value强制转换为'System.DateTime'。请使用可空类型。” 。如何解决此问题并处理所有数据类型的空值?

1 个答案:

答案 0 :(得分:3)

如果您的数据库字段可以为空,那么您还需要将正在读取它的C#属性设置为可为空。

将您的媒体资源声明为Nullable<DateTime>。有一个方便的简写:DateTime?

public class Person
{
    // DateOfBirth is NOT nullable - everyone has a date of birth...
    public DateTime DateOfBirth { get; set; }

    // DateOfDeath IS nullable - not everyone is dead yet...
    public DateTime? DateOfDeath { get; set; }
}

然后,当您预测新对象时,可以使用:

DateOfDeath = p.Field<DateTime?>("DateOfDeath"),