[CSHARP-1869] Full featured LINQ support for TimeSpan Created: 18/Dec/16  Updated: 15/Nov/22

Status: Backlog
Project: C# Driver
Component/s: Linq
Affects Version/s: 2.3
Fix Version/s: None

Type: New Feature Priority: Major - P3
Reporter: Frédéric Barrière Assignee: Unassigned
Resolution: Unresolved Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

MongoDB.Driver 2.3.0
.net core (netstandard1.5)
Mongo 3.2
Visual Studio 2015



 Description   

I want to create an simple aggregation request using linq expression to extract groups and calculate some values.
It works fine except for property of type TimeSpan.

collection.Aggregate()
.Match(x => x.Date >= minimumDate)
.Group(
x => x.UserId,
group => new

{ id = group.Key, Count = group.Count(), Price = group.Sum(x => x.Price), // float : ok Duration = group.Sum(x => x.Duration) // TimeSpan : ko !! }

);

The Sum of Duration property for group does not work. There is no Sum extension method that accept a TimeSpan in System.Linq.Enumerable.
If I change to x => x.Duration.Ticks, the Driver throws an exception with message saying that it does not know translate Ticks.
I tried to create a Sum extension method accepting a TimeSpan. It compiles but yet fails at runtime because the driver does not recognize this method that is not part of the framework.

So how to do. I do not want to used aggregate API with BsonDocument. I want to use linq expression as it allow to abstract the data access layer and it is great for unit tests.



 Comments   
Comment by Robert Stam [ 15/Nov/22 ]

We should support all manner of TimeSpan operations.

Just a few examples:

new TimeSpan(ticks)
new TimeSpan(hours, minutes, seconds)
...
TimeSpan.FromTicks(ticks)
TimeSpan.FromSeconds(seconds)
...
timeSpan.Ticks
timeSpan.Seconds
timeSpan.TotalSeconds
...
timeSpan.Add(timeSpan)
timeSpan.Divide(divisor)
...

 

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