2012-03-01 52 views
0

我想排序gridview的我的ObjectDataSource從我的實體模型使用函數導入(存儲過程)。我是跟着導遊是從 http://www.asp.net/web-forms/tutorials/continuing-with-ef/using-the-entity-framework-and-the-objectdatasource-control-part-3-sorting-and-filtering功能導入實體框架的OrderBy

其中有這樣的代碼:

public IEnumerable<Department> GetDepartments(string sortExpression) 
    { 
     if (String.IsNullOrWhiteSpace(sortExpression)) 
     { 
      sortExpression = "Name"; 
     } 
     return context.Departments.Include("Person").OrderBy("it." + sortExpression).ToList(); 
    } 

我怎樣才能做一個排序依據,如果我調用存儲過程?

我的代碼:

If String.IsNullOrWhiteSpace(sortExpression) Then 
       sortExpression = "Status" 
      End If 

      retReq = dataContext.usp_GetReport(Nothing, Nothing, Nothing, period, Nothing, Nothing, _ 
          Nothing, Nothing, Nothing).ToList() 

任何想法?在此先感謝

更新 - 分辨率

我敢肯定,有使排序功能的工作在我的情況的更好的方法。但是,這是我做的方法,它的工作原理,但如果有人可以幫助我簡化它,請讓我知道謝謝。

從MSDN的文檔http://msdn.microsoft.com/en-us/library/bb534966(v=vs.96).aspx#Y1200

If sortExpression IsNot Nothing Then 
       If sortExpression = "StatusDate DESC" Then 
        retReq = dataContext.usp_GetReport(Nothing, Nothing, Nothing, period, Nothing, Nothing, _ 
         Nothing, Nothing, Nothing).OrderByDescending(Function(test As usp_GetReport_Result) test.StatusDate).ToList() 

       ElseIf sortExpression = "StatusDate" Then 
        retReq = dataContext.usp_GetReport(Nothing, Nothing, Nothing, period, Nothing, Nothing, _ 
         Nothing, Nothing, Nothing).OrderBy(Function(test As usp_GetReport_Result) test.StatusDate).ToList() 

       End If 
End If 

回答

0

要麼 1)排序衍生的存儲過程中,或 2)使用LINQ對象返回的數據進行排序。

例如

var sorted = retReq.OrderBy(x=>x.SomeProperty); 

明顯2)將可能不是您所需要的,如果比如你的存儲過程將返回頂部(n)的結果進行排序。

+0

我嘗試使用您所提供的代碼行,但智能感知是不提供的「排序依據」功能。在將值返回給我的應用程序層後,我會使用它嗎?代碼是返回一個IEnumerable(中usp_GetReport_Result),並通過我的ObjectDataSource的SelectMethod – beachbum320 2012-03-01 17:45:29

+0

而實際上連接的功能裏面,排序的結果是什麼,我打算做的。確切地說,我想在點擊列標題時對gridview進行排序。 – beachbum320 2012-03-01 17:50:13

+0

您需要的C#相當於‘使用System.Linq的;’大概‘進口System.Linq的’ – Phil 2012-03-01 17:53:42