I have a List myList of MyObjects. Is it possible to check if myList contains a particular myObject based on a property of myObject in VB.NET? In C#, you'd something similar to this right:
myList.Exists(myObject => myObject.property1 == 3)
I'm sure you could use myList.Exists in VB.NET too, just with its lambda expression syntax.
However, the more general way is to use the Any LINQ operator, with the overload that takes a predicate. For example:
myList.Any(Function(myObject) myObject.property1 = 3)
Personally I prefer to use LINQ operators unless the more specific List<T> method provides a significant advantage for some reason.
EDIT:
If you need to access the object afterwards, just use:
Dim foo = myList.FirstOrDefault(Function(myObject) myObject.property1 = 3)
If (foo Is Not Nothing) Then
...
End If
It's roughly the same, except VB.NET has a different syntax for lambda expressions:
myList.Exists(Function(myObject) myObject.property1 = 3)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With