简体   繁体   English

无法将类型为'System.Data.EnumerableRowCollection`1 [System.Int32]'的对象强制转换为'System.IConvertible'

[英]Unable to cast object of type 'System.Data.EnumerableRowCollection`1[System.Int32]' to type 'System.IConvertible'

I am trying to store value by using code below but it is giving me error. 我试图通过使用下面的代码存储价值,但它给了我错误。

int dqty=  Convert.ToInt32(from row in result.AsEnumerable()
where row.Field<string>("batch_num") == k_batch.ToString() 
select row.Field<int>("qty"));

Basically i want to retrieve value of "qty". 基本上我想要检索“数量”的值。

Disregarding your other problems, you cant convert a list/collection to an integer 忽略您的其他问题,您无法将列表/集合转换为整数

You'll need to use FirstOrDefault or similar 您需要使用FirstOrDefault或类似的

Enumerable.FirstOrDefault Method (IEnumerable) Enumerable.FirstOrDefault方法(IEnumerable)

Returns the first element of a sequence, or a default value if the sequence contains no elements. 返回序列的第一个元素,如果序列不包含元素,则返回默认值。

Example

 int dqty = (from row in result.AsEnumerable()
              where row.Field<string>("batch_num") == k_batch.ToString() 
              select row.Field<int>("qty")).FirstOrDefault();

Update 更新

Further exmaple 进一步的例子

var list = new List<int>()  {23, 345, 546, 345};

var result = (
    from row in list
    where row > 23
    select row).FirstOrDefault();

Console.WriteLine(result);

Output 产量

345

Full Demo here 完整的演示在这里

暂无
暂无

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

相关问题 无法转换类型为“ System.Data.EnumerableRowCollection`1 [System.String]”的类型为“ System.IConvertible” - unable to cast of type 'System.Data.EnumerableRowCollection`1[System.String]' to type 'System.IConvertible' 无法将类型为“ System.Data.Linq.DataQuery`1 [System.Int32]”的对象转换为类型为“ System.IConvertible”的对象 - Unable to cast object of type 'System.Data.Linq.DataQuery`1[System.Int32]' to type 'System.IConvertible' System.Data.Entity.Infrastructure.DbRawSqlQuery`1 [System.Int32]&#39;键入“ System.IConvertible错误” - System.Data.Entity.Infrastructure.DbRawSqlQuery`1[System.Int32]' to type 'System.IConvertible Error 从ASP.NET调用Oracle存储过程时,无法将类型为“ System.Int32 []”的对象转换为类型为“ System.IConvertible” - Unable to cast object of type 'System.Int32[]' to type 'System.IConvertible when calling Oracle stored procedure from ASP.NET 无法将类型为“数据”的对象转换为类型为“ System.IConvertible”的对象 - Unable to cast object of type 'Data' to type 'System.IConvertible' 无法将类型为&#39;System.Data.EnumerableRowCollection`1 [System.Data.DataRow]&#39;的对象转换为相同类型 - Unable to cast object of type 'System.Data.EnumerableRowCollection`1[System.Data.DataRow]' to same type 无法将“System.Data.Entity.DynamicProxies”类型的 object 转换为“System.IConvertible”类型 - Unable to cast object of type 'System.Data.Entity.DynamicProxies' to type 'System.IConvertible' 无法将类型为“骰子”的对象转换为类型为“ System.IConvertible”的对象 - Unable to cast object of type 'Dice' to type 'System.IConvertible' 无法将类型为DataTable的对象转换为类型System.IConvertible - Unable to cast object of type DataTable to type System.IConvertible 无法将“MyType”类型的对象强制转换为“IMyInterface`1 [System.IConvertible]” - Unable to cast object of type 'MyType' to type 'IMyInterface`1[System.IConvertible]'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM