【发布时间】:2011-02-09 16:32:15
【问题描述】:
我试图找出 DataRowCollection.Add(DataRow row) 将新行插入其数据表的位置。它在表格的末尾,就像一个追加?是随机的吗?
另外,我想在循环数据表时使用它。如果存在某些条件,则添加一个包含不同数据的新行,以通过 For 循环运行到数据表的末尾。这种方法有什么具体问题吗?我还能怎么处理?
编辑:我正在循环访问存储在内存中的 .Net DataTABLE。在此循环操作期间,我没有触及存储原始数据的数据库。 DataTABLE 在循环之前填充,这不是问题。
这里是相关代码:
DataTable machineANDlastDate = new DataTable();
//Populate machineANDlastDate
for (int i = 0; i < machineANDlastDate.Rows.Count; i++)
{
lastFutureDate = DateTime.Parse(machineANDlastDate.Rows[i]["MaxDueDate"].ToString());
newDateTime = lastFutureDate.AddDays(frequency); //This is where the new date is created.
machineSerial = machineANDlastDate.Rows[i]["machineSerial"].ToString();
if (newDateTime < DateTime.Now)
{
machineANDlastDate.Rows.Add(new String[] { machineSerial, newDateTime.AddDays(frequency).ToString() });
continue;
}
...Removed for irrelevancy...
}
这是在数据表末尾添加一行的有效方法吗?
【问题讨论】:
-
除非是散列,否则加到最后
-
我认为散列在你有键/值的地方更常见。键是散列的,因此值查找速度很快。
-
我没有看到哈希与我的情况有任何相关性。我在 DataTable 中使用字符串,不涉及哈希。
-
我认为 Alex 只是意味着
Add将新项目添加到除哈希之外的每个集合的末尾。你的集合不是哈希,所以你不会有这个问题。
标签: c# .net datatable for-loop