コード例 #1
0
        public static Task <DtResult <TResult> > GetResultDatatablesAsync <TResult, TEntity>(IQueryable <TEntity> query,
                                                                                             string search, int?draw, string sortOrder, int?start, int?length, List <string> columnFilters) where TResult : BaseDto where TEntity : BaseEntity
        {
            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 ? filter.SortBy(sortOrder)
                //.Skip(start ?? 0)
                //.Take(length ?? 0).ToListAsync().Result : Activator.CreateInstance(typeof(List<>).MakeGenericType(typeof(TResult)));

                var resultFilter = type != null?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;
            }
        }