根据所选值从列表中检索项目

时间:2015-02-14 16:20:39

标签: vb.net linq list foreach

函数retrieveFixtureReport()返回一个Report列表,每个列表都包含一个PlayerID。我正在尝试设置标签以反映从cmbSelectedPlayer中选择的所选报告。列表中的每个报告都包含唯一的PlayerID。我已经尝试了各种不同的方法来访问所选报告的属性,包括LINQ,但到目前为止都没有成功。 For Each循环似乎不是仅选择一个Report的正确选项,它也阻止我从cmbSelectedPlayer中选择其他PlayerID(仅显示列表中的最后一个Report)。代码如下所示:

Public Sub setFixtureReport()

            'If UC_Menu_Scout1.cmbSelectedPlayer.SelectedItem IsNot Nothing Then

            If UC_Menu_Scout1.cmbSelectedPlayer.Items.Count > 0 Then

                Dim getPlayerReport = _
                (From rpt As Report In retrieveFixtureReport() _
                Where rpt.PlayerID = UC_Menu_Scout1.cmbSelectedPlayer.SelectedItem.PlayerID) '.AsEnumerable

                'For Each rpt As Report In getPlayerReport()

                'For Each rpt As Report In retrieveFixtureReport.Where(Function(x) x.PlayerID = UC_Menu_Scout1.cmbSelectedPlayer.SelectedItem.PlayerID)

                'Dim rpt As Report = getPlayerReport 'retrieveFixtureReport(0)

                '*****General Information
                UC_Menu_Scout1.lblRptPosition.Text = rpt.PositionPlayed
                UC_Menu_Scout1.lblFoot.Text = rpt.PreferredFoot
                UC_Menu_Scout1.txtComments.Text = rpt.Comments
                UC_Menu_Scout1.lblStatus.Text = rpt.MonitorStatus

                'Next

                setColours()
                'End If
            End If
        End Sub

组合框cmbSelectedPlayer从playerList填充(这也被添加到retrieveFixxtureReport中):

UC_Menu_Scout1.cmbSelectedPlayer.DataSource = playerList
        UC_Menu_Scout1.cmbSelectedPlayer.DisplayMember = "PlayerFullName"
        UC_Menu_Scout1.cmbSelectedPlayer.ValueMember = "PlayerID"

任何建议都将不胜感激。

1 个答案:

答案 0 :(得分:0)

如果您的组合框绑定到List(Of Player),那么您需要做的就是将SelectedItem强制转换为它的类型:

Dim player = TryCast(UC_Menu_Scout1.cmbSelectedPlayer.SelectedItem, Player)
If Not player Is Nothing Then
  Dim getPlayerReport = (From rpt As Report In retrieveFixtureReport()
                         Where rpt.PlayerID = player.PlayerID).FirstOrDefault()
  If Not getPlayerReport Is Nothing Then
    UC_Menu_Scout1.lblRptPosition.Text = getPlayerReport.PositionPlayed
    UC_Menu_Scout1.lblFoot.Text = getPlayerReport.PreferredFoot
    UC_Menu_Scout1.txtComments.Text = getPlayerReport.Comments
    UC_Menu_Scout1.lblStatus.Text = getPlayerReport.MonitorStatus
  End If
End If