Skip to content

OrderBy with mapped EF entity property not working because of RestrictOrderByToPropertyOrField #928

Open

Description

@bzaugt

Using this with Entity Framework 8 (and sql server), OrderBy does not work when an entity property is a mapped column.

Example entity:

[Table("Invoices")]
public partial class Invoice
{
    public int Id { get; set; }

    [Column("DatePaid")] // property is mapped to the column "DatePaid" in the database
    public DateTime? PaymentDate { get; set; }
}

Then running OrderBy:

var invoices = context.Invoices;
var config = new ParsingConfig()
{
    // this is the default value, but setting it explicitly here for clarity
    RestrictOrderByToPropertyOrField = true
};
var orderedQuery = invoices.OrderBy(config, "x => x.PaymentDate == null ? 1 : 2");

Expected result: Using the default configuration, I would expect the property to be parsed and mapped correctly.

Actual result: This results in a ParseException with message: No property or field 'null' exists in type 'Invoice'

When setting RestrictOrderByToPropertyOrField to false, it works.

Also See #927

Activity

  1. menes97 commented on Nov 19, 2026

    @menes97
    Contributor

    Reproduced on master (0297001, v1.8.0, net10.0). It doesn't need EF or a [Column] mapping. A plain in-memory IQueryable<Invoice> fails the same way:

    q.OrderBy(new ParsingConfig(), "x => x.PaymentDate == null ? 1 : 2");
    // ParseException: No property or field 'null' exists in type 'Invoice'

    With RestrictOrderByToPropertyOrField = false it works. x.PaymentDate != null and iif(...) fail the same way under the default config, while Id == 1 ? 1 : 2 works.

    The cause is in ExpressionParser.ParseIdentifier (around line 1111). When parsing for OrderBy with the restriction on, any identifier found in KeywordsHelper is rejected unless it's it/root/parent. That table also holds the constants null, true and false (as well as iif, isnull, ...), so null is refused and reported as an unknown property.

    As an experiment I allowed keywords that map to a ConstantExpression in that check, a one-line change. With it, PaymentDate == null ? 1 : 2 and PaymentDate != null parse under the default config, while iif(...) and the other functions stay blocked. I haven't run the full test suite on it.

    Would you accept that (allow the constant keywords null/true/false in restricted OrderBy)? Or should the restriction stay as it is, with only a clearer error message? I can open a PR with tests either way.

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

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions