Skip to content

== null on a non-nullable collection property parses, then can fail in EF Core #211

Open

Description

@pdevito3

Summary

== null and != null on a collection property parse with no error. When the collection is a projection of a navigation, EF Core cannot translate the comparison, and the query throws when it runs. In an API, the client gets a 500 for a filter that QueryKit accepted.

Repro (QueryKit 1.16.2, Npgsql EF Core 10.0.0)

public class User { public int Id { get; set; } public List<UserPermission> UserPermissions { get; set; } = []; }
public class UserPermission { public int Id { get; set; } public int UserId { get; set; } public string Name { get; set; } = ""; }
public class UserDto { public int Id { get; set; } public List<string> Permissions { get; set; } = []; }

var dtos = db.Users.Select(u => new UserDto { Id = u.Id, Permissions = u.UserPermissions.Select(p => p.Name).ToList() });
dtos.ApplyQueryKitFilter("Permissions == null").ToQueryString();
Filter Result
Permissions == null InvalidOperationException: "The LINQ expression 'DbSet() .ToList() == null)' could not be translated."
Permissions != null The same
Permissions #== 0 SQL OK

Why not refuse it on every collection

== null on a collection is a supported behavior. FilterParser.cs:602-607 has its own branch for it. A collection that maps to a nullable column (a Postgres array, for example) translates == null, and the column can hold null. To refuse the comparison on every collection breaks those callers.

Proposal

Refuse == and != with null only when the collection property is not nullable. Read this with NullabilityInfoContext (.NET 6 and later):

  • List<string> Permissions (not nullable) refuses. It can never be null, so the comparison has no use. Throw a QueryKitParsingException, for example: "A collection property that is not nullable cannot compare with null. To find an empty collection, use #== 0."
  • List<string>? Tags (nullable) keeps the current behavior.
  • If the nullability is unknown (nullable reference types are off), keep the current behavior.

A downstream app now walks the parsed expression to find this comparison and returns a 400. Its operator sweep over each list finds the 500 when the check is off. The lists of that app use non-nullable collections, so this rule covers them.

Activity

  1. changed the title [-]== null on a projected collection property parses, then fails in EF Core[/-] [+]== null on a non-nullable collection property parses, then can fail in EF Core[/+] on Nov 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions