I am writing a LINQ Entity Framework query in which I want to parse a string in UNIQUEIDENTIFIER (aka GUID) as part of a sentence WHERE:
public IEnumerable<User> Find(Guid guid)
{
return dbContext
.Users
.Where(user => Guid.Parse(user.GuidText) == guid);
}
I know this is possible in SQL because I tested it:
SELECT *
FROM Users
WHERE CAST(GuidText AS UNIQUEIDENTIFIER) = @guid;
However, I did not find a way to generate the part CAST. I tried:
(Guid)user.GuidTextwhich generates a compiler error.Convert.ToGuid(user.GuidText)but this method does not exist.Guid.Parse(user.GuidText)but this causes the Entity Framework to generate an error when it translates a LINQ query.new Guid(user.GuidText)but this causes the Entity Framework to generate an error when it translates a LINQ query.(Guid)Convert.ChangeType(user.GuidText, typeof(Guid))but this causes the Entity Framework to generate an error when it translates a LINQ query.SqlGuid.Parse(user.GuidText)but this causes the Entity Framework to generate an error when it translates a LINQ query.
How can i achieve this? I am ready to insert SQL into the code as a last resort.
source
share