VB.NET LINQ query without string code

I have the following code to create a static list of elements and get 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 is nothing special in the class:

 <Serializable> Public Class SubstructureType Public Property Id As String Public Property Description As String End Class 

When passed to Id, the SingleOrDefault method will find the value in the list regardless of the shell of the string. This can be seen in the screenshot below: enter image description here

Question : Why does I call SingleOrDefault in my collection to filter out the Id value that finds the item in the list, even if the case is different (ie, "Purlin" and "PURLIN"). This is clearly visible in my howIsThisFindingAnything variable, where I explicitly change the case.

Note:

  • SingleOrDefault uses a standard .NET call
  • Platform Version: .NET 4
+7
source share
2 answers

VB.Net has a compilation option with an instruction / project: Option Compare .

There are two options for this: Text and Binary .

Most likely you configured Text somewhere.

Depending on the settings of your environment, it will be determined how this is set by default. Checking my settings, I usually set it to default in binary format in project settings.

The difference is (from MSDN here ):

Binary:

Results in string comparisons based on sort order derived from internal binary representations of characters. This type of comparison is useful, especially if strings can contain characters that should not be interpreted as text. In this case, you do not want to bias comparisons with alphabetical equivalents, such as case insensitivity.

Text

String matching results based on case insensitive text sorting defined by your language system. This type of comparison is useful if your strings contain all text characters, and you want to compare them based on letter equivalents, such as case insensitivity and closely related letters. For example, you can consider A and a equal, and Γ„ and Γ€ before B and b.

These options affect not only the obvious equality scenario, but also areas such as sorting sorting.

This can be set / default in the following places:

  • The topmost part of the code file
  • The project is set by default in the project properties β†’ Compilation page
  • By default, for new projects specified in the section β€œSettings β†’ Projects and Solutions β†’ VB Settings Page.

Personally, I would like to use Binary , as it behaves most similarly to other languages ​​such as C # / Javascript, etc., and then explicitly encodes where I need case insensitive equality.

+7
source share

According to the comment, the answer to this question was to use String.Compare instead of =

See this topic for more string comparisons in vb

0
source share

All Articles