简体   繁体   English

错误“无法将'System.Int32'类型的对象强制转换为'System.Collections.Generic.List`1 [System.Int32]''

[英]error “Unable to cast object of type 'System.Int32' to type 'System.Collections.Generic.List`1[System.Int32]'”

I have an package delivery application and I am getting an error in my code below: 我有一个包交付应用程序,我在下面的代码中收到错误:

public class DeliveryDriver
{
    public List<int> DriverID { get; set; }
    public List<string> DriverName { get; set; }
}

var IDeliveryDriver = new List<DeliveryDriver>();

foreach (DataRow dr in dsDriverID.Tables[0].Rows)
{
    IDeliveryDriver.Add(new DeliveryDriver
    {
        DriverID = (List<int>)dr["DriverId"],
        DriverName = (List<string>)dr["DriverName"]
    });
}

The error occurs at the IDeliveryDriver.Add line and it goes into a catch block. 错误发生在IDeliveryDriver.Add行,它进入catch块。 The error is: 错误是:

Unable to cast object of type 'System.Int32' to type 'System.Collections.Generic.List`1[System.Int32]' 无法将类型为'System.Int32'的对象强制转换为'System.Collections.Generic.List`1 [System.Int32]'

What is causing the error? 是什么导致错误?

The error message says it all - the DriverId field in each row is an int , not a List<int> . 错误消息说明了一切 - 每行中的DriverId字段是一个int ,而不是List<int> Ditto for DriverName being a string , not a List<string> . 同样, DriverName是一个string ,而不是List<string> You should change your DriverId and DriverName properties to be int and string respectively, then cast to the right type too: 您应该将DriverIdDriverName属性分别更改为intstring ,然后再转换为正确的类型:

public class DeliveryDriver
{
    public int DriverID { get; set; }
    public string DriverName { get; set; }
}

...

foreach (DataRow dr in dsDriverID.Tables[0].Rows)
{
    IDeliveryDriver.Add(new DeliveryDriver
    {
        DriverID = (int) dr["DriverId"],
        DriverName = (string) dr["DriverName"]
    });
}

After all, each delivery driver does only have one name and one ID, don't they? 毕竟,每个送货司机只有一个名字和一个身份证,不是吗?

暂无
暂无

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

相关问题 无法将类型为“WhereListIterator`1[System.Object]”的对象转换为类型“System.Collections.Generic.IEnumerable`1[System.Int32]” - Unable to cast object of type 'WhereListIterator`1[System.Object]' to type 'System.Collections.Generic.IEnumerable`1[System.Int32]' ASP.NET LINQ错误无法创建类型为&#39;System.Collections.Generic.List`1 [System.Int32]&#39;的查询结果 - ASP.NET LINQ Error Cannot create a query result of type 'System.Collections.Generic.List`1[System.Int32]' 错误:System.InvalidCastException:无法将“System.Byte”类型的对象转换为“System.Int32”类型 - Error: System.InvalidCastException: Unable to cast object of type 'System.Byte' to type 'System.Int32' 错误无法将类型为“ System.Int32”的对象转换为类型为“ System.String”的对象 - Error Unable to cast object of type 'System.Int32' to type 'System.String' 错误:无法将类型为“ System.Int32”的对象转换为类型为“ System.String”的对象 - Error: Unable to cast object of type 'System.Int32' to type 'System.String' C#:无法将“System.Int64”类型的对象转换为“System.Int32”类型 - C#: Unable to cast object of type 'System.Int64' to type 'System.Int32' 无法将类型为“ System.Int64”的对象转换为类型为“ System.Int32”的对象 - Unable to cast object of type 'System.Int64' to type 'System.Int32' 无法将类型为&#39;System.Data.EnumerableRowCollection`1 [System.Int32]&#39;的对象强制转换为&#39;System.IConvertible&#39; - Unable to cast object of type 'System.Data.EnumerableRowCollection`1[System.Int32]' to type 'System.IConvertible' System.InvalidCastException:无法将“System.Double”类型的 object 转换为代码中的“System.Int32”类型 - System.InvalidCastException: Unable to cast object of type 'System.Double' to type 'System.Int32' in code 无法将 System.Int64 类型的 object 转换为 System.Int32 类型 - Unable to cast object of type System.Int64 to type System.Int32
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM