Method ‘System.Object CompareObjectEqual(System.Object, System.Object, Boolean)’ has no supported translation to SQL. Solution to error

I was running a basic LINQ 2 SQL statement:

From role In db.user_role _
Where role.USER_ID = Session("user_id") Select role

when I encountered following error message:

Method ‘System.Object CompareObjectEqual(System.Object, System.Object, Boolean)’ has no supported translation to SQL.

After a little research I found the solution.

Apparently parameters for LINQ 2 SQL statements have to match the type of property (db field) used in the query exactly, no implicit conversion/type inference is going on. In my case role.USER_ID is of a String type and Session variable is of an Object type. When I explicitly converted Session variable to string:

From role In db.user_role _
Where role.USER_ID = Session("user_id").ToString Select role

it worked. So to avoid this error make sure parameter for your query has the same type as property it is being compared to and use type conversion if needed.

3 replies on “Method ‘System.Object CompareObjectEqual(System.Object, System.Object, Boolean)’ has no supported translation to SQL. Solution to error”

  1. Great tip, thanks! I had the same error and you saved me much headscratching!

Leave a Reply

Your email address will not be published. Required fields are marked *