如何提高数据表服务器端处理的排序和过滤性能,
我有100K记录,在我下面的例子中,我只显示了2列,但我有25列
这是我的代码
var allrecords = UserDataAccess.GetAllUsers(Uow).ToList(); // here i call ToList in order to use the count
var totalRec = allrecords.Count;
int filteredRecords;
IList<User> filter;
IEnumerable<User> sort;
// filter
if (!string.IsNullOrEmpty(param.Search.Value))
{
filter = UserSearch(param.Search.Value, allrecords).ToList();
filteredRecords = filter.Count;
}
else
{
filter = allrecords;
filteredRecords = totalRec;
}
// sort
if (param.SortOrder != null && param.Order != null)
{
sort =
Sort(filter, param.Order[0], param.SortOrder)
.Skip(param.Start)
.Take(param.Length);
}
else
{
sort = filter.Skip(param.Start)
.Take(param.Length);
}
internal static IEnumerable<User> Sort(IEnumerable<User> data, DTOrder order, string columnName)
{
var sortColumnIndex = order.Column;
Func<User, string> orderingFunction = (
c =>sortColumnIndex == 0 ? c.Name : sortColumnIndex == 1 ?
c.IC :ortColumnIndex == 2 ? c.Age :"");
var sortDirection = order.Dir.ToString().ToLower(); // asc or desc
return sortDirection == "asc" ? data.OrderBy(orderingFunction) : data.OrderByDescending(orderingFunction);
}
internal static IEnumerable<User> UserSearch(string search, IEnumerable<User> records)
{
return records
.Where(c => c.Name.ToLower().Contains(search.ToLower())
||
c.FuelAssembly.Lot != null && c.IC .ToString().ToLower().Contains(search.ToLower())
); ;
}
我怎样才能改善这一点?我还有以下代码用于排序,但我的id给出了错误
public static IOrderedQueryable<TSource> Sort<TSource, TKey>(this IQueryable<TSource> source, Func<TSource, TKey> keySelector, bool descending = false)
{
if (descending) return source.OrderByDescending(keySelector);
else return source.OrderBy(keySelector);
}
无法将类型'System.Linq.IOrderedEnumerable'隐式转换为'System.Linq.IOrderedQueryable'
排序和过滤需要很长时间