开发者

Lambda not working on array

I have the following array

 Dim items() = {
                              New CheckedListBoxItem("NYC", False),
                              New CheckedListBoxItem("CHI", False),
                              New CheckedListBoxItem("PHL", False),
                              New CheckedListBoxItem("SFO", False),

                }

I am trying to query against it like this

        Try
            Dim item As CheckedListBoxI开发者_如何学Pythontem = items.ToList().Where(Function(x) x.Value = "PHL")
            MsgBox(item.Value)
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

I am getting the error

Value cannot be null.

Parameter name: source

I also tried

 Dim item As CheckedListBoxItem = items.FirstOrDefault(Function(x) x.Value.ToString() = "PHL")

What am i missing. I just need to query against the list to get an item and change it's checkedstate from false to true.


You need to do:

item = items.ToList().Where(Function(x) x.Value = "PHL").First

MsgBox(item.Value)

First returns the First item from the collection or throws an exception if the collection is empty.

FirstOrDefault returns the First item from the collection or the default instance for the type if the collection is empty.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜