[CSHARP-4415] DateTime.Year throws an exception for IQueryable<DateTime> Created: 16/Nov/22  Updated: 28/Oct/23  Resolved: 17/Nov/22

Status: Closed
Project: C# Driver
Component/s: LINQ3
Affects Version/s: 2.18.0
Fix Version/s: 2.19.0

Type: Bug Priority: Unknown
Reporter: Alistair Steele Assignee: Robert Stam
Resolution: Fixed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Attachments: PNG File image-2022-11-16-11-18-05-393.png    
Issue Links:
Backports
backports CSHARP-4743 Add support for DateTime.Date and Dat... Closed

 Description   

Summary

Attempting to access a member of a DateTime type (Year, Month, Day, etc) fails with exception: 

System.NotSupportedException: 'Serializer for System.DateTime must implement IBsonDocumentSerializer to be used with LINQ.'

thrown from MemberExpressionToAggregationTranslator.Translate.

I stepped through and found that within MemberExpressionToAggregationExpressionTranslator.TryTranslateDateTimeProperty the 'container.Expression.Type' value is the type of '.Year', not the parent DateTime and as such fails the type check. The 'container.Expression.Expression.Type' is DateTime however so maybe it's just looking at the wrong expression.

This access worked in V2 so I assume DateTime component access is still intended.

How to Reproduce

With an IQueryable<DateTime> attempt to access '.Year'

Additional Background

I've included a screen cap of the locals within the TryTranslateDateTimeProperty __ 



 Comments   
Comment by Githook User [ 17/Nov/22 ]

Author:

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

Message: CSHARP-4415: DateTime.Year throws an exception for IQueryable<DateTime>.
Branch: master
https://github.com/mongodb/mongo-csharp-driver/commit/ec753b85d7e9281e92eb89176e4050050ecb94da

Comment by Robert Stam [ 16/Nov/22 ]

Thanks for reporting this and for the very helpful reproduction instructions.

A fix will be in the next release.

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