patterncsharpMinor
Constructing file paths for each row of a datatable in C#
Viewed 0 times
fileeachdatatablepathsconstructingforrow
Problem
I need to iterate through a DataTable and columns in each rows and do some data manipulation.
Below given is an extract from my code, I use c#.net. I expect maximum rows in datatable will be less than 25K.
How I can improve the code for better performance?
Below given is an extract from my code, I use c#.net. I expect maximum rows in datatable will be less than 25K.
How I can improve the code for better performance?
DataTable metadaDataTable = GetMetaDataTable();
foreach (DataRow row in metadaDataTable.Rows)
{
string newfileName = string.Empty;
foreach (DataColumn col in metadaDataTable.Columns)
{
if (col.ColumnName != "path_variable")
{
newfileName += row[col].ToString() + "_";
}
}
newPath = CleanFileName(newfileName);
}Solution
One simple trick here would be to take the column processing outside of the row processing (the column metadata should not change while the rows probably will):
Note also I've sprinkled a liberal amount of LINQ in there to signify intent rather than mechanism.
Finally, Also surrounded the lifetime of
using (var metadaDataTable = GetMetaDataTable())
{
var columns = metadaDataTable.Columns
.Cast()
.Where(col => col.ColumnName != "path_variable")
.ToList();
var path_variable = metadaDataTable.Columns
.Cast()
.SingleOrDefault(col => col.ColumnName == "path_variable");
string path_variable_value;
foreach (var row in metadaDataTable.Rows.Cast())
{
var newfileName = columns.Aggregate(string.Empty, (current, col) => current + (row[col] + "_"));
newPath = CleanFileName(newfileName);
path_variable_value = path_variable == null ? null : row[path_variable].ToString();
// TODO: do something with the path_variable_value before continuing the loop.
}
}Note also I've sprinkled a liberal amount of LINQ in there to signify intent rather than mechanism.
Finally, Also surrounded the lifetime of
metaDataTable with a using construct as DataTable implements the IDisposable interface.Code Snippets
using (var metadaDataTable = GetMetaDataTable())
{
var columns = metadaDataTable.Columns
.Cast<DataColumn>()
.Where(col => col.ColumnName != "path_variable")
.ToList();
var path_variable = metadaDataTable.Columns
.Cast<DataColumn>()
.SingleOrDefault(col => col.ColumnName == "path_variable");
string path_variable_value;
foreach (var row in metadaDataTable.Rows.Cast<DataRow>())
{
var newfileName = columns.Aggregate(string.Empty, (current, col) => current + (row[col] + "_"));
newPath = CleanFileName(newfileName);
path_variable_value = path_variable == null ? null : row[path_variable].ToString();
// TODO: do something with the path_variable_value before continuing the loop.
}
}Context
StackExchange Code Review Q#152294, answer score: 3
Revisions (0)
No revisions yet.