String Comparisons using LINQ Expressions and String.Format

I'm using LINQ expressions to dynamically search for values in a collection and I've come across a strange issue that seems to be caused by searching for a string that is the result of a String.Format() operation.

Here is a simplified version of what I am trying to do, since in practice I do not actually know what type of value I am searching for, I have to treat everything as an Object.

Dim stringToFind As String = "Some Special Value"

' Create a collection of 10 strings, one of which being the string to find '
Dim myStrings As New List(Of Object)
For i As Integer = 0 To 9
    If (i = 3) Then
        myStrings.Add("Boring Plain Old Value")
    End If

' Create the predicate for <Function(x) x = stringToFind> '
Dim left = Expression.Parameter(GetType(Object), "x")
Dim right = Expression.Constant(stringToFind)
Dim eq = Expression.Equal(left, right)
Dim predicate = Expression.Lambda(Of Func(Of Object, Boolean))(eq, left).Compile()

' Compare the results '
Dim standardResults = myStrings.Where(Function(x) x = stringToFind)
Dim expressionResults = myStrings.Where(predicate)

' Expected Output:'
' Standard Method: 1 '
' LINQ Expressions: 1 '
Console.WriteLine(String.Format("Standard Method: {0}", standardResults.Count()))
Console.WriteLine(String.Format("LINQ Expressions: {0}", expressionResults.Count()))

What happens here is that the standard method correctly returns 1 result, while the LINQ Expressions method returns 0. However, if the special value is added to the collection without using String.Format() (myStrings.Add(stringToFind)) then the expression returns the correct result as well.

Is there something that String.Format() does to change the encoding of the string, or anything that would effect how a constant expression is created for the string value? Is there another way I could compare two values using LINQ expressions that would get me around this limitation?


The VB.NET equality operator does not do a reference comparison. Roughly speaking, it uses the object.Equals method (in contrast to object.ReferenceEquals). Expression trees use object reference equality.

Using string.Format does not do anything special. (There are no "special" strings).

Need Your Help

How to recreate the table by key?

r data.table

I thought it could be a very easy question, but I am really a new beginner for R.

Centered CSS Menu (I've reviewed many answers but can't figure this one out)

html css css3 menu navigation

I am sorry in advance, I know multiple postings on this topic is a drag.. But I have tried at least 10 different solutions and cannot figure out why this menu will not center.