【发布时间】:2015-01-12 19:45:45
【问题描述】:
我有一个数据表,其中包含每月拨打的服务电话列表。 F4 列包含机器的序列号,现在我要做的就是找到为每个序列号放置的服务调用次数,并将计数添加到新的 a 列中。我在下面编写了代码,一切正常,但是当数据中出现井号“#”以反映未知或尚未分配的序列号时,出现了问题。这会导致错误“无法对 System.String 和 System.Int32 执行 '=' 操作”。在“DataRow[] FoundRow = dt.Select("[F4] =" + chkStr); F4 列是文本/字符串,我什至尝试克隆数据表并将列手动分配给 .DataType = typeof(String);我知道它的 # 符号给出了问题,就好像我从数据中删除以进行测试一样,一切都很好。任何人都可以对此有所了解。非常感谢。
// datatable dt contains all my monthly call data
dt.Columns.Add("Counter", typeof(System.Int32)); // add a count column to the datatable
for (int i = 0; i < dt.Rows.Count; i++) //loop through the datatable
{
string chkStr = dt.Rows[i]["F4"].ToString(); // get 1st serial num, then each other one in loop
DataRow[] FoundRows = dt.Select("[F4] =" + chkStr); // OK now select all the table rows containing this serial, place in FoundRows
Int32 count = FoundRows.Length; // get the length which is a count of the same serial num / service calls
dt.Rows[i]["Counter"] = count;
}
【问题讨论】: