DevExpress MVC Gridview + LINQ

L2Eer picture L2Eer · Jan 25, 2012 · Viewed 9.6k times · Source

We have a DX gridview being rendered in a specifically designed view. We pass a predefined ViewModel object whose values are filled from a Linq-2-Entities query. The problem is that in our callback function the L2E query is actually executed on the DB before any filtering, sorting and paging has been performed by the DevExpress grid. IE.: (simplified example, in the actual situation we select data from several tables but still in a single linq query)

public ActionResult GridViewPartial(string myParameters) 
{
    var modelData = from s in db.myTable 
                                     select new { modelName = s.Name };

    return PartialView("GridViewPartial", modelData);
}

In this situation the query is actually executed before the data has been passed to the View. Therefore it actually selects way too much data from the DB while the gridview only displays the selected page.

How would we have to modify the query so it only selects the data of the page the user has selected? IE. skip 10 rows and take 10 in the L2E query when the user selects page 2, instead of selecting 100000 rows (if there are that many in the table) and afterwards applying the filtering/sorting/paging, like in the described situation?

Answer

Mikhail picture Mikhail · Jan 26, 2012

The MVC GridView Extension supports the so-called “server mode” functionally via the internal LinqServerModeDataSource object.

It requires the IQueryable object as a datasource:

Direct LINQ query:

http://www.devexpress.com/issue=Q333116

@Html.DevExpress().GridView(...).BindToLINQ(string.Emptry, string.Emptry, (s, e) => { e.KeyExpression = Key_Column_Here; e.QueryableSource = Linq_Query_Here; }

The Table/View from the LinqToX DataCotnext/Classes;

http://mvc.devexpress.com/GridView/DataBindingToLargeDatabase

@Html.DevExpress().GridView(...).BindToLINQ(Data_Context_Name_Here, Table_View_Name_Here).GetHtml()