對於下面的代碼,我使用LINQ to SQL來爲綁定到ObjectDataSource的GridView分頁,並通過「ID」列進行排序。Linq to SQL:通過任意列名命令asp gridview objectdatasource
public IQueryable<MYDATATYPE> GetMYDATATYPE(int maximumRows, int startPageIndex, int startRowIndex, string orderByColumnName)
{
// page query
var query = Context.MYDATATYPEs.Skip(startRowIndex).Take(maximumRows);
// ordering by the ID column
query = query.OrderBy(x => x.ID);
return query;
}
代替通過ID列排序的,是有可能通過在orderByColumnName參數,而不是指定的列名進行排序?
我想我可能只是把「orderByColumnName」在OrderByFunction,這樣,
query = query.OrderBy(orderByColumnName);
但它給我這個錯誤:一個使用該函數的ASP對象數據源
Error CS0411 The type arguments for method 'Queryable.OrderBy(IQueryable, Expression>)' cannot be inferred from the usage. Try specifying the type arguments explicitly.
這裏:
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server"
SelectMethod="GetMYDATATYPE" SelectCountMethod="GetMYDATATYPECount"
TypeName="Paging.MyData" EnablePaging="true"
SortParameterName="OrderByColumName">
<SelectParameters>
<asp:Parameter Name="maximumRows" Type="Int32" />
<asp:Parameter Name="startPageIndex" Type="Int32" />
<asp:Parameter Name="startRowIndex" Type="Int32" />
</SelectParameters>
</asp:ObjectDataSource>