使用LINQ获取项目有时不起作用

时间:2016-10-18 21:33:14

标签: vb.net linq lambda predicate

我是VB的新手,请帮助我解释我的代码有什么问题,我不知道如何达到目标,即:
在ComboBox.selectedIndexChange方法中,我从TabPage获取带有LINQ的自定义Control(DrawTree),然后使用Predicate委托,我得到当前的CategorySet,因为它的条目需要使用DrawTree的条目重置。
从代码中提取:

Private Sub category_SelectedIndexChanged(sender As Object, e As EventArgs) Handles pool4Combo.SelectedIndexChanged, pool8Combo.SelectedIndexChanged, Pool16Combo.SelectedIndexChanged, Pool32Combo.SelectedIndexChanged
        Dim selectedCombo As ComboBox = sender
        If Not selectedCombo.SelectedIndex = -1 Then
            Dim selectedCategorySet As CategorySet = selectedCombo.SelectedItem
            Dim holder As TabPage = selectedCombo.Parent

            Dim drawTrees = From controls In holder.Controls.OfType(Of DrawTree)
                            Select controls
            Dim drawTree As DrawTree = CType(drawTrees(0), DrawTree)
            Dim pool As PoolSet = CType(context.Pools.Find(Function(n) (n.PoolName.Equals(holder.Name))), PoolSet)
            Dim cat As CategorySet = CType(pool.Categories.Find(Function(n) (n.CategoryName.Equals(selectedCategorySet.CategoryName))), CategorySet)
            cat.Entries = drawTree.getEntries()

            While holder.Controls.Count > 1
                holder.Controls.RemoveAt(holder.Controls.Count - 1)
            End While
            etc.
            etc.
            etc.
        End If
    End Sub

所以,问题是,当此代码运行时drawTree.getEntries()将为Nothing,但是,如果我注释掉poolcat声明,并且只打印{ {1}},它将包含我需要的条目 为什么这样以及如何解决? 非常感谢!

0 个答案:

没有答案