ListView: Cannot order by type 'System.Object'
I'm trying to sort a column in a ListView using Lambda Expression but, when I try to bind the result to the list view, I'm getting this error.
Here is the code, I'm trying to use.
Private Sub BindCategories(Optional ByVal SortColumn As String = Nothing, Optional ByVal SortOrder As SortDirection = SortDirection.Ascending开发者_如何学运维)
Dim DS As IQueryable(Of DataEntities.Category) = DataEntities.Category.GetAllItems
If Not String.IsNullOrEmpty(SortColumn) Then
DS = Sort(Of DataEntities.Category)(DS.AsEnumerable, SortColumn, SortDirection.Ascending).AsQueryable
End If
lstCategories.DataSource = DS
lstCategories.DataBind()
End Sub
and here is the code for Sort
function.
Private Function Sort(Of T)(ByVal src As IEnumerable(Of T), ByVal columnName As String, ByVal orderby As SortDirection) As IEnumerable(Of T)
Dim param = Expression.Parameter(GetType(T), "item")
Dim sortExpression = Expression.Lambda(Of Func(Of T, Object))(Expression.Convert(Expression.[Property](param, columnName), GetType(Object)), param)
Select Case orderby
Case WebControls.SortDirection.Ascending
Return src.AsQueryable().OrderBy(sortExpression)
Case Else
Return src.AsQueryable().OrderByDescending(sortExpression)
End Select
End Function
The error page shows that the error occurred at lstCategories.DataBind()
Source Error:
Line 15: End If
Line 16: lstCategories.DataSource = DS
Line 17: lstCategories.DataBind() << **Error occurs at the point.**
Line 18: End Sub
Line 19:
Stack Trace
[InvalidOperationException: Cannot order by type 'System.Object'.]
System.Data.Linq.SqlClient.QueryConverter.VisitOrderBy(Expression sequence, LambdaExpression expression, SqlOrderType orderType) +908326 System.Data.Linq.SqlClient.QueryConverter.VisitSequenceOperatorCall(MethodCallExpression mc) +8217 System.Data.Linq.SqlClient.QueryConverter.VisitMethodCall(MethodCallExpression mc) +74 System.Data.Linq.SqlClient.QueryConverter.VisitInner(Expression node) +1003 System.Data.Linq.SqlClient.QueryConverter.ConvertOuter(Expression node) +79 System.Data.Linq.SqlClient.SqlProvider.BuildQuery(Expression query, SqlNodeAnnotations annotations) +114 System.Data.Linq.SqlClient.SqlProvider.System.Data.Linq.Provider.IProvider.Execute(Expression query) +132 System.Data.Linq.DataQuery1.System.Collections.Generic.IEnumerable<T>.GetEnumerator() +35 System.Collections.Generic.List
1..ctor(IEnumerable1 collection) +7667686 System.Linq.Enumerable.ToList(IEnumerable
1 source) +61 System.Data.Linq.Provider.BindingList.Create(DataContext context, IEnumerable1 sequence) +53 System.Data.Linq.DataQuery
1.GetNewBindingList() +51 System.Data.Linq.DataQuery`1.System.ComponentModel.IListSource.GetList() +15 System.Web.UI.DataSourceHelper.GetResolvedDataSource(Object dataSource, String dataMember) +50 System.Web.UI.WebControls.ReadOnlyDataSource.System.Web.UI.IDataSource.GetView(String viewName) +41 System.Web.UI.WebControls.DataBoundControl.ConnectToDataSourceView() +128 System.Web.UI.WebControls.DataBoundControl.GetData() +4 System.Web.UI.WebControls.DataBoundControl.PerformSelect() +60 System.Web.UI.WebControls.ListView.PerformSelect() +57 System.Web.UI.WebControls.BaseDataBoundControl.DataBind() +73 Admin_Categories.BindCategories(String SortColumn, SortDirection SortOrder) in F:\Work\Under Development\Websites\Smile\Smile-Deploy\Admin\Categories.aspx.vb:17 Admin_Categories.lstCategories_Sorting(Object sender, ListViewSortEventArgs e) in F:\Work\Under Development\Websites\Smile\Smile-Deploy\Admin\Categories.aspx.vb:65 System.Web.UI.WebControls.ListView.OnSorting(ListViewSortEventArgs e) +110 System.Web.UI.WebControls.ListView.HandleSort(String sortExpression, SortDirection sortDirection) +57 System.Web.UI.WebControls.ListView.HandleSort(String sortExpression) +52 System.Web.UI.WebControls.ListView.HandleEvent(EventArgs e, Boolean causesValidation, String validationGroup) +268 System.Web.UI.WebControls.ListView.OnBubbleEvent(Object source, EventArgs e) +233 System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +37 System.Web.UI.WebControls.LinkButton.OnCommand(CommandEventArgs e) +118 System.Web.UI.WebControls.LinkButton.RaisePostBackEvent(String eventArgument) +135 System.Web.UI.WebControls.LinkButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10 System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13 System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +175 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1565
There is no comparison defined for the type Object
. An object can be anything, how would you compare two objects without knowing what they are? For instance, you can't compare an int and a string... which is greater between 42
and "Hello world"
?
So you need to know what you're comparing...
精彩评论