Constructing file paths for each row of a datatable in C# 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?

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);
   }
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):

using (var metadaDataTable = GetMetaDataTable())
{
    var columns = metadaDataTable.Columns
        .Cast&lt;DataColumn&gt;()
        .Where(col =&gt; col.ColumnName != "path_variable")
        .ToList();
    var path_variable = metadaDataTable.Columns
        .Cast&lt;DataColumn&gt;()
        .SingleOrDefault(col =&gt; col.ColumnName == "path_variable");
    string path_variable_value;

    foreach (var row in metadaDataTable.Rows.Cast&lt;DataRow&gt;())
    {
        var newfileName = columns.Aggregate(string.Empty, (current, col) =&gt; 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.