C# DataRow RowFilter return 不正确的行数
C# DataRow RowFilter return incorrect row count
我有一项 windows 服务,该服务每 1 分钟 运行 检查设备上的交易金额。
该代码非常适合检查设备是否存在以及检查设备名称。
但是,有 1 个部分检查了行数但没有意义。
当 RowFilter 已经正确时,交易量始终是总交易量而不是每台设备。
下图供参考,我正在检查我的变量是否正确并且确实如此。
我尝试了不同的方法来过滤数据表中的行。
尝试 1:
int i = 0;
foreach (DataRow row in HRDT.Rows)
{
string temp = HRDT.Rows[i].Field<string>(HRDT.Columns.IndexOf("HRDevices"));
iDT.DefaultView.RowFilter = "serialno = " + temp;
i++;
}
尝试 2:
foreach (DataRow row in HRDT.Rows)
{
HRDevice = row["HRDevices"].ToString();
iDT.DefaultView.RowFilter = "serialno = " + HRDevice ;
}
iDT.DefaultView.RowFilter = "serialno = " + temp;
其中 None 给出了正确的结果。
结果应该显示每个设备的总行数,而不是所有设备的总行数。
我认为我的行过滤方法有误,因此寻求帮助。
当前完整代码:
iDT = Ingressds.Tables["ytlhpfprec"];
int i = 0;
foreach (DataRow row in HRDT.Rows)
{
HRDevice = row["HRDevices"].ToString();
HRDName = row["HRDName"].ToString();
string temp = HRDT.Rows[i].Field<string>(HRDT.Columns.IndexOf("HRDevices"));
LMTF.LogMessageToFile("Device serial is : "+ HRDevice);
try
{
iDT.DefaultView.RowFilter = "serialno = " + HRDT.Rows[i].Field<string>(HRDT.Columns.IndexOf("HRDevices"));
LMTF.LogMessageToFile("Device serial is : "+ HRDevice + " And temp is " + temp);
int ipRows = iDT.Rows.Count;
string ipString = ipRows.ToString();
LMTF.LogMessageToFile(HRDName + " have " + ipString + " transaction.");
}
catch (System.Exception ex)
{
LMTF.LogMessageToFile("Error in getting Ingress transaction in Property : " + HRDName + " " + ex.Message);
}
i++;
}
Ingressds.Dispose();
Ingressds.Clear();
DefaultView
是 iDT
的 属性,return 是 DataView
。但是您指的不是 that DataView
实例,而是指 iDT
实例。如果您修改代码以使用视图,那么它可能会更明显。您可以使用 view.Count
到 return 中的记录数 DataView
:
try
{
DataView view = new DataView(iDT);
view.RowFilter = "serialno = " + temp);
LMTF.LogMessageToFile("Device serial is : "+ HRDevice + " And temp is " + temp);
int ipRows = view.Count;
string ipString = ipRows.ToString();
LMTF.LogMessageToFile(HRDName + " have " + ipString + " transaction.");
}
我有一项 windows 服务,该服务每 1 分钟 运行 检查设备上的交易金额。
该代码非常适合检查设备是否存在以及检查设备名称。
但是,有 1 个部分检查了行数但没有意义。
当 RowFilter 已经正确时,交易量始终是总交易量而不是每台设备。
下图供参考,我正在检查我的变量是否正确并且确实如此。
我尝试了不同的方法来过滤数据表中的行。
尝试 1:
int i = 0;
foreach (DataRow row in HRDT.Rows)
{
string temp = HRDT.Rows[i].Field<string>(HRDT.Columns.IndexOf("HRDevices"));
iDT.DefaultView.RowFilter = "serialno = " + temp;
i++;
}
尝试 2:
foreach (DataRow row in HRDT.Rows)
{
HRDevice = row["HRDevices"].ToString();
iDT.DefaultView.RowFilter = "serialno = " + HRDevice ;
}
iDT.DefaultView.RowFilter = "serialno = " + temp;
其中 None 给出了正确的结果。 结果应该显示每个设备的总行数,而不是所有设备的总行数。
我认为我的行过滤方法有误,因此寻求帮助。
当前完整代码:
iDT = Ingressds.Tables["ytlhpfprec"];
int i = 0;
foreach (DataRow row in HRDT.Rows)
{
HRDevice = row["HRDevices"].ToString();
HRDName = row["HRDName"].ToString();
string temp = HRDT.Rows[i].Field<string>(HRDT.Columns.IndexOf("HRDevices"));
LMTF.LogMessageToFile("Device serial is : "+ HRDevice);
try
{
iDT.DefaultView.RowFilter = "serialno = " + HRDT.Rows[i].Field<string>(HRDT.Columns.IndexOf("HRDevices"));
LMTF.LogMessageToFile("Device serial is : "+ HRDevice + " And temp is " + temp);
int ipRows = iDT.Rows.Count;
string ipString = ipRows.ToString();
LMTF.LogMessageToFile(HRDName + " have " + ipString + " transaction.");
}
catch (System.Exception ex)
{
LMTF.LogMessageToFile("Error in getting Ingress transaction in Property : " + HRDName + " " + ex.Message);
}
i++;
}
Ingressds.Dispose();
Ingressds.Clear();
DefaultView
是 iDT
的 属性,return 是 DataView
。但是您指的不是 that DataView
实例,而是指 iDT
实例。如果您修改代码以使用视图,那么它可能会更明显。您可以使用 view.Count
到 return 中的记录数 DataView
:
try
{
DataView view = new DataView(iDT);
view.RowFilter = "serialno = " + temp);
LMTF.LogMessageToFile("Device serial is : "+ HRDevice + " And temp is " + temp);
int ipRows = view.Count;
string ipString = ipRows.ToString();
LMTF.LogMessageToFile(HRDName + " have " + ipString + " transaction.");
}