gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 00:22:03 25 4
gpt4 key购买 nike

有没有一种简单的方法可以使用指定的值从列表中删除对象?我将 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com