如何更改List(Of Order)的顺序并进行分页

时间:2015-05-18 23:27:10

标签: asp.net .net vb.net

我的代码需要帮助。

我的目标是能够使用两个按钮的点击事件来浏览订单列表。此订单列表也可以使用通过Query-string传递的参数进行重新排序。

目前我可以进行分页,但不能100%准确地工作。

例如,使用我当前的代码,如果我有一个包含以下订单的电子邮件列表:

J.email.com
R.email.com
R.email.com
R.email.com
T.email.com

我的分页代码将按照以下顺序浏览记录:

T.email.com - previous
J.email.com - current
R.email.com - next
R.email.com - next
R.@email.com - next

如果我将订单更改为降序:

T.email.com
R.email.com
R.email.com
R.email.com
J.email.com

分页代码将以:

开头
T.email.com
J.email.com
R.email.com
R.email.com
R.email.com

如果我在记录时点击上一个' T @email.com'然后我得到一个页面,用户会收到通知说没有更多要浏览的记录,但是如果我点击“下一步”按钮。按钮,然后我转到以下记录:' J @email.com',但整个集合和分页变为:

T.email.com
J.email.com
R.email.com
R.email.com
R.email.com

这是我到目前为止的代码:

Protected Sub btnUp_Click(sender As Object, e As EventArgs) Handles btnUp.Click

Dim currentOrderID As String
Dim currentOrder As Order
Dim nextOrder As Order = Nothing

SiteKey = Request.QueryString("siteKey")
strEnv = Request.QueryString("strEnv")
currentOrderID = Request.QueryString("order_id")
strSortBy = Request.QueryString("SortBy")
strSortDirection = Request.QueryString("SortDirection")

orderList = New Orders(SiteKey)
orderList.GetOrdersByDate(CDate(Request.QueryString("Start")), CDate(Request.QueryString("End")).AddDays(1), strEnv)

If Not String.IsNullOrEmpty(strSortBy) Then
    orderList.Items.AsQueryable.OrderBy(strSortBy + " " + strSortDirection)
End If

currentOrder = (From order In orderList.Items
                Where order.OrderID.Equals(currentOrderID)
                Select order).FirstOrDefault()

If currentOrder IsNot Nothing Then
    Dim index = orderList.Items.IndexOf(currentOrder)
    If Not index <= 0 Then _
        nextOrder = DirectCast(orderList.Items(index - 1), Order)
        If nextOrder IsNot Nothing Then
            btnUp.Visible = True
            Response.Redirect("~/order-review.aspx?order_id=" & nextOrder.OrderID & "&siteKey=" & SiteKey & "&strEnv=" & strEnv & "&Start=" & CDate(Request.QueryString("Start")) & "&End=" & CDate(Request.QueryString("End")) & "&SortBy=" & strSortBy &                                   "&SortDirection=" & strSortDirection)
        Else
            btnUp.Visible = False
            lblnoRecs.Visible = True
            lblnoRecs.Text = "No records to display. Click the Previous button to go to the next available order, or the link below to go to the search page."
            lnkgoBack.Visible = True
            lnkgoBack.Text = "Web"
            lnkgoBack.NavigateUrl = Page.ResolveUrl("~/order-history.aspx?&siteKey=" & SiteKey & "&strEnv=" & strEnv & "&Start=" & Request.QueryString("Start") & "&End=" & Request.QueryString("End"))
        End If
    End If

End Sub

Protected Sub btnDown_Click(sender As Object, e As EventArgs) Handles btnDown.Click

Dim currentOrderID As String = String.Empty
Dim previousOrder As Order = Nothing

SiteKey = Request.QueryString("siteKey")
strEnv = Request.QueryString("strEnv")
currentOrderID = Request.QueryString("order_id")
strSortBy = Request.QueryString("SortBy")
strSortDirection = Request.QueryString("SortDirection")

orderList = New Orders(SiteKey)
orderList.GetOrdersByDate(CDate(Request.QueryString("Start")), CDate(Request.QueryString("End")).AddDays(1), strEnv)

Dim temporders As System.Collections.Generic.List(Of Order) = orderList.Items

If Not String.IsNullOrEmpty(strSortBy) Then
    orderList.Items.AsQueryable.OrderBy(strSortBy + " " + strSortDirection)
End If

Dim currentOrder = (From order In orderList.Items
                    Where order.OrderID.Equals(currentOrderID)
                    Select order).FirstOrDefault()

If currentOrder IsNot Nothing Then
    Dim index = orderList.Items.IndexOf(currentOrder)
    If Not index >= orderList.Items.Count - 1 Then _
       previousOrder = DirectCast(orderList.Items(index + 1), Order)
       If previousOrder IsNot Nothing Then
           btnDown.Visible = True
           Response.Redirect("~/order-review.aspx?order_id=" & previousOrder.OrderID & "&siteKey=" & spiderSiteKey & "&strEnv=" & strEnv &"&Start=" & CDate(Request.QueryString("Start")) & "&End=" & CDate(Request.QueryString("End")) & "&SortBy=" & strSortBy & "&SortDirection=" & strSortDirection)
        Else
            btnDown.Visible = False
            lblnoRecs.Visible = True
            lblnoRecs.Text = "No records to display. Click the Next button to go to the next available order, or the link below to go to the search page."
            lnkgoBack.Visible = True
            lnkgoBack.Text = "Web"
            lnkgoBack.NavigateUrl = Page.ResolveUrl("~/order-history.aspx?&siteKey=" & SiteKey & "&strEnv=" & strEnv & "&Start=" & Request.QueryString("Start") & "&End=" & Request.QueryString("End"))
        End If
    End If
End Sub

如果没有属性和方向的排序参数,则应忽略此代码:

If Not String.IsNullOrEmpty(strSortBy) Then
    orderList.Items.AsQueryable.OrderBy(strSortBy + " " + strSortDirection)
End If

我是否有可能获得纠正此代码的指导?

谢谢。

更新

我尝试重新排序后重新分配订单列表:

orderList = orderList.Items.AsQueryable.OrderBy(strSortBy + " " + strSortDirection)

但是我收到了一个错误:

An exception of type 'System.InvalidCastException' occurred in 
App_Web_order-review.ascx.cc671b29.h0sxd1rw.dll but was not handled in user 
code

Additional information: Unable to cast object of type System.Linq.EnumerableQuery`1[Order]' to type 'Orders'.

更新

我必须转换为list,例如:

orderList.Items = orderList.Items.AsQueryable.OrderBy(strSortBy + " " + strSortDirection).ToList

我认为分页发生的顺序仍然不正确。喜欢在原帖中描述。

1 个答案:

答案 0 :(得分:0)

orderby返回一个集合,所以你应该保存它:

If Not String.IsNullOrEmpty(strSortBy) Then
    orderList = orderList.Items.AsQueryable.OrderBy(strSortBy + " " + strSortDirection)
End If