Skip to content

[Question] Does OData $orderby support working with Edm.Date or DateOnly fields? #1148

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
AndriiLesiuk opened this issue Dec 27, 2023 · 8 comments
Assignees
Labels
bug Something isn't working

Comments

@AndriiLesiuk
Copy link

Is it possible to make the OData $orderby function work with Edm.Date or DateOnly fields?
It currently only works with DateTime fields.
I didn't raise this question on "Discussions" because there it takes a long time to wait for an answer, for which I apologize.
I would be grateful for any example or advice.

@AndriiLesiuk AndriiLesiuk added the bug Something isn't working label Dec 27, 2023
@aboryczko
Copy link

I have an Edm.Date field in one of my models and $orderby works properly. The field is a DateTime field in .NET. When I was working on it a year ago there was no support for DateOnly in this library.

I would have answer it also in Discussions because I check both 😉

@AndriiLesiuk
Copy link
Author

I have an Edm.Date field in one of my models and $orderby works properly. The field is a DateTime field in .NET. When I was working on it a year ago there was no support for DateOnly in this library.

I would have answer it also in Discussions because I check both 😉

What EF provider and database did you use in your project that worked with Edm.Date?

@AndriiLesiuk
Copy link
Author

I use Postgres and the libraries for it:

@aboryczko
Copy link

EF Core 6 and Microsoft SQL Server 2019, but like I said the .NET object has a DateTime field (it didn't work with any other type)
public DateTime VerifiedOn { get; set; }
and in the model I have
entity.Property(x => x.VerifiedOn).AsDate();

@AndriiLesiuk
Copy link
Author

@aboryczko
Sorry to bother you again, but unfortunately my problem has not gone away.
It's not possible to use /odata/Entity?$orderby=date(where date is DateTime with AsDate())
I tried a few ways:

  1. Add ColumnAttribute:
    image

  2. Add AsDate in model builder:
    image

And I still getting this error:
image

I found this all happening in the LiteralFormatter class(Microsoft.OData.Core).
Here we have such section, which is ignored:
image

and then it just go to
image

Do you have any ideas?
Big thanks.

@AndriiLesiuk AndriiLesiuk reopened this Jan 4, 2024
@AndriiLesiuk
Copy link
Author

I tried almost everything on here but it didn't help.
link

@xuzhg
Copy link
Member

xuzhg commented Jan 4, 2024

I tried almost everything on here but it didn't help. link

Can you create a 'value' converter 'https://github.com/OData/odata.net/blob/master/src/Microsoft.OData.Core/ODataPayloadValueConverter.cs' and use it in your project?

@AndriiLesiuk
Copy link
Author

I tried almost everything on here but it didn't help. link

Can you create a 'value' converter 'https://github.com/OData/odata.net/blob/master/src/Microsoft.OData.Core/ODataPayloadValueConverter.cs' and use it in your project?

Converter works:
image

, but this will change all DateTime fields in my project.
Instead, I need to change only some of them that are of type DateTime.
image

When I try something like this:
image
image

, it still fails any of the type checks in the FormatRawLiteral(object value) function.

I expected the value to go into this section of code, but for some reason it doesn't work.
image

Also parameters that are passed to ConvertToPayloadValue(object value, IEdmTypeReference edmTypeReference) to write at least some primitive check in order to limit this functionality for certain fields.

Is it even possible to do anything else?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants