Skip to content

Support for System.DateOnly and Nullable in Expression Building #137

@takato1314

Description

@takato1314

Hi 👋,

I'm encountering an issue when using Castle.DynamicLinqQueryBuilder with properties of type System.DateOnly or System.DateOnly?. The following exception is thrown during expression generation:

Property 'System.DateTime Date' is not defined for type 'System.DateOnly' (Parameter 'property')

This seems to occur because these lines always assumes the property is of type DateTime? and attempts to access the .Date property, which does not exist on DateOnly.

🔍 Codebase:

if (Nullable.GetUnderlyingType(propertyExp.Type) != null)
{
    exOut = Expression.Property(propertyExp, typeof(DateTime?).GetProperty("Value"));
    exOut = Expression.Equal(
        Expression.Property(exOut, typeof(DateTime).GetProperty("Date")),
        Expression.Convert(someValue, typeof(DateTime)));

    exOut = Expression.AndAlso(GetNullCheckExpression(propertyExp), exOut);
}
else
{
    exOut = Expression.Equal(
        Expression.Property(propertyExp, typeof(DateTime).GetProperty("Date")),
        Expression.Convert(someValue, propertyExp.Type));
}

When propertyExp.Type is of DateOnly type, this throws an exception because DateOnly does not have a .Date property.

✅ Suggested Fix:

Add type checking for DateOnly and DateOnly? and handle them separately without trying to access .Date. A direct comparison should suffice.

💡 Feature Request:

Please consider adding native support for System.DateOnly and System.DateOnly? in the expression builder logic, similar to how DateTime and DateTime? are handled.

📌 Why this matters:

As of EF Core 8, DateOnly and TimeOnly are officially supported types for entity properties. This makes it a valid and increasingly common use case for dynamic LINQ expression builders to support these types.

Thanks for the great library!

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