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 DataView
but a DataTable
, you should be able to get a performance boost - not order-of-magnitude, but 25-30% - by using DataTable.Sort
:
private static DataTable SortDataTable(DataTable t,
string filterExpression,
string sortExpression)
{
DataTable t1 = t.Clone();
t1.BeginLoadData();
foreach (DataRow r in t.Select(filterExpression, sortExpression))
{
t1.Rows.Add(r.ItemArray);
}
t1.EndLoadData();
return t1;
}
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 DataRow
s that DataTable.Select
returns you can get a considerable improvement.