# Pastebin XhDlFzqH namespace API.Services { public class EntityDataTableService : IEntityDataTableService where TEntity : EntityBase, new() { private readonly ILogger _logger; private readonly IMapper _mapper; private WuitCMDBDbContext _context; string errorMessage = string.Empty; public EntityDataTableService( WuitCMDBDbContext context, ILogger logger, IMapper mapper) { this._context = context; _logger = logger; _mapper = mapper; } public DataTableResponseJsonModel JQueryDataTable( DataTablesAjaxPostViewModel dataTableRequest, Expression> customFilterPredicate = null) { // if columns[i].search is populated, but stringProperties is null, we are just going to ignore the search. // Maybe we should throw a warning so the developer can be made aware that searches are taking place but are not configured? if (dataTableRequest == null) { throw new ArgumentNullException(nameof(dataTableRequest)); } if (dataTableRequest.order == null || dataTableRequest.order.Count <= 0) { throw new ArgumentNullException(nameof(dataTableRequest.order)); } IQueryable query = _context.Set(); var projectedQuery = query .AsNoTracking() .ProjectTo(); // // Global filtering // if(customFilterPredicate != null) { projectedQuery = projectedQuery.Where(customFilterPredicate); } // // Column Searching // if (stringProperties != null) { foreach (var column in dataTableRequest.columns.Where(x => x.searchable && !string.IsNullOrWhiteSpace(x.search.value))) { projectedQuery = projectedQuery .Search( ... ) // PROBLEM. All I have is `dataTableRequest.columns[i].name`, I also only have TViewModel, so I am unable to configure the properties of TViewModel I want to search against. // Should I move this up and out so the caller (controller action) can configure it knowing the real type? Is there some magic reflection I can use here? .Containing(column.search.value); } } // // Ordering // foreach (var orderRequest in dataTableRequest.order) { var orderColumn = dataTableRequest.columns[orderRequest.column]; if (orderColumn.orderable) { var orderColumnsName = !string.IsNullOrWhiteSpace(orderColumn.name) ? orderColumn.name : orderColumn.data; projectedQuery = projectedQuery.OrderBy($"{orderColumnsName} {orderRequest.dir}"); } } // // Paging // var responseViewModel = projectedQuery .ToPagedList(dataTableRequest.StartPage, dataTableRequest.length) // Query Execution .ToResponseViewModel(query.Count(), dataTableRequest.draw); // Query Execution return responseViewModel; } } }