public class OrdersController : Controller
{
// GET: api/Orders
[HttpGet]
public object Get()
{
var queryString = Request.Query;
var data = OrdersDetails.GetAllRecords().ToList();
string filter = queryString["$filter"];
string auto = queryString["$inlineCount"];
// filtering
if (filter != null)
{
var newfiltersplits = filter;
var filtersplits = newfiltersplits.Split('(', ')', ' ');
var filterfield = filtersplits[1];
var filtervalue = filtersplits[3];
if (filtersplits.Length == 5)
{
if (filtersplits[1] == "tolower")
{
filterfield = filter.Split('(', ')', '\'')[2];
filtervalue = filter.Split('(', ')', '\'')[4];
}
}
if (filtersplits.Length != 5)
{
filterfield = filter.Split('(', ')', '\'')[3];
filtervalue = filter.Split('(', ')', '\'')[5];
}
switch (filterfield)
{
case "CustomerID":
data = (from cust in data
where cust.CustomerID.ToLower().StartsWith(filtervalue.ToString())
select cust).ToList();
break;
}
}
// paging
int skip = Convert.ToInt32(queryString["$skip"]);
int take = Convert.ToInt32(queryString["$top"]);
if (auto == null)
{
return new { Items = data };
}
else
{
return take != 0 ? new { Items = data.Skip(skip).Take(take).ToList(), Count = data.Count() } : new { Items = data, Count = data.Count() };
}
} |
<ejs-grid id="GridOverview" allowFiltering="true" allowPaging="true">
<e-data-manager url="/Home/UrlDataSource" adaptor="UrlAdaptor"></e-data-manager>
<e-grid-pagesettings pageSize="4"></e-grid-pagesettings>
<e-grid-filtersettings type="Menu"></e-grid-filtersettings>
<e-grid-columns>
<e-grid-column field="OrderID" headerText="Order ID" isPrimaryKey="true" textAlign="Right" width="100"></e-grid-column>
<e-grid-column field="CustomerID" headerText="Customer ID" type="string" width="120"></e-grid-column>
<e-grid-column field="Freight" headerText="Freight" textAlign="Right" format="C2" editType="numericedit" width="120"></e-grid-column>
<e-grid-column field="ShipCountry" headerText="Ship Country" width="150"></e-grid-column>
</e-grid-columns>
</ejs-grid>
|
...
public class HomeController : Controller
{
[HttpPost]
public IActionResult UrlDatasource([FromBody]DataManagerRequest dm)
{
IEnumerable DataSource = OrdersDetails.GetAllRecords();
DataOperations operation = new DataOperations();
if (dm.Search != null && dm.Search.Count > 0)
{
DataSource = operation.PerformSearching(DataSource, dm.Search); //Search
}
if (dm.Sorted != null && dm.Sorted.Count > 0) //Sorting
{
DataSource = operation.PerformSorting(DataSource, dm.Sorted);
}
if (dm.Where != null && dm.Where.Count > 0)
{
DataSource = operation.PerformFiltering(DataSource, dm.Where, dm.Where[0].Operator); //Filtering
}
int count = DataSource.Cast<OrdersDetails>().Count();
if (dm.Skip != 0)
{
DataSource = operation.PerformSkip(DataSource, dm.Skip); //Paging
}
if (dm.Take != 0)
{
DataSource = operation.PerformTake(DataSource, dm.Take);
}
return dm.RequiresCounts ? Json(new { result = DataSource, count = count }) : Json(DataSource);
}
...
}
... |
Index.cshtml
<ejs-grid id="Grid" allowPaging="true" allowFiltering="true" toolbar="@(new List<string>() { "Add", "Edit", "Delete", "Update", "Cancel" })">
<e-grid-filterSettings type="Menu"></e-grid-filterSettings>
<e-grid-editSettings allowAdding="true" allowDeleting="true" allowEditing="true" mode="Normal" showConfirmDialog="true" showDeleteConfirmDialog="true"></e-grid-editSettings>
<e-data-manager url="/Home/TelecomDataSource" insertUrl="/Home/Insert" updateUrl="/Home/Update" removeUrl="/Home/Delete" adaptor="UrlAdaptor"></e-data-manager>
<e-grid-columns>
. . . . . . .
. . . . . . .
</e-grid-columns>
</ejs-grid>
|
HomeController.cs
public ActionResult TelecomDataSource([FromBody]DataManagerRequest dataManager)
{
IEnumerable data = _context.Orders.ToList();
DataOperations operation = new DataOperations();
if (dataManager.Search != null && dataManager.Search.Count > 0)
{
data = operation.PerformSearching(data, dataManager.Search); //Search
}
if (dataManager.Sorted != null && dataManager.Sorted.Count > 0) //Sorting
{
data = operation.PerformSorting(data, dataManager.Sorted);
}
if (dataManager.Where != null && dataManager.Where.Count > 0)
{
data = operation.PerformFiltering(data, dataManager.Where, dataManager.Where[0].Operator); //Filtering
}
int count = data.Cast<Orders>().Count();
if (dataManager.Skip != 0)
{
data = operation.PerformSkip(data, dataManager.Skip);
}
if (dataManager.Take != 0)
{
data = operation.PerformTake(data, dataManager.Take);
}
return dataManager.RequiresCounts ? Json(new { result = data, count = count }) : Json(data);
}
|
Hi
In solution above, in line
IEnumerable data = _context.Orders.ToList();
you are loading all data from database to server memory.
What if there will be millions of records?
Can you filter data without loading it?
In EntityFramework, by using operations on IQueryable, not ToList(), so the query can be processed by database, not on www server?
Index.cshtml
<ejs-grid id="Grid" height="273" allowPaging="true" allowFiltering="true">
<e-data-manager url="/Home/UrlDataSource" adaptor="UrlAdaptor"></e-data-manager>
<e-grid-filterSettings type="Excel"></e-grid-filterSettings>
<e-grid-columns>
<e-grid-column field="CustomerID" headerText="Customer ID" isPrimaryKey="true" textAlign="Right" width="100"></e-grid-column>
<e-grid-column field="ContactName" headerText="Contact Name" type="string" width="120"></e-grid-column>
<e-grid-column field="ContactTitle" headerText="ContactTitle" textAlign="Right" width="120"></e-grid-column>
<e-grid-column field="City" headerText="Ship City" width="150"></e-grid-column>
</e-grid-columns>
</ejs-grid> |
[Homecontroller.cs]
public IActionResult UrlDatasource([FromBody]DataManagerRequest dm)
{
EFDataContext db = new EFDataContext();
IQueryable<Customers> DataSource = db.customers;
QueryableOperation operation = new QueryableOperation();
if (dm.Where != null && dm.Where.Count > 0) //Filtering
{
DataSource = operation.PerformFiltering(DataSource, dm.Where, dm.Where[0].Operator);
}
int count = DataSource.Count();
if (dm.Skip != 0)
{
DataSource = operation.PerformSkip(DataSource, dm.Skip); //Paging
}
if (dm.Take != 0)
{
DataSource = operation.PerformTake(DataSource, dm.Take);
}
return dm.RequiresCounts ? Json(new { result = DataSource, count = count }) : Json(DataSource);
} |