Reputation: 2404
I have a couple of datatables which contain a lot of data so i am using server side processing in order to provide the data in order to increase performance. Generally these work absolutely fine. However, the issue comes when trying to filter on the tables. It doesnt seem to respect the where clauses in my LINQ statement and i am at a loss as to why.
One of my datatable initisalisation examples looks like this:
$('#link-list').dataTable({
'bServerSide': true,
'sAjaxSource': '@Url.Action("LazyLoadComms", "Communication")',
'bProcessing': true,
async: false,
'aoColumns': [
{
'mDataProp':'Id'
},
{
'mDataProp': 'Customer'
},
{
'mDataProp': 'Receiver'
},
{
'mDataProp': 'PartNo'
},
{
'mDataProp': 'DateOpened'
}
],
bAutoWidth: false,
bLengthChange: false,
pageLength: 10,
'order': [[4, 'desc']]
});
And the server side method is as follows:
public ActionResult LazyLoadComms(JqueryDataTableParams param)
{
var communications = _uow.CommunicationService.Get().ToList();
IEnumerable<Communication> filteredComms;
if (!string.IsNullOrEmpty(param.sSearch))
{
filteredComms = communications.Where(c => !string.IsNullOrEmpty(c.Customer.Name) ? c.Customer.Name.ToLower().Contains(param.sSearch.ToLower()) : false
|| !string.IsNullOrEmpty(c.Receiver) ? c.Receiver.ToLower().Contains(param.sSearch.ToLower()) : false
|| !string.IsNullOrEmpty(c.PartNo) ? c.PartNo.ToLower().Contains(param.sSearch.ToLower()) : false);
}
else
{
filteredComms = communications;
}
var sortColumnIndex = Convert.ToInt32(Request["iSortCol_0"]);
Func<Communication, string> orderingFunction = (c => sortColumnIndex == 0 ? c.CommunicationId.ToString() :
sortColumnIndex == 1 ? c.Customer.Name :
sortColumnIndex == 2 ? c.Receiver :
sortColumnIndex == 3 ? c.PartNo :
c.DateOpened.ToLongDateString());
var sortDirection = Request["sSortDir_0"];
if (sortDirection == "asc")
filteredComms = filteredComms.OrderBy(orderingFunction);
else
filteredComms = filteredComms.OrderByDescending(orderingFunction);
var displayedComms = filteredComms
.Skip(param.iDisplayStart)
.Take(param.iDisplayLength)
.Select(c => new
{
Id = c.CommunicationId,
Customer = c.Customer.Name,
Receiver = c.Receiver,
PartNo = c.PartNo,
DateOpened = c.DateOpened.ToShortDateString() + " " + c.DateOpened.ToShortTimeString()
});
var json = Json(new
{
sEcho = param.sEcho,
iTotalRecords = communications.Count(),
iTotalDisplayRecords = filteredComms.Count(),
aaData = displayedComms
},
JsonRequestBehavior.AllowGet);
return json;
}
They do not seem very consistent. As in this example it never returns correct part numbers and is on and off as to whether it returns other columns that match the input also.
The input is always one word without spaces and is converted to lower case so it should match but does not return correctly.
Any help is greatly appreciated.
Many Thanks!!!!
Upvotes: 1
Views: 714
Reputation: 148
Please replace below code. This might be issue of Speed. Please share if any error you got:
filteredComms = communications.Where(c => (!string.IsNullOrEmpty(c.Customer.Name) && c.Customer.Name.ToLower().Contains(param.sSearch.ToLower()))
|| (!string.IsNullOrEmpty(c.Receiver) && c.Receiver.ToLower().Contains(param.sSearch.ToLower()))
|| (!string.IsNullOrEmpty(c.PartNo) && c.PartNo.ToLower().Contains(param.sSearch.ToLower())));
Upvotes: 2