1
I’m having a little trouble checking the numbers of a DataTable-X
with a DataTable-Y
, I have to check if the number of DataTable-X
exists in the DataTable-Y
(and if the DataTable-X
with the data of DataTable-Y
), the way I’m doing spending right 50 minutes.
Number of Lines of each Datatable:
DataTable-X => 38.258 linhas (10 colunas)
DataTable-X => 61.235 linhas (4 colunas)
In practice, the loop
occurs DataTable-X
x DataTable-X
times, that’s a lot, but 50 minutes I think too much time.
Follows 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";
}
Do you bring the database data? If it is database could not make an SQL comparison and do this operation in the database?
– novic
Unfortunately I have to read N xlsx files, and while performing this write treatment on a new xlsx with all unified.
– D1Dih
I understood @D1dih but, if you did so, the reading of data play on two tables in the database (could be even temporary) and then make a process to expedite the process and soon after the result generate again the xlsx?
– novic
@Virgilionovic understood. This way it is possible to do yes. You believe there is no improvement in the form used above?
– D1Dih
I can’t tell you and measure, but it’s slow to really read so many lines and keep comparing two very large items in memory, in my opinion you need to organize the data first (I would throw it in a database) and then extract the data. This process can improve performance since organized and logical data tends to be easier to compare ... !!!
– novic
@Virgilionovic Very observant and good suggestion.
– D1Dih
I followed that line of organizing the data and include a break when finding the item. I sorted the columns I’m comparing in ascending order: Dataview dv = dTx.Defaultview; dv.Sort = "Column name to be sorted Asc"; dTx = dv.Totable(); Dataview dvTwo = dTy.Defaultview; dvTwo.Sort = "Column name to be sorted Asc"; dTy = dvTwo.Totable(); .
– D1Dih
haaa... this has halved the processing time. :)
– D1Dih