Linq. Why do I get this error?

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
 
J

Jon Skeet [C# MVP]

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
 
P

Pavel Minaev

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)."
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Linq extension method error 34
LINQ Question 1
Linq and JSon 5
Linq 1
Linq > Group 2
Linq. Please, need help. 1
Linq. Where 10
Linq. Take and OrderBy 7

Top