elahehab
elahehab

Reputation: 335

Guid CompareTo doesn't translate in EF Core 7

I used the following EF Core Linq query with EF 6 where PK is a Guid and it worked:

var partialSCs = repository.MyClass
                           .Where(x => x.PK.CompareTo(pk) > 0))
                           .OrderBy(x => x.PK)
                           .Take(10);

EF Linq query was successfully translated to a SQL query.

Then we changed to EF Core 7, and now I get this error:

: 'The LINQ expression 'DbSet() .Where(s.PK.CompareTo((object)__pk_2) > 0)' could not be translated. Additional information: Translation of method 'System.Guid.CompareTo' failed. If this method can be mapped to your custom function, see https://go.microsoft.com/fwlink/?linkid=2132413 for more information. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to 'AsEnumerable', 'AsAsyncEnumerable', 'ToList', or 'ToListAsync'. See https://go.microsoft.com/fwlink/?linkid=2101038 for more information.'

I already read the following articles:

How to compare two GUIDs in Linq to Entities

Entity Framework Core: Guid Greater Than for Paging

What they say is that CompareTo must work. But it doesn't in my case.

Upvotes: 4

Views: 414

Answers (1)

elahehab
elahehab

Reputation: 335

I found the problem! Actually EF can translate CompareTo for 2 Guid objects. My problem was that one of the Guids was nullable it was like Guid.CompareTo(Guid?) So it couldn't translate it.

It can be seen in the error: DbSet() .Where(s.PK.CompareTo((object)__pk_2) > 0) that it is trying to cast __pk_2 to object.

Upvotes: 1

Related Questions