I have a performance problem when querying the numbers of a DataTable-X
with a DataTable-Y
, I have to check if the DataTable-X
number exists in DataTable-Y
(and if I find it complements DataTable-X
with data from DataTable-Y
), the way I'm doing right spend of 50 minutes .
Number of Rows of each DataTable :
DataTable-X => 38.258 linhas (10 colunas)
DataTable-X => 61.235 linhas (4 colunas)
In practice, loop
occurs DataTable-X
x DataTable-X
times, that's a lot, but 50 minutes I think too much time.
Follow the code:
for (int i = 0; i < dTx.Rows.Count; i++)
{
bool ganbis1 = false;
foreach (DataRow oLinha in dTy.Rows)
{
if (oLinha[0] != "")
{
if (Convert.ToInt64(oLinha[0].ToString()) ==
Convert.ToInt64(dTx.Rows[i][1].ToString().Replace(" ","")))
{
// dTx = "Coluna1", "Coluna2", "Coluna3", "Coluna4",
// "Coluna5", "Coluna6", "Coluna7", "Coluna8", "Coluna9", "Coluna10"
// dTy = "Coluna1", "Coluna2", "Coluna3","Coluna4"
// Preenche coluna 7
if ((Convert.ToString(oLinha[1]) == "") ||
(Convert.ToString(oLinha[1]) == "#N/A"))
{
dTx.Rows[i][7] = "#n/D";
}
else
{
dTx.Rows[i][7] = Convert.ToString(oLinha[1]);
}
// Preenche coluna 8
if ((Convert.ToString(oLinha[2]) == "") || (Convert.ToString(oLinha[2]) == "#N/A"))
{
dTx.Rows[i][8] = "#n/D";
}
else
{
dTx.Rows[i][8] = Convert.ToString(oLinha[2]);
}
// Preenche coluna 9
if ((Convert.ToString(oLinha[3]) == "") || (Convert.ToString(oLinha[3]) == "#N/A"))
{
dTx.Rows[i][9] = "#n/D";
}
else
{
dTx.Rows[i][9] = Convert.ToString(oLinha[3]);
}
// Preenche coluna 10
if ((Convert.ToString(oLinha[4]) == "") || (Convert.ToString(oLinha[4]) == "#N/A"))
{
dTx.Rows[i][10] = "#n/D";
}
else
{
dTx.Rows[i][10] = Convert.ToString(oLinha[4]);
}
ganbis1 = true;
}
}
}
}
if ((ganbis1 == false) && (i != dTx.Rows.Count-1))
{
// Quando não for encontrado preencher com #n/D
dTx.Rows[i][7] = "#n/D";
dTx.Rows[i][8] = "#n/D";
dTx.Rows[i][9] = "#n/D";
dTx.Rows[i][10] = "#n/D";
}