开发者

Gridview filter and sorting using LINQ

I'm upgrading a project to .net 4. My GridViews are using DataSets and implement filtering, sorting and paging using an ObjectDataSource.

What is the best practice to connect a GridView to a Linq query and implement sorting, filtering and paging?

Do i still use a DataSet and ObjectDataSource or is there a way to use another type of DataSource that let's me connect directly to a LINQ query result which handles the filtering, sorting and paging for me?

Edit:

There's busineseslogic in between the UI and DatabaseContext therefore connecting directly to my ORM isn't an option. It has to be against a LINQ query result (IQueryable). I'm using separate Insert/Update/Delete fun开发者_运维技巧ctions to handle business logic before talking to the ORM.


What you can do is keep your ObjectDataSource (ODS) but take the logic that you use to retrieve the data and put it into a class that uses Linq (or any other data access technology) to retrive the data for you.

To do this in the ObjectDataSource you specifiy the name of the class that will be doing the work for you e.g.

<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" 
                          TypeName="CustomDAL" 
                          SelectMethod="GetData"/>

This will make the ODS create an instance of the CustomDAL class and call the GetData method.

Sorting and paging are supported as well but its probably best to implement the actual logic yourself in relation to restricting data that is retrieved.

To enable paging you add EnablePaging="True" to the ODS declaration and on your SelectMethod you add StartRowIndex and PageSize handling them in your linq using the Skip and Take methods to give you the right records e.g.

var results= (from user in context.users 
          where user.UserId == userId
          select user).Skip(StartRowIndex).Take(PageSize).ToList()
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜