检查列表框中是否已存在文本框值

时间:2020-12-27 04:28:32

I have a ListBox bound to a observableCollection of "widgets". Each widget in the collection only has a name field at the moment but that will change.

我有一个ListBox绑定到obidgetableCollection的“小部件”。集合中的每个窗口小部件目前只有一个名称字段,但会发生变化。

On the window I have a text box where a user can enter another name for addition to the observableCollection.

在窗口中,我有一个文本框,用户可以在其中输入另一个名称以添加到observableCollection。

Each entry has to be unique.

每个条目必须是唯一的。

At the moment I use this

目前我用这个

  Dim query As IEnumerable(Of clsWidget)
  query = WidgetSource.Where(Function(widget) widget.name = txtNewName.Text)

  If query.Count > 0 Then
    Debug.Print("Tried to add a widget that already in the collection")
  End If

Is there a more efficient way of doing this?

有没有更有效的方法呢?

1 个解决方案

#1


0  

It can be more efficiently coded by using the code below which only requires one line.

使用下面只需要一行的代码可以更有效地编码。

    If WidgetSource.Any(Function(widget) widget.name = txtNewName.Text) Then Debug.Print("Tried to add a widget that already in the collection")

If by efficient you mean it runs faster then I doubt there will be much difference between my code and yours.

如果有效率你的意思是它运行得更快,那么我怀疑我的代码和你的代码之间会有很大差异。

#1


0  

It can be more efficiently coded by using the code below which only requires one line.

使用下面只需要一行的代码可以更有效地编码。

    If WidgetSource.Any(Function(widget) widget.name = txtNewName.Text) Then Debug.Print("Tried to add a widget that already in the collection")

If by efficient you mean it runs faster then I doubt there will be much difference between my code and yours.

如果有效率你的意思是它运行得更快,那么我怀疑我的代码和你的代码之间会有很大差异。