Skip to content

A range set column projected as IReadOnlyList<TRange> or IEnumerable<TRange> fails on the server with 42846 #585

Done

Description

@CaffeinatedCoder

Found by a pre-release sweep of main @ 577b85e (lens: framework-heuristics, from a lead); run against PostgreSQL 16. Probably not new in 11.0.0: the 10.x RangeSet<TRange, T> is an IReadOnlyList<TRange> too (not run there).

Problem

A final Select that hands a range set column to something typed as a collection of its ranges fails on the server:

db.Reservations.Select(r => new { Blocked = (IReadOnlyList<DateRange>)r.BlockedDays })
// the same for a DTO member declared IReadOnlyList<DateRange> or IEnumerable<DateRange>,
// where the compiler writes the conversion
SELECT r."BlockedDays"::daterange[] AS "L" FROM "Reservations" AS r
PostgresException: 42846: cannot cast type datemultirange to daterange[]

EF Core translates the conversion to the interface as a cast to the array mapping Npgsql has for a list of ranges. PostgreSQL has no such cast.

Measured on 577b85e:

Final projection Result
(IReadOnlyList<DateRange>)r.BlockedDays 42846
(IEnumerable<DateRange>)r.BlockedDays 42846
((IReadOnlyList<DateRange>)r.BlockedDays)[0] 42846
r.BlockedDays[0] the first range
((IReadOnlyCollection<DateRange>)r.BlockedDays).Count 2
r.BlockedDays.ToList(), .ToArray() the ranges

It fails loudly, with a server error that does not say what to write instead. ToList() is the way round it.

Change

Keep the conversion out of the SQL: the column is read as the set, and the conversion to an interface the set implements runs in memory, as ToList() does. Where that happens (the interceptor before translation, or the type mapping source declining the array mapping for a set-typed operand) is to be decided against EF Core 11's own handling of a converted column in a projection. Related: #94 (enumeration of a multirange column) and #407.

Test

An integration test over every discovered range set type that projects the column as each BCL collection interface it implements, into an anonymous type and a DTO member, and compares with the loaded set. It fails with 42846 without the change.

Activity

  1. added
    bug
    area/range-set
    package/efcore
    severity/low
    on Nov 29, 2026
  2. added this to the 11.0.0 milestone on Nov 30, 2026
  3. added 2 commits that reference this issue on Nov 30, 2026
  4. added a commit that references this issue on Nov 30, 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

    area/range-setbugpackage/efcoreseverity/low

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions