gpt4 book ai didi

c# - DataRowCollection 没有类型推断

转载 作者:行者123 更新时间:2023-11-30 20:53:24 25 4
gpt4 key购买 nike

<分区>

我有一个项目将一些来自 SQL 的数据存储在 DataTable 中,然后将每个 DataRow 映射到自定义类实例。

当我遍历 Rows 属性(属于 DataRowCollection 类型)时,没有类型推断。

所以这是行不通的:

var dt = new DataTable();
foreach(var row in dt.Rows)
{
int id = Int32.Parse(row.ItemArray[0].ToString());
// doesn't compile
}

但是这样做:

var dt = new DataTable();
foreach(DataRow row in dt.Rows)
{
int id = Int32.Parse(row.ItemArray[0].ToString());
}

为什么编译器无法确定 是什么类型?在枚举 DataRowCollection 的情况下,var 关键字可以代表其他内容吗?除了数据行之外,是否还有其他内容可以在 DataRowCollection 中枚举?

这就是您需要明确说明的原因吗?

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