I have the following code to create a static list of elements and retrieve them by Id:
Private Shared Property SubStructTypes As IList(Of SubstructureType)
Shared Sub New()
SubStructTypes = New List(Of SubstructureType) From {
New SubstructureType With {.Id = "PURLIN", .Description = "Purlin"},
New SubstructureType With {.Id = "METALDECKING", .Description = "Metal Decking"},
New SubstructureType With {.Id = "WOODDECKING", .Description = "Wood Decking"}
}
End Sub
Public Shared Function GetById(ByVal myId As String) As SubstructureType
If String.IsNullOrWhiteSpace(myId) Then
Return Nothing
End If
Dim straightCompare = SubStructTypes.SingleOrDefault(Function(subStruct) subStruct.Id = myId)
Dim howIsThisFindingAnything = SubStructTypes.SingleOrDefault(Function(subStruct) subStruct.Id.ToUpper() = myId.ToLower())
Return SubStructTypes.SingleOrDefault(Function(subStruct) subStruct.Id.ToLower() = myId.ToLower())
End Function
There's nothing special about the class:
<Serializable>
Public Class SubstructureType
Public Property Id As String
Public Property Description As String
End Class
When passing in an Id, the SingleOrDefault method will find the value in the list regardless of the string casing. This is seen in the screenshot below:
Question:
Why is calling SingleOrDefault on my collection to filter off an Id value finding the element in the list, even though the casing is different (i.e. "Purlin" vs "PURLIN"). This is clearly seen in my howIsThisFindingAnything
variable where I explicitly change the casing.
Note:
- SingleOrDefault is using the standard .NET call
- Framework version: .NET 4