You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
OrderBy with mapped EF entity property not working because of RestrictOrderByToPropertyOrField #928
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")]publicpartialclassInvoice{publicintId{get;set;}[Column("DatePaid")]// property is mapped to the column "DatePaid" in the databasepublicDateTime?PaymentDate{get;set;}}
Then running OrderBy:
varinvoices=context.Invoices;varconfig=newParsingConfig(){// this is the default value, but setting it explicitly here for clarityRestrictOrderByToPropertyOrField=true};varorderedQuery=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.
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(newParsingConfig(),"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.
Activity
menes97 commentedon Nov 19, 2026
Reproduced on master (0297001, v1.8.0, net10.0). It doesn't need EF or a
[Column]mapping. A plain in-memoryIQueryable<Invoice>fails the same way:With
RestrictOrderByToPropertyOrField = falseit works.x.PaymentDate != nullandiif(...)fail the same way under the default config, whileId == 1 ? 1 : 2works.The cause is in
ExpressionParser.ParseIdentifier(around line 1111). When parsing for OrderBy with the restriction on, any identifier found inKeywordsHelperis rejected unless it'sit/root/parent. That table also holds the constantsnull,trueandfalse(as well asiif,isnull, ...), sonullis refused and reported as an unknown property.As an experiment I allowed keywords that map to a
ConstantExpressionin that check, a one-line change. With it,PaymentDate == null ? 1 : 2andPaymentDate != nullparse under the default config, whileiif(...)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/falsein 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.