public static IQueryable <T> ApplyFilterAndSort <T>(IQueryable <T> query, ModelProperty[] properties,
                                                     DataTablesRequest dtRequest)
 {
     query = ApplyTableFilter(query, properties, dtRequest);
     query = ApplyColumnFilter(query, properties, dtRequest);
     query = ApplyOrderBy(query, properties, dtRequest);
     return(query);
 }
 private static IQueryable <T> ApplyColumnFilter <T>(IQueryable <T> query, ModelProperty[] properties,
                                                     DataTablesRequest dtRequest)
 {
     foreach (var sc in dtRequest.Columns.Where(s => s.Searchable && s.Search.Value != "").Select(s => s))
     {
         var searchValue = sc.Search.Value;
         var property    = properties.First(c => c.Name == sc.Data);
         var builder     = new DataTablesWhereBuilder();
         builder.AddFilter(property, searchValue);
         query = query.Where(string.Join(" or ", builder.DynamicLinqString),
                             builder.DynamicLinqParameters.ToArray());
     }
     return(query);
 }
        public DatatablesRepsonse GetResponse <TSource>(IQueryable <TSource> query, DataTablesRequest param)
        {
            var totalRecords = query.Count(); //Execute this query

            var modelProperties = ModelProperties <TSource> .Properties;

            query = DataTablesFiltering.ApplyFilterAndSort(query, modelProperties, param);
            var totalDisplayRecords = query.Count(); //Execute this query

            var skipped = query.Skip(param.Start);
            var data    = (param.Length <= 0 ? skipped : skipped.Take(param.Length)).ToArray(); //Execute this query

            return(new DatatablesRepsonse
            {
                recordsTotal = totalRecords,
                recordsFiltered = totalDisplayRecords,
                draw = param.Draw,
                data = data.Cast <object>().ToArray(),
            });
        }
        private static IQueryable <T> ApplyOrderBy <T>(IQueryable <T> query, ModelProperty[] columns,
                                                       DataTablesRequest dtParameters)
        {
            var sortString = "";

            for (var i = 0; i < dtParameters.Order.Count(); i++)
            {
                var columnNumber = dtParameters.Order[i].Column;
                var sortDir      = dtParameters.Order[i].Dir;
                var columnName   = dtParameters.Columns[columnNumber].Data;
                if (i != 0)
                {
                    sortString += ", ";
                }
                sortString += columnName + " " + sortDir;
            }
            if (string.IsNullOrWhiteSpace(sortString))
            {
                sortString = columns[0].Name;
            }
            query = query.OrderBy(sortString);
            return(query);
        }
        public JsonResult GetJSonResult <TSource>(IQueryable <TSource> query, DataTablesRequest param)
        {
            var result = GetResponse(query, param);

            return(new JsonResult(result));
        }