Daniil_Kosta
Daniil_Kosta

Reputation: 51

LINQ to Entities does not recognize the method System.Guid

I have the following error :

LINQ to Entities does not recognize the method 'System.Guid GetIdAuthenticatedUser()' method, and this method cannot be translated into a store expression.

This happens when I run this code:

   ...
 if (_context.Voice.Any(u => u.VoiceUserId == _repository.GetIdAuthenticatedUser())) //error here
                    {
                        Response.Redirect("MainPage.aspx");
                    }
    ...

Method GetIdAuthenticatedUser() returns to id of the authenticated user:

 public Guid GetIdAuthenticatedUser()
        {
            MembershipUser user = Membership.GetUser();
            var a = user.ProviderUserKey;
            var userid = new Guid(a.ToString());
            return userid;
        }

u.VoiceUserId is of type Guid

What need to do to make it work?

Upvotes: 0

Views: 2397

Answers (1)

JoaoFSA
JoaoFSA

Reputation: 267

Just move the call to the function to outside the linq query

var id = _repository.GetIdAuthenticatedUser();
if (_context.Voice.Any(u => u.VoiceUserId == id))
{
    Response.Redirect("MainPage.aspx");
}

The problem is that, like the error says, the method GetIdAuthenticatedUser() isnt part of linq so when linq is trying to generate the query for the DB it doesnt know how to translate the method call to sql

Upvotes: 4

Related Questions