Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
对使用自定义投影进行数据绑定的ASP.NET ListView进行排序_Listview_Sorting_Linq To Objects - Fatal编程技术网

对使用自定义投影进行数据绑定的ASP.NET ListView进行排序

对使用自定义投影进行数据绑定的ASP.NET ListView进行排序,listview,sorting,linq-to-objects,Listview,Sorting,Linq To Objects,下面是一个场景——我从查询表transactions和v_患者中的数据开始。我对一个列表(事务)执行该查询。我操纵(Concat(),GroupBy(),等等)该列表,直到到达最终投影,并将DataBind()映射到GroupMatchListView。我在标记中有一些链接按钮,它们具有Sort命令。现在我需要在对列表排序的GroupMatchListView\u Sorting事件处理程序中编写一些内容。我想我需要使用IComparer或GroupMatchListView.Items.Ord

下面是一个场景——我从查询表
transactions
v_患者
中的数据开始。我对一个
列表(事务)
执行该查询。我操纵(
Concat()
GroupBy()
,等等)该列表,直到到达最终投影,并将
DataBind()
映射到GroupMatchListView。我在标记中有一些链接按钮,它们具有
Sort
命令。现在我需要在对列表排序的
GroupMatchListView\u Sorting
事件处理程序中编写一些内容。我想我需要使用IComparer或
GroupMatchListView.Items.OrderBy
对列表视图进行排序,但我很困惑。我不想运行另一个SQL查询并重做列表操作。谢谢你的帮助

最后预测:

Dim goliath = From f In wholeResults _
                          Group f By f.v_patient Into Group _
                          Order By v_patient.last_name, v_patient.first_name, v_patient.date_of_birth _
                          Select New With {.PatientID = v_patient.patient_id, .LastName = v_patient.last_name, .FirstName = v_patient.first_name, _
                             .DOB = v_patient.date_of_birth, .Ins = v_patient.insname, .MatchCount = Group.Count(), .Matches = Group}

            GroupMatchListView.DataSource = goliath
            GroupMatchListView.DataBind()
我有一些带有排序命令的按钮:

<th id="Th4" runat="server" style="width: 100px" width="100px">
                                        <asp:LinkButton ID="SortLastNameButton" runat="server" CommandName="Sort" CommandArgument="LastName" ForeColor="White">Last Name</asp:LinkButton>
                                    </th>
                                    <th id="Th5" runat="server" style="width: 100px" width="100px">
                                        <asp:LinkButton ID="SortFirstNameButton" runat="server" CommandName="Sort" CommandArgument="FirstName" ForeColor="White">First Name</asp:LinkButton>
                                    </th>
                                    <th id="Th6" runat="server" style="width: 85px" align="center" width="85px">
                                        <asp:LinkButton ID="SortDOBButton" runat="server" CommandName="Sort" CommandArgument="DOB" ForeColor="White" ToolTip="Date of Birth">DOB</asp:LinkButton>
                                    </th>
                                    <th id="Th7" runat="server" style="width: 185px" width="185px">
                                        <asp:LinkButton ID="SortInsuranceButton" runat="server" CommandName="Sort" CommandArgument="Ins" ForeColor="White">Insurance</asp:LinkButton>
                                    </th>
再次感谢

编辑:我想我走对了。我采用GroupMatchListView.Items,使用OrderBy,并将结果分配给一个列表(ListViewDatItems)。然后清除GroupMatchListView.Items,并将这些项从ListViewDataItems列表中添加回来。然而,当我回到页面时,一切都没有改变。如果只使用DataBind(),则会得到一个空的ListView。如果将ListViewDataItems列表指定为数据源,然后指定为DataBind,则会出现错误。有人知道我该怎么结束吗

以下是我得出的结论:

    Protected Sub GroupMatchListView_Sorting(sender As Object, e As System.Web.UI.WebControls.ListViewSortEventArgs) Handles GroupMatchListView.Sorting

            Dim caesar As List(Of ListViewDataItem) = (GroupMatchListView.Items.OrderBy(Function(i) CType(i.FindControl("FirstName"), Label).Text)).ToList()

            GroupMatchListView.Items.Clear()

            For Each i As ListViewDataItem In caesar
                GroupMatchListView.Items.Add(i)
            Next

            ''I don't know what comes next.  The following does not work:
            ''GroupMatchListView.DataSource = GroupMatchListView.Items
            ''GroupMatchListView.DataBind()
        End Sub

嗯,我不能用GroupMatchesListView.Items来完成。相反,我将先前查询的结果放入会话变量中。然后,当需要排序时,我将其取出,排序,并将其发送回会话

代码如下:

  • 首先,我把它投入到会话中

        goliath = (From f In wholeResults _
                      Group f By f.v_patient Into Group _
                      Order By v_patient.last_name, v_patient.first_name, v_patient.date_of_birth _
                      Select New With {.PatientID = v_patient.patient_id, .LastName = v_patient.last_name, .FirstName = v_patient.first_name, _
                         .DOB = v_patient.date_of_birth, .Ins = v_patient.insname, .MatchCount = Group.Count(), .Matches = Group}).ToList()
    
    Session("ListOfMatches") = goliath
    GroupMatchListView.DataSource = goliath
    
  • 然后我施展了我的魔法

    受保护的子组MatchListView_排序(发件人作为对象,e作为System.Web.UI.WebControl.ListViewSortEventArgs)处理组MatchListView.Sorting

            Dim interimMatches As IEnumerable(Of Object) = CType(Session("ListOfMatches"), IEnumerable(Of Object))
    
            Select Case e.SortExpression
                Case "LastName"
                    If CurrentSortExpression = "LastName" Then
                        interimMatches = interimMatches.Reverse()
                    Else
                        interimMatches = interimMatches.OrderBy(Function(i) CType(i.LastName, String)).ToList()
                        CurrentSortExpression = "LastName"
                    End If
                Case "FirstName"
                    If CurrentSortExpression = "FirstName" Then
                        interimMatches = interimMatches.Reverse()
                    Else
                        interimMatches = interimMatches.OrderBy(Function(i) CType(i.FirstName, String)).ToList()
                        CurrentSortExpression = "FirstName"
                    End If
                Case "DOB"
                    If CurrentSortExpression = "DOB" Then
                        interimMatches = interimMatches.Reverse()
                    Else
                        interimMatches = interimMatches.OrderBy(Function(i) CType(i.DOB, Date)).ToList()
                        CurrentSortExpression = "DOB"
                    End If
                Case "Ins"
                    If CurrentSortExpression = "Ins" Then
                        interimMatches = interimMatches.Reverse()
                    Else
                        interimMatches = interimMatches.OrderBy(Function(i) CType(i.Ins, String)).ToList()
                        CurrentSortExpression = "Ins"
                    End If
            End Select
            Session("ListOfMatches") = interimMatches
            ViewState("CurrentSortExpression") = CurrentSortExpression
            GroupMatchListView.DataSource = interimMatches
            GroupMatchListView.DataBind()
    
        End Sub
    
希望这对别人有帮助

        Dim interimMatches As IEnumerable(Of Object) = CType(Session("ListOfMatches"), IEnumerable(Of Object))

        Select Case e.SortExpression
            Case "LastName"
                If CurrentSortExpression = "LastName" Then
                    interimMatches = interimMatches.Reverse()
                Else
                    interimMatches = interimMatches.OrderBy(Function(i) CType(i.LastName, String)).ToList()
                    CurrentSortExpression = "LastName"
                End If
            Case "FirstName"
                If CurrentSortExpression = "FirstName" Then
                    interimMatches = interimMatches.Reverse()
                Else
                    interimMatches = interimMatches.OrderBy(Function(i) CType(i.FirstName, String)).ToList()
                    CurrentSortExpression = "FirstName"
                End If
            Case "DOB"
                If CurrentSortExpression = "DOB" Then
                    interimMatches = interimMatches.Reverse()
                Else
                    interimMatches = interimMatches.OrderBy(Function(i) CType(i.DOB, Date)).ToList()
                    CurrentSortExpression = "DOB"
                End If
            Case "Ins"
                If CurrentSortExpression = "Ins" Then
                    interimMatches = interimMatches.Reverse()
                Else
                    interimMatches = interimMatches.OrderBy(Function(i) CType(i.Ins, String)).ToList()
                    CurrentSortExpression = "Ins"
                End If
        End Select
        Session("ListOfMatches") = interimMatches
        ViewState("CurrentSortExpression") = CurrentSortExpression
        GroupMatchListView.DataSource = interimMatches
        GroupMatchListView.DataBind()

    End Sub