使用LINQ查询使用来自访问数据库的数据填充组合框

时间:2015-04-28 23:55:34

标签: vb.net linq

在VB.Net中:

我正在尝试使用MS Access数据库中的数据填充表单上的组合框。特别;从数据库中取出所有姓氏,在输出列表中按升序对它们进行排序,我将其命名为玩家,然后将玩家中的每个项目添加到我的组合框(private void cmpClose_MouseUp(object sender, MouseEventArgs e) { OpenPDF currentOpenPDF; // iterate through all the tab pages for (int i = 0; i < tcDocuments.TabCount; i++) { // get their rectangle area and check if it contains the mouse cursor if (tcDocuments.GetTabRect(i).Contains(e.Location)) { // Do something to the tab } } } )。

cboPlayer

我知道这并不完全正确,但不能肯定要指向的方向。当我运行程序时,组合框中填充了 Public Sub GetPlayers() Dim PlayerLastName As New List(Of String) PlayerLastName.Add("Smith") PlayerLastName.Add("Hill") PlayerLastName.Add("Beyer") PlayerLastName.Add("Wilson") PlayerLastName.Add("Hudson") PlayerLastName.Add("van Zegeren") PlayerLastName.Add("Bibbs") PlayerLastName.Add("Muller") PlayerLastName.Add("Pierce") PlayerLastName.Add("Henry") PlayerLastName.Add("Johnston") Dim Players = From Last In PlayerLastName Order By Last Ascending Select Last cboPlayer.Items.Add(Players.ToString) cboPlayer.SelectedItem = 0

任何想法可能意味着什么或我做错了什么?

1 个答案:

答案 0 :(得分:1)

Maybe over thinking it just a little? Why not try without the linq, List has a sort function. Also just bind PlayerLastName to the combobox.

    Public Sub GetPlayers()

        Dim PlayerLastName As New List(Of String)
        PlayerLastName.Add("Smith")
        PlayerLastName.Add("Hill")
        PlayerLastName.Add("Beyer")
        PlayerLastName.Add("Wilson")
        PlayerLastName.Add("Hudson")
        PlayerLastName.Add("van Zegeren")
        PlayerLastName.Add("Bibbs")
        PlayerLastName.Add("Muller")
        PlayerLastName.Add("Pierce")
        PlayerLastName.Add("Henry")
        PlayerLastName.Add("Johnston")

        PlayerLastName.Sort()    

        cboPlayers.DataSource = PlayerLastName
        cboPlayers.SelectedIndex = -1

     End sub

Edit:

or if you still want to us linq then, you need to change the linq return of IEnumerable to a List...

Dim Players = From Last In PlayerLastName
                        Order By Last Ascending
                        Select Last

ComboBox1.DataSource = Players.ToList
ComboBox1.SelectedIndex = -1