2011-07-19 79 views
1

我想使用Lambda表達式對ListView中的列進行排序,但是當我嘗試將結果綁定到列表視圖時,出現此錯誤。ListView:無法按類型'System.Object'排序

這是我正在嘗試使用的代碼。

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: 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.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 source) +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(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

+0

'GetType(Object)'可能是問題所在。 – leppie

+0

我終於訴諸於Scott Guthrie的Dynamic LINQ方法。無論如何,感謝...... –

回答

1

沒有該類型定義的比較Object。一個對象可以是任何東西,你如何比較兩個對象而不知道它們是什麼?例如,你不能比較一個int和一個字符串......它在42"Hello world"之間更大?

所以你需要知道你在比較什麼......

+0

我終於訴諸於Scott Guthrie的Dynamic LINQ方法。不管怎麼說,還是要謝謝你.... –

相關問題