vb.net 在通用列表中搜索对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/200151/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Search for Object in Generic List
提问by Saif Khan
Is it possible to search for an object by one of its properties in a Generic List?
是否可以通过通用列表中的一个属性来搜索对象?
Public Class Customer
Private _id As Integer
Private _name As String
Public Property ID() As Integer
Get
Return _id
End Get
Set
_id = value
End Set
End Property
Public Property Name() As String
Get
Return _name
End Get
Set
_name = value
End Set
End Property
Public Sub New(id As Integer, name As String)
_id = id
_name = name
End Sub
End Class
Then loading and searching
然后加载和搜索
Dim list as new list(Of Customer)
list.Add(New Customer(1,"A")
list.Add(New Customer(2,"B")
How can I return customer object with id =1? Does this have to do with the "Predicate" in Generics?
如何返回 id =1 的客户对象?这是否与泛型中的“谓词”有关?
Note: I am doing this in VB.NET.
注意:我在 VB.NET 中这样做。
回答by Jon Skeet
Yes, this has everything to do with predicates :)
是的,这与谓词有关:)
You want the Find(Of T)method. You need to pass in a predicate (which is a type of delegate in this case). How you construct that delegate depends on which version of VB you're using. If you're using VB9, you could use a lambda expression. (If you're using VB9 you might want to use LINQ instead of Find(Of T) in the first place, mind you.) The lambda expression form would be something like:
您需要Find(Of T)方法。您需要传入一个谓词(在这种情况下是一种委托)。您如何构造该委托取决于您使用的 VB 版本。如果您使用的是 VB9,则可以使用 lambda 表达式。(请注意,如果您使用的是 VB9,您可能希望首先使用 LINQ 而不是 Find(Of T)。)lambda 表达式形式将类似于:
list.Find(function(c) c.ID = 1)
I'm not sure if VB8 supports anonymous methods in the same way that C# 2 does though. If you need to call this from VB8, I'll see what I can come up with. (I'm more of a C# person really :)
我不确定 VB8 是否像 C# 2 那样支持匿名方法。如果你需要从 VB8 调用它,我会看看我能想出什么。(我更像是一个 C# 人 :)
回答by Ozgur Ozcitak
Generally you need to use predicates:
通常你需要使用谓词:
list.Add(New Customer(1, "A"))
list.Add(New Customer(2, "B"))
Private Function HasID1(ByVal c As Customer) As Boolean
Return (c.ID = 1)
End Function
Dim customerWithID1 As Customer = list.Find(AddressOf HasID1)
Or with inline methods:
或者使用内联方法:
Dim customerWithID1 As Customer = list.Find(Function(p) p.ID = 1)
回答by chrissie1
You could also overload the equals method and then do a contains. like this
您还可以重载 equals 方法,然后执行包含。像这样
Dim list as new list(Of Customer)
list.Add(New Customer(1,"A")
list.Add(New Customer(2,"B")
list.contains(new customer(1,"A"))
the equals method then would look like this
然后equals方法看起来像这样
public overrides function Equals(Obj as object) as Boolean
return Me.Id.Equals(ctype(Obj,Customer).Id
end Function
Not tested but it should be close enough.
未测试,但应该足够接近。
回答by Aleris
If you are using .NET 3.5 this can be done with LINQ to Objects:
如果您使用的是 .NET 3.5,这可以通过LINQ to Objects 完成:
How to: Query an ArrayList with LINQ
If not, in .NET 2.0 you can use the Find
method of the list.
如果没有,在 .NET 2.0 中你可以使用Find
list的方法。
The idea is that you will need to provide an method that return true if a property of your object satisfies a certain condition.
这个想法是,如果您的对象的属性满足特定条件,您将需要提供一个返回 true 的方法。