Using LinqDataSource with linq to sharepoint for paging

1.3k views Asked by At

I am developing a a Sharepoint 2010 visual webpart and I am trying to use LinqDataSource in it to handle paging and sorting in a GridView. I made my datacontext and entity objects with spmetal. and now this is my code:

my mark up:

<%@ Register TagPrefix="asp" Namespace="System.Web.UI.WebControls" Assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" %>

<asp:LinqDataSource runat="server" ID="LinqDataSource1" OnSelecting="MySelecting" />
<asp:GridView ID="GridView1" runat="server"  AllowPaging="True" PageSize="3" 
    AutoGenerateColumns="False" DataSourceID="LinqDataSource1" 
    EnableModelValidation="True">
    <Columns>
        <asp:BoundField DataField="title" HeaderText="Title" />

    </Columns>
</asp:GridView>

and my code:

protected void MySelecting(object sender, LinqDataSourceSelectEventArgs e)
{


    TestEntitiesDataContext dc = new TestEntitiesDataContext("http://sp/sites/test");

    e.Result = from item in dc.TestList
               select new
                 {
                     title = item.Title,
                     numberField = item.NumberField.ToString()
                 };


}

Now the problem is when I try to view the webpart on the site I get this error: Expression of type 'System.Int32' cannot be used for return type 'System.Object'

When I deactivate paging on grid view this error disappears.

Do you have any idea why this is happening?

I 'd be grateful of any help.

1

There are 1 answers

0
Gennadiy Pochtar On

for me the following work around has helped:

LinqDataSource1.AutoPage = false;
LinqDataSource1.AutoSort = false;

in MySelecting:

e.Result = [your nice LINQ expression].Skip(e.Arguments.StartRowIndex).Take(e.Arguments.MaximumRows);
e.Arguments.TotalRowCount = [your nice LINQ expression].Count();

And if you need sorting, you should also process e.Arguments.SortExpression.