I have a performance bottleneck on a DataView.Sort. The code is below.
/// <summary>
/// Filters the data table and returns a new data table with only the filtered rows.
/// </summary>
/// <param name="dtInput">The dt input.</param>
/// <param name="filterExpression">The filter expression.</param>
/// <returns></returns>
protected virtual DataTable FilterDataTable(DataTable dtInput, string filterExpression)
{
DataTable result = dtInput;
if (!string.IsNullOrEmpty(filterExpression) && filterExpression.Trim().Length > 0)
{
DataView view = new DataView(dtInput);
view.RowFilter = filterExpression;
view.Sort = HierarchyFieldMap.DisplayedValue;
result = view.ToTable();
}
return result;
}
Any idea’s on how to improve this method?
It takes ~1 second to execute.
EDIT
I found this link on DataView’s Poor Peformance with Large RecordSets
Since you’re not returning a
DataViewbut aDataTable, you should be able to get a performance boost – not order-of-magnitude, but 25-30% – by usingDataTable.Sort:Most of the time that’s being taken up there is copying the data into the new table. If you can avoid creating a new table and work with the array of
DataRows thatDataTable.Selectreturns you can get a considerable improvement.