public static Task <DtResult <TResult> > GetResultQueryDatatablesAsync <TResult, TEntity>(IQueryable <TEntity> query, string search, int?draw, string sortOrder, int?start, int?length, List <string> columnFilters) where TResult : BaseDto { try { //var mapper = DependencyResolver.Current.GetService<IMapper>(); IMapper mapper = new CustomMapper(); var filter = FilterResult(search, query, columnFilters); //var count = filter.CountAsync().Result; var count = filter.Count(); if (sortOrder.StartsWith("String", StringComparison.Ordinal)) { sortOrder = sortOrder.Replace("String", ""); } var type = typeof(TEntity).GetTypeInfo().GetProperties().FirstOrDefault(a => sortOrder.StartsWith(a.Name, StringComparison.Ordinal)); var resultFilter = type != null?mapper.MapToIQueryable <TEntity, TResult>(filter) .OrderBy(a => sortOrder) .Skip(start ?? 0) .Take(length ?? 0) : Activator.CreateInstance(typeof(List <>).MakeGenericType(typeof(TResult))); var result = new DtResult <TResult> { draw = draw, data = mapper.MapTo <List <TResult> >(resultFilter), recordsFiltered = count, recordsTotal = count }; return(Task.FromResult(result)); } catch (Exception e) { Console.WriteLine(e); throw; } }