大家晚上好
我在表中有一个包含字符串类型值的故障列。该列中的每个单元格可能包含多个逗号分隔的错误(例如:CPU不是wkg,HDD损坏等)。我的要求是读取每个单元格的值,将每个错误存储在ArrayList中,然后打印一个表格,使其显示两列(faultname,noOftimeItccured)。为了实现这一点,我正在对数组列表进行排序,然后找出每个错误的出现次数。 我面临的问题 1.我没有得到预期的结果(逻辑中可能存在一些问题) 2.无法在webform中以表格的格式显示结果。 3.结果应显示在网格视图之后,而显示在菜单栏上方。
请指导我获得所需的结果,输出或其他方法来实现上述问题。
提前致谢。
if (ddlSparesInfo.SelectedValue == "Fast Moving Spares")
{
lblMessage.Text = "";
try
{
SqlCommand cmd = new SqlCommand("getFastMovingSpares", DBConn.Conn);
DBConn.OpenConn();
using (SqlDataReader dr = cmd.ExecuteReader())
{
ArrayList faults = new ArrayList();
while (dr.Read())
{
string cellValue = dr.GetString(7);
string[] splitFaults = cellValue.Split(',');
for (int i = 0; i < splitFaults.Length; i++)
{
faults.Add(splitFaults[i]);
}
}
faults.Sort();
int count = 1;
for (int i = 1; i < faults.Count; i++)
{
if (faults[i - 1] == faults[i])
{
count++;
}
else
{
Response.Write("<table>");
Response.Write("<tr>");
Response.Write("<td>");
Response.Write("<p>" + faults[i] + "<p>");
Response.Write("</td>");
Response.Write("<td>");
Response.Write("<p>" + count + "<p>");
Response.Write("</td>");
Response.Write("</tr>");
Response.Write("</table>");
count = 0;
}
}
}
}
catch (Exception)
{
throw;
}
finally
{
DBConn.CloseConn();
}
}
}
答案 0 :(得分:0)
您可以使用Dictionary<key,value>
来存储故障和计数。
if (ddlSparesInfo.SelectedValue == "Fast Moving Spares")
{
lblMessage.Text = "";
try
{
SqlCommand cmd = new SqlCommand("getFastMovingSpares", DBConn.Conn);
DBConn.OpenConn();
using (SqlDataReader dr = cmd.ExecuteReader())
{
Dictionary<string, int> faults = new Dictionary<string, int>();
while (dr.Read())
{
string cellValue = dr.GetString(7);
string[] splitFaults = cellValue.Split(',');
for (int i = 0; i < splitFaults.Length; i++)
{
string currentFault = splitFaults[i];
if (faults.Keys.Any(x => x == currentFault))
{
faults[currentFault] += 1;
}
else
{
faults.Add(currentFault, 1);
}
}
}
Response.Write("<table>");
foreach (var fault in faults)
{
Response.Write("<tr>");
Response.Write("<td>");
Response.Write("<p>" + fault.Key + "<p>");
Response.Write("</td>");
Response.Write("<td>");
Response.Write("<p>" + fault.Value + "<p>");
Response.Write("</td>");
Response.Write("</tr>");
}
Response.Write("</table>");
}
}
catch (Exception)
{
throw;
}
finally
{
DBConn.CloseConn();
}
}