[CSHARP-4743] Add support for DateTime.Date and DateTime.TimeOfDay Created: 02/Aug/23  Updated: 24/Aug/23  Resolved: 09/Aug/23

Status: Closed
Project: C# Driver
Component/s: API
Affects Version/s: 2.20.0
Fix Version/s: 2.21.0

Type: Improvement Priority: Unknown
Reporter: Sasan Pasha Assignee: Robert Stam
Resolution: Done Votes: 0
Labels: Bug
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Backports
backported by CSHARP-4415 DateTime.Year throws an exception for... Closed
backports CSHARP-4772 Add support for List<T>.Exists and Ar... Closed
backports CSHARP-4773 Linq3Implementation: MongoDB.Driver.L... Closed
Documentation Changes: Not Needed
Documentation Changes Summary:

1. What would you like to communicate to the user about this feature?
2. Would you like the user to see examples of the syntax and/or executable code and its output?
3. Which versions of the driver/connector does this apply to?


 Description   

Summary

doing a linq query on datetime.year results in an exception :Serializer for System.DateTime must implement IBsonDocumentSerializer to be used with LINQ.

Please provide the version of the driver. If applicable, please provide the MongoDB server version and topology (standalone, replica set, or sharded cluster).

How to Reproduce

example:

                var interactions = await (await EquipmentInteractionsCollection.AsQueryable()
                    .Where(b => b.MemberId == memberId &&
                                (b != null && b.InteractionDate.HasValue && b.InteractionDate.Value.Date >= startDateTime.Date)).ToCursorAsync()).ToListAsync();

Additional Background

I think this issue was brought in story CSHARP-4415 and was addressed, but it seems it's not completely fixed



 Comments   
Comment by Githook User [ 09/Aug/23 ]

Author:

{'name': 'rstam', 'email': 'robert@robertstam.org', 'username': 'rstam'}

Message: CSHARP-4743: Add support for DateTime.Date and DateTime.TimeOfDay.
Branch: master
https://github.com/mongodb/mongo-csharp-driver/commit/5412f334218e77139a17462385d8151182b06fa6

Comment by Robert Stam [ 07/Aug/23 ]

When using `DateTime.Date` and `DateTime.TimeOfDay` keep in mind that `DateTime` values are stored in UTC on the server and that these operations will be done on UTC values.

Comment by Robert Stam [ 07/Aug/23 ]

While we support most DateTime properties, we missed adding support for `Date` and `TimeOfDay`.

I am adding support for those now.

Comment by Sasan Pasha [ 07/Aug/23 ]

Hi robert@mongodb.com . my apologies - you are correct, It's for the Date query.

Comment by Robert Stam [ 07/Aug/23 ]

Your sample code works for me when using `b.InteractionDate.Value.Year` but when using `b.InteractionDate.Value.Date` I get an exception.

I'm updating the summary to mention `DateTime.Date` instead of `DateTime.Year`.

Let me know if this is incorrect.

Comment by Robert Stam [ 07/Aug/23 ]

Note: the test for "b != null" is not necessary and can be removed.

Comment by Robert Stam [ 07/Aug/23 ]

Thank you for reporting this. I would like to investigate it. The summary says "DateTime.Year throws an exception" but your sample code does not use `Year` at all. Can you please clarify? Thanks.

Comment by PM Bot [ 02/Aug/23 ]

Hi sasan.pasha@gmail.com, thank you for reporting this issue! The team will look into it and get back to you soon.

Generated at Wed Feb 07 21:49:13 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.