I'm attempting to follow this series of articles. I'm between parts 2 and 3 but am having some issues.
I'm writing the code in VB.Net which has thrown a couple of quirks.
Specifically, when visiting the expression tree, string comparisons aren't working as expected.
This method in the QueryProvider (Part 2)
Protected Overrides Function VisitMethodCall(m As MethodCallExpression) As Expression
If m.Method.DeclaringType = GetType(Queryable) AndAlso m.Method.Name = "Where" Then
sb.Append("SELECT * FROM (")
Me.Visit(m.Arguments(0))
sb.Append(") AS T WHERE ")
Dim lambda As LambdaExpression = DirectCast(StripQuotes(m.Arguments(1)), LambdaExpression)
Me.Visit(lambda.Body)
Return m
End If
Throw New NotSupportedException(String.Format("The method '{0}' is not supported", m.Method.Name))
End Function
Is throwing a NotImplementedException for String comparisons
m.Method.DeclaringType
is of type Microsoft.VisualBasic.CompilerServices.Operators
and m.Method.Name
is CompareString
. It looks like the string equality is handled slightly differently by VB and isn't being picked up in the correct way.
I'm using a Query.Where(function(x) x.Content_Type <> "")
to test.
Specifically, if I debug the calls to VisitBinary(b As BinaryExpression)
(Also Part 2), b
is {(CompareString(x.Content_Type, "", False) != 0)}
This then attempts to visit b.Left
(CompareString(x.Content_Type, "", False)
) which is where we fall through the hole in VisitMethodCall
.
If I just expand the If in VisitMethodCall to be
If (
m.Method.DeclaringType = GetType(Queryable) AndAlso
m.Method.Name = "Where"
) Or (
m.Method.DeclaringType = GetType(Microsoft.VisualBasic.CompilerServices.Operators) AndAlso
m.Method.Name = "CompareString") Then
It throws an InvalidCastException on the trying to convert the StripQuotes(m.Arguments(1))
to LambdaExpression
(says it's a ConstantExpression
)
What do I need to do to handle string comparisons correctly from VB?