ListView:无法按类型'System.Object'排序

时间:2011-07-19 08:57:01

标签: asp.net vb.net linq listview sorting

我正在尝试使用Lambda Expression对ListView中的列进行排序,但是当我尝试将结果绑定到列表视图时,我收到此错误。

以下是我正在尝试使用的代码。

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

这是Sort函数的代码。

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

错误页面显示错误发生在lstCategories.DataBind()

来源错误:

Line 15:         End If  
Line 16:         lstCategories.DataSource = DS 
Line 17:         lstCategories.DataBind()  << **Error occurs at the point.** 
Line 18:     End Sub 
Line 19:

堆栈跟踪

  

[InvalidOperationException:无法按类型'System.Object'排序。]
  System.Data.Linq.SqlClient.QueryConverter.VisitOrderBy(表达式   sequence,LambdaExpression表达式,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(表达式   节点)+79
  System.Data.Linq.SqlClient.SqlProvider.BuildQuery(表达式查询,   SqlNodeAnnotations注释)+114
  System.Data.Linq.SqlClient.SqlProvider.System.Data.Linq.Provider.IProvider.Execute(表达式   查询)+132
  System.Data.Linq.DataQuery 1.System.Collections.Generic.IEnumerable<T>.GetEnumerator() +35 System.Collections.Generic.List 1..ctor(IEnumerable的1 collection) +7667686 System.Linq.Enumerable.ToList(IEnumerable 1   来源)+61
  System.Data.Linq.Provider.BindingList.Create(DataContext context,   IEnumerable 1 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(字符串   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   在F:\ Work \ Under中的SortOrder)   发展\网站\微笑\微笑,部署\管理\ Categories.aspx.vb:17
  Admin_Categories.lstCategories_Sorting(对象发件人,   ListViewSortEventArgs e)在F:\ Work \ Under中   发展\网站\微笑\微笑,部署\管理\ 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 causeValidation,String validationGroup)+268
  System.Web.UI.WebControls.ListView.OnBubbleEvent(Object source,   EventArgs e)+233 System.Web.UI.Control.RaiseBubbleEvent(Object   来源,EventArgs args)+37
  System.Web.UI.WebControls.LinkBut​​ton.OnCommand(CommandEventArgs e)   +118 System.Web.UI.WebControls.LinkBut​​ton.RaisePostBackEvent(String   eventArgument)+135
  System.Web.UI.WebControls.LinkBut​​ton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(字符串   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(布尔值   includeStagesBeforeAsyncPoint,Boolean includeStagesAfterAsyncPoint)   1565

1 个答案:

答案 0 :(得分:1)

没有为类型Object定义比较。一个对象可以是任何东西,你如何比较两个对象而不知道它们是什么?例如,你不能比较一个int和一个字符串......在42"Hello world"之间哪个更大?

所以你需要知道你在比较什么...