4

In the following code, I am trying to get a profile for the user that is currently logged in.

Dim userProfile = db1.UserProfiles.Where(Function(p) p.UserId = Membership.GetUser.ProviderUserKey).Single

The error is "LINQ to Entities does not recognize the method 'System.Object CompareObjectEqual(System.Object, System.Object, Boolean)' method, and this method cannot be translated into a store expression."

Does anyone know what's wrong with this, or is there a better way... And, how can I make this more secure; i.e. add a condition if no record is found?

I am using VB ASP.NET MVC 3.

Thank you.

Edit:

Here's my new code:

        Dim db1 As UserProfileDbContext = New UserProfileDbContext
        Dim user = Membership.GetUser()
        Dim key As Guid = user.ProviderUserKey
        Dim finalKey = key.ToString
        Dim userProfile = db1.UserProfiles.Where(Function(p) p.UserId = finalKey).Single
        Dim companyId = userProfile.CompanyId
2
  • What is the type of the UserId property?
    – nemesv
    Aug 1, 2012 at 19:53
  • UserId is a string from the database. Aug 1, 2012 at 19:56

1 Answer 1

7

L2E is trying to render your lamba expression (p) p.UserId = Membership.GetUser.ProviderUserKey in to an SQL Expression which it can use to hit the database.

However, Membership.GetUser() is a .NET method. L2E is complaining that it doesn't know how to render this method in to SQL syntax.

Try this instead:

Dim user = Membership.GetUser()
Dim userProfile = db1.UserProfiles.Where(Function(p) p.UserId = user.ProviderUserKey).Single

edit: MembershipUser.ProviderUserKey is a CLR Object. SQL can't compare two objects, so you'll need to strong type it before running the expression. For example, if your user key is a String:

Dim user = Membership.GetUser()
Dim key as String= user.ProviderUserKey
Dim userProfile = db1.UserProfiles.Where(Function(p) p.UserId = key).Single

This should work better because a simple equality expression = is understood by L2E and can be rendered in to an equivilent SQL expression, something like:

SELECT * FROM Profiles WHERE UserId = @Argument, where the @Argument is supplied by entity framework.

Also as an aside, L2E will group chained calls until the end, so an expression like:

db1.UserProfiles.Skip(10).Take(30).Where(Function(p) p.UserId = Membership.GetUser.ProviderUserKey)

... would still fail, because L2E will combine the Skip Take and Where components in to a single SQL expression. You can force L2E to hit the server by calling ToArray ToList or ToDictionary. That expression could be made valid by changing it to:

db1.UserProfiles.Skip(10).Take(30).ToArray().Where(Function(p) p.UserId = Membership.GetUser.ProviderUserKey)

The ToArray forces the execution of the SQL statement, giving you a .NET Array which does support complex lambas.

7
  • Thanks that took care of that error but I get a new one that says, "Object reference not set to an instance of an object." Any idea on that one or should I open a new question? Aug 1, 2012 at 19:52
  • Can you step through the debugger and see if Membership.GetUser is returning null? null.ProviderUserKey will surely cause a problem. Also, .Single() is programmed to throw an exception if the resultset is empty- in other words, if it fails to find a profile. Aug 1, 2012 at 19:59
  • Sorry to waste your time. I realized I got logged out which would make the current user null. However, when logging in and running the code, I still get this error, "LINQ to Entities does not recognize the method 'System.Object CompareObjectEqual(System.Object, System.Object, Boolean)' method, and this method cannot be translated into a store expression." Aug 1, 2012 at 20:03
  • That's because MembershipUser.ProviderUserKey is an object. I'll update my answer to reflect this. Aug 1, 2012 at 20:07
  • I tried the latest code but it didn't work, so I finally tried converting the guid into a string to be compared with the other string from the database, but it gives me an error "The 'IsCompanyOwner' property on 'UserProfile' could not be set to a 'String' value. You must set this property to a non-null value of type 'Boolean'." I updated my question with my current code. Aug 1, 2012 at 20:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.