简体   繁体   English

IList对象创建中的System.InvalidCastException

[英]System.InvalidCastException in IList object creation

Following code is supposed to convert a DataTable to a IList of objects. 应该使用以下代码将DataTable转换为对象的IList The problem would be, if there is a null value in DataTable it'll generate an DBNull error. 问题是,如果DataTable有一个空值,它将生成一个DBNull错误。

Is there a way to check for nulls before assigning values to properties of Deduction object? 在将值分配给Deduction对象的属性之前,是否可以检查空值?

public IList<Deduction> PrepareDeductions() //Entry point
{
    return GetDeductionsToList();
}


public IList<Deduction > GetDeductionsToList()
{
    return CalculateDeductions().Rows.OfType<DataRow>().Select(CreateDeductions).ToList();
}

private DataTable CalculateDeductions()
{
    return _DataService.GetDeductions();
}

private static Deduction CreateDeductions(DataRow row)
{
    return new Deduction()
    {
        EmployeeID = Convert.ToInt32(row[0]),
        EPFAmount = Convert.ToDecimal(row[2]),
        AdvanceAmount = Convert.ToDecimal(row[3]),
        MealsAmount = Convert.ToDecimal(row[4]),
        LoanInstalmentAmount = Convert.ToDecimal(row[5]),
        UniformInstalmentAmount = Convert.ToDecimal(row[6]),
        InsuranceInstalmentAmount = Convert.ToDecimal(row[7]),
        FineAmount = Convert.ToDecimal(row[8]),
        DeathDonationAmount = Convert.ToDecimal(row[9]),
        WelfareAmount = Convert.ToDecimal(row[10]),
    };
}

You can use IsNull to check for null; 您可以使用IsNull检查是否为空; see http://msdn.microsoft.com/en-us/library/chk182xa.aspx 参见http://msdn.microsoft.com/en-us/library/chk182xa.aspx

private static Deduction CreateDeductions(DataRow row)
{
    return new Deduction()
    {
        EmployeeID = row[0] == DBNull.Value ? 0 : Convert.ToInt32(row[0]),
        EPFAmount = row[2] == DBNull.Value ? 0 : Convert.ToDecimal(row[2]),
        AdvanceAmount = row[3] == DBNull.Value ? 0 : Convert.ToDecimal(row[3]),
        MealsAmount = row[4] == DBNull.Value ? 0 : Convert.ToDecimal(row[4]),
        LoanInstalmentAmount = row[5] == DBNull.Value ? 0 : Convert.ToDecimal(row[5]),
        UniformInstalmentAmount = row[6] == DBNull.Value ? 0 : Convert.ToDecimal(row[6]),
        InsuranceInstalmentAmount = row[7] == DBNull.Value ? 0 : Convert.ToDecimal(row[7]),
        FineAmount = row[8] == DBNull.Value ? 0 : Convert.ToDecimal(row[8]),
        DeathDonationAmount = row[9] == DBNull.Value ? 0 : Convert.ToDecimal(row[9]),
        WelfareAmount = row[10] == DBNull.Value ? 0 : Convert.ToDecimal(row[10]),
    };
}

Not sure but you can check like 不确定,但您可以检查像

if (row[0] != System.DBNull.Value))
{
    EmployeeID = Convert.ToInt32(row[0]);
}
else
{
 EmployeeID = somethingelse;
}

You could use the DataRow.Field extension method, which has nicer support for nullable values in the DB, and doesn't return DBNull . 您可以使用DataRow.Field扩展方法,该方法对DB中的可为空的值提供了更好的支持,并且不返回DBNull Your properties should be nullable - eg int? 您的属性应该可以为空-例如int? , decimal? decimal? .

EmployeeID = row.Field<int?>(0);
EPFAmount = row.Field<decimal?>(2);
etc.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM