【发布时间】:2021-04-01 08:33:35
【问题描述】:
我目前正在使用现有的一段代码,它可以将指定数据表列中的所有数据行作为整数插入到列表中。但是,在当前代码中,我只能添加所有数据行。我希望能够插入 x 数量的数据行,我该怎么做?
代码:
var dt = new DataTable
{
Columns = { { "Lastname", typeof(int) }, { "Firstname", typeof(int) } }
};
dt.Rows.Add(1, 2);
dt.Rows.Add(4, 5);
dt.Rows.Add(7, 4);
dt.Rows.Add(54, 67);
List<int> ids = new List<int>();
foreach (DataRow row in dt.Rows)
ids.Add((int)row[0]);
foreach(int e in ids)
Console.WriteLine(e);
Console.Read();
此代码当前将打印出1,4,7,54,但如果我只想打印1,4,7,该怎么办?
【问题讨论】: