Linq. Why do I get this error?

  • Thread starter Thread starter shapper
  • Start date Start date
S

shapper

Hello,

I am using the following Linq Query:

var filter = (from t in database.Tags
where t.Name.StartsWith(q,
StringComparison.CurrentCultureIgnoreCase)
orderby t.Name
select t);

But I am getting an error:

Server Error in '/' Application
The method 'StartsWith' has a translation to SQL, but the overload
'Boolean StartsWith (System.String, System.StringComparison)' does
not.

Description:

An unhandled exception occurred during the execution of the current
web request. Please review the stack trace for more information about
the error and where it originated in the code.

Exception Details:

System.NotSupportedException: The method 'StartsWith' has a
translation to SQL, but the overload 'Boolean
StartsWith(System.String, System.StringComparison)' does not.

Does anyone has any idea what am I doing wrong?

Thanks,
Miguel
 
Hello,

I am using the following Linq Query:

      var filter = (from t in database.Tags
                      where t.Name.StartsWith(q,
StringComparison.CurrentCultureIgnoreCase)
                      orderby t.Name
                      select t);

But I am getting an error:

System.NotSupportedException: The method 'StartsWith' has a
translation to SQL, but the overload 'Boolean
StartsWith(System.String, System.StringComparison)' does not.

Does anyone has any idea what am I doing wrong?

Yes - you're using a method call which doesn't have a direct
translation into SQL. In other words, LINQ to SQL is happy to
translate some overloads of StartsWith, but not the one you're using.

It's possible that in *this particular case* using ToLower and
StartsWith separately is the right way to go - but look at the
generated SQL and see whether it's what you think it should be, taking
culture into account.

Jon
 
Yes - you're using a method call which doesn't have a direct
translation into SQL. In other words, LINQ to SQL is happy to
translate some overloads of StartsWith, but not the one you're using.

For completeness, here's the list of unsupported methods:

http://msdn.microsoft.com/en-us/library/bb386970.aspx

In particular:

"Unsupported String methods in general:
Culture-aware overloads (methods that take a CultureInfo /
StringComparison / IFormatProvider)."
 
Back
Top