Answer the question
In order to leave comments, you need to log in
How to improve performance when iterating over the elements of a DataTable?
The essence of the problem is as follows, using a sql query, I get a table with 19,000 rows.
Next, I need to fill in another table based on the data in this table:
for (int i = 0; i < data.Rows.Count; i++)
{
ClearCurrentConsoleLine(menuBaseString.Length, menuBaseString.Length + 1);
Console.Write(i + "\\" + data.Rows.Count);
var row = data.Rows[i];
context.Pages.Add(new Page()
{
//...
Catalog_Id = Convert.ToInt32(row.ItemArray[2]),
CatalogName = row.ItemArray[3].ToString(),
CatalogIdentifer = row.ItemArray[4].ToString(),
//...
});
}
context.savechanges();
Answer the question
In order to leave comments, you need to log in
well, actually everything turned out to be simple, first you need to create a List with all the objects that need to be added, and then do context.AddRange
var tmp = new List<Page>();
for (int i = 0; i < data.Rows.Count; i++)
{
ClearCurrentConsoleLine(menuBaseString.Length, menuBaseString.Length + 1);
Console.Write(i + "\\" + data.Rows.Count);
var row = data.Rows[i];
tmp.Add(new Page()
{
//...
Catalog_Id = Convert.ToInt32(row.ItemArray[2]),
CatalogName = row.ItemArray[3].ToString(),
CatalogIdentifer = row.ItemArray[4].ToString(),
//...
});
}
context.Pages.AddRange(tmp);
And what is ClearCurrentConsoleLine(menuBaseString.Length, menuBaseString.Length + 1);
If this is an output of progress or some data to the console, remove this line and performance will increase by 50 times. In general, run the process under the debugger and profiling. Check which libraries and methods are slowing down the process.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question