views:

317

answers:

3

Using VB.net, the following snippet gives the error below.

Dim _account = Account.Find(Function(x As Account) x.AccountName = txtFilterAccountName.Text)

or similarly if I do

.SingleOrDefault (Function(x As Account) x.AccountName = txtFilterAccountName.Text)

will both give the error "The method 'CompareString' is not supported". If I make the same call searching for an integer (ID field) it works fine.

.SingleOrDefault (Function(x As Account) x.Id = 12)

So interger matching is fine but strings don't work Is this a problem with the VB.net templates?

+1  A: 

No this is not a problem with Vb.Net templates.

The problem is that you are not using a normal LINQ provider. Based on your tag (subsonic) I'm guessing you're using a LINQ to SQL query.

The problem is that under the hood, this is trying to turn your code into an expression tree which is then translated into an SQL like query. Your project settings are turning your string comparison into a call in the VB runtime. Specifically, Microsoft.VisualBasic.CompilerServices.Operators.CompareString.

The LINQ2SQL generater in question or VB compiler (can't remember where this check is done off the top of my head) does not understand how to translate this to an equivalent bit of SQL. Hence it generates an error. You need to use a string comparison function which is supported by LINQ2SQL.

EDIT Update

It looks like the CompareString operator should be supported in the Linq2SQL case. Does subsonic have a different provider which does not support this translation?

JaredPar
Thanks for the insight. I'm not sure the details of the Link2SQL generator. I use SubSonic to get insulated from some of those details. But with the newly released SubSonic 3.0 it seems the VB guys really have to get in to the guts of it to make the changes necessary to get it working. This gives me something to research, so again...thanks.
John Granade
+1  A: 

The problem is with SubSonic3's SQL generator and the expression tree generated from VB.NET.

VB.NET generates a different expression tree as noted by JaredPar and SubSonic3 doesn't account for it - see Issue 66.

I have implemented the fix as described but it has yet to merge into the main branch of SubSonic3.

BlackMael
BlackMael, thanks for the comment and direction. Would you give a little more insight on your fix. The article shows the ConvertVBStringCompare helper function using BinaryExpression but for SubSonic Find or SingleorDefault those take Expression and I don't think it's possible to convert those back and forth. You mentioned on issue 66 that you have a fix. Could you merge that in to the VB.net template fork so we could get those while they decide how to merge it in to production? Thanks. John
John Granade
A: 

BlackMael's fix has been committed:

http://github.com/subsonic/SubSonic-3.0/commit/d25c8a730a9971656e6d3c3d17ce9ca393655f50

The fix solved my issue which was similar to John Granade's above.

Thanks to all involved.

joshblair