I would like the following function to return Nothing if the element with the specified key is not in the collection. Instead, it returns an error – something to the effect of “no element found”
Public Class MyCollection
Inherits System.Collections.ObjectModel.Collection(Of MyType)
Public Function FindByActivityKey(ByVal searchValue As Integer) As MyType
Return (From P In Me Order By P.ActivityPin.PrimaryKey = searchValue).First
End Function
End Class
Suggestions?
Replace
First()withFirstOrDefault()as inFirst()assumes there is at least one element and throws an exception if it can’t find any.FirstOrDefault()returnsNothingby default.