.net - 如何通过对象值从 List(Of myClass) 中删除对象?

标签 .net vb.net list

有没有一种简单的方法可以使用指定的值从列表中删除对象? 我将 2 个人添加到列表中,现在如何在不使用任何循环的情况下通过名字删除一个人? (如果可能的话)

Public Class Form1
    Public Persons As New List(Of Person)
    Private Sub Test()
        Persons.Add(New Person With {.Name = "Jamie", .Age = 99})
        Persons.Add(New Person With {.Name = "Adam", .Age = 40})

        'How to remove a person from the list having the name "Jamie" ?
        'Persons.Remove(Name = "Jamie")... ???
    End Sub
End Class

Public Class Person
    Public Name As String
    Public Age As Integer
End Class

最佳答案

Persons.RemoveAll(person => person.Name == "Jamie")

在 VB 中:

Persons.RemoveAll(Function(person) person.Name = "Jamie")

(感谢海因兹)

关于.net - 如何通过对象值从 List(Of myClass) 中删除对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7412398/

相关文章:

jquery - 如何将无序列表 move 到 JQuery 页面上的新部分?

python - 解释嵌套列表理解如何工作?

c# - c# 应用程序会在 linux 操作系统上运行吗

c# - .NET,弹跳城堡 : How to save public Key then import it from a File?

.net - 我想在 Linux mono 上运行基于 Vb.Net Hello World GUI 的应用程序

vb.net - 在 tabControl 中隐藏和显示 TabPages

python - 如果条件为真则合并子列表

.net - 安装 Microsoft.dotnet-httprepl 失败

.net - 在Visual Studio中更改目标框架

mysql - VB.NET 将 24 小时 mySQL TIME() 转换为 12 小时格式