Skip to content

Nullable Type comparison HasValue with boolean needs parentheses #2090

@Isitar

Description

@Isitar

Hi

The Model looks something like this:

public class MyClass {
  public Guid Id {get;set;}
  public Guid? ParentId {get;set;}
}

then the query:

var valueFilterNegated = true;
... // some logic to set the valueFilterNegated

dbContext.MyClasses.Where(c =>valueFilter != c.ParentId.HasValue).CountAsync(cancellationToken);

the SQL Query produced is:

SELECT COUNT(*)::INT
FROM "MyClasses" AS s
WHERE ($1 <> s."ParentId" IS NOT NULL)

with $1 being either True or False, depending on the valueFilterNegated.

The Problem is, that the <> operator has a stronger binding than the IS NOT NULL and therefore it tries to compare boolean <> guid which throws: operator does not exist: boolean <> uuid at character

The solution is quite simple, put parantheses:

SELECT COUNT(*)::INT
FROM "MyClasses" AS s
WHERE ($1 <> (s."ParentId" IS NOT NULL))

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions