gpt4 book ai didi

c# - 无法转换类型为 'System.Data.EnumerableRowCollection` 的对象 1[System.Int32 ]' to type ' System.IConvertible'

转载 作者:太空宇宙 更新时间:2023-11-03 22:43:27 25 4
gpt4 key购买 nike

我正在尝试使用下面的代码来存储值,但它给我错误。

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

基本上我想检索“数量”的值。

最佳答案

不管你的其他问题,你不能将列表/集合转换为整数

您需要使用 FirstOrDefault 或类似的

Enumerable.FirstOrDefault Method (IEnumerable)

Returns the first element of a sequence, or a default value if the sequence contains no elements.

示例

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

更新

进一步的例子

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

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

Console.WriteLine(result);

输出

345

Full Demo here

关于c# - 无法转换类型为 'System.Data.EnumerableRowCollection` 的对象 1[System.Int32 ]' to type ' System.IConvertible',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51074225/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com