C# – Comparing GUID to string in LINQ to Entites throws error

c++linq

EDIT, this is not a duplicate. The suggested SO links want me to call ToString() but I am running a .COUNT() and trying to do a greater than comparison so calling ToString() is not a correct answer.

I am trying to fill a variable using the IF shortcut but when I run it I get the error

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

The code I am running is

IsAdmin = (db.yaf_prov_RoleMembership
             .Where(rm => rm.UserID == UserKey
                          && rm.RoleID == Guid.Parse("adsfasd654asdf816asdf"))
             .Count() > 0) ? true : false;

If I take away the Guid.Parse and do a straight up comparison then I get the error

Operator == cannot be applied to operands of type System.Guid and String

What needs to be done in order to compare a GUID to a string in a LINQ query?

Best Solution

Don't know why Dave deleted his answer - you should just move string parsing out of store expression. Otherwise Entity Framework tries to convert Guid.Parse method call to SQL, and fails to do that:

var adminRoleID = Guid.Parse("adsfasd654asdf816asdf");

Also use Queryable.Any to simplify your query:

IsAdmin = db.yaf_prov_RoleMembership
            .Any(rm => rm.UserID == UserKey && rm.RoleID == adminRoleID);