2012-03-01 58 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