【问题标题】:Function Import Entity Framework orderby函数导入实体框架 orderby
【发布时间】:2012-03-01 17:31:15
【问题描述】:

我正在尝试使用我的 objectdatasource 对 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

哪个有这个代码:

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

如果我正在调用存储过程,我该如何执行 OrderBy?

我的代码:

  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

【问题讨论】:

    标签: function gridview frameworks import entity


    【解决方案1】:

    要么 1)在存储过程中排序,或者 2) 使用Linq to objects 对返回的数据进行排序。

    例如

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

    显然 2) 将对可能不是您需要的结果进行排序,例如,如果您的 sproc 返回 top(n)。

    【讨论】:

    • 我尝试使用您提供的代码行,但智能感知不适用于“OrderBy”功能。在将值返回给我的应用程序层后,我会使用它吗?该代码位于返回 IEnumerable(Of usp_GetReport_Result) 并通过我的 objectdatasource 的 SelectMethod 连接的函数中
    • 实际上,我打算对结果进行排序。确切地说,我想在单击列标题时对网格视图进行排序。
    • 你需要相当于 C# 的“using System.Linq;”,可能是“import System.Linq”
    • 仍然没有显示 retReq 的 OrderBy 函数。我必须指定排序的类型吗?我将其命名为 Dim sorted = Nothing 。我应该在retReq = dataContext.usp_GetReport(...).ToList() 之后做吗?还是在通话之外? ...谢谢,非常感谢您的帮助。
    • 我很确定这可行,但我的函数导入可能有不同的场景,这实际上没有意义。但我现在已经发布了我的手动修复。因为它适用于我的 gridview 排序。谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-24
    • 2012-01-01
    • 2018-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多