Hi,
I have implemented server-side paging and filtering in my grid.
This is my C# method...
public ActionResult DataSource(DataManagerRequest dm)
{
IEnumerable data = db.RegisteredVoters;
DataOperations operation = new DataOperations();
if (dm.Sorted != null && dm.Sorted.Count > 0) //Sorting
{
data = operation.PerformSorting(data, dm.Sorted);
}
if (dm.Where != null && dm.Where.Count > 0) //Filtering
{
WhereFilter whereFilter = dm.Where.First();
data = operation.PerformFiltering(data, dm.Where, whereFilter.Operator);
}
int count = data.Cast<RegisteredVoter>().Count();
if (dm.Skip != 0)
{
data = operation.PerformSkip(data, dm.Skip);
}
if (dm.Take != 0)
{
data = operation.PerformTake(data, dm.Take);
}
return Json(new { result = data, count = count });
}
The line that I have underlined, converts the ENTIRE search results into a list and then gets the count. This is very inefficient when my search returns 1 million records. This completely negates the reason I have implemented server-side paging and filtering.
Is there any way to ONLY get the count of the current filter query without converting all the items into a list?
Please do let me know.
Regards,
pixie.
Hi,
I don't think you have answered my question.
I do know that the count is required for the paging component and I do know that calling the .count() LINQ method iterates ALL the records and gets the count. These two points I already know.
But all the records are not required to be returned for the paging component to work, it only needs the count correct?
I am asking you if there is any functionality that your DataOperations class can provide to achieve this since we both know that iterating 1 million records to get the count is not very efficient.
The DataOperations class is already generating the query from the filters selected. It would be much more efficient and useful if that class also runs a "count *" query with those same filters so that iterating to get the count is then not required.
If you force the users to call the .count() method on the entire result set, then you are negating the whole concept of server-side paging.
Do you understand what I am trying to say?
Is there any solution to this?
Regards,
pix.
Hi,
I'
I'm sorry, but what you said there, is incorrect. You said, "But the data count retrieval is a common case where the count has to be fetched by executing the entire data."
That is not correct as the count may be fetched in two ways as I have mentioned below.
1) The inefficient way:
1.1) Execute the query in question, SELECT [columns] from [table name] where [filter clauses]
1.2) Iterate through all the records and get the count
2) What I am suggesting:
2.1) Have a functionality to get the count of the query directly by executing, SELECT count(*) from [table name] where [filter clauses]
In the first method, if the page size is 10, and the query returns 1 million records, all 1 million records are returned from the server, just to get the count. This cannot be right!
In the second method, two queries are executed, one which returns just the count as a number, and another that returns just the 10 records. This will be orders of magnitude faster than the first method when large data is concerned.
What I am asking is, does the functionality for the second method currently exist? And if it does not exist, then can it be implemented?
Regards,
pix.
|
IQueryable<Orders> DataSource = _context.Orders.AsQueryable();
QueryableOperation operation = new QueryableOperation(); |