Skip to content

Commit ab53920

Browse files
authored
CSHARP-2108: Subtracting one date from another in LINQ. (#837)
1 parent ea67297 commit ab53920

File tree

2 files changed

+96
-1
lines changed

2 files changed

+96
-1
lines changed

src/MongoDB.Driver/Linq/Linq3Implementation/Ast/Expressions/AstDateDiffExpression.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public AstDateDiffExpression Update(
7676
AstExpression timezone,
7777
AstExpression startOfWeek)
7878
{
79-
if (startDate == _startDate && EndDate == _endDate && unit == _unit && startOfWeek == _startOfWeek && timezone == _timezone)
79+
if (startDate == _startDate && endDate == _endDate && unit == _unit && startOfWeek == _startOfWeek && timezone == _timezone)
8080
{
8181
return this;
8282
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/* Copyright 2010-present MongoDB Inc.
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
using System;
17+
using System.Globalization;
18+
using System.Linq;
19+
using FluentAssertions;
20+
using MongoDB.Driver.Core.Misc;
21+
using MongoDB.Driver.Core.TestHelpers.XunitExtensions;
22+
using MongoDB.Driver.Linq;
23+
using Xunit;
24+
25+
namespace MongoDB.Driver.Tests.Linq.Linq3ImplementationTests.Jira
26+
{
27+
public class CSharp2108Tests : Linq3IntegrationTest
28+
{
29+
[Fact]
30+
public void Aggregate_Project_should_work()
31+
{
32+
RequireServer.Check().Supports(Feature.DateOperatorsNewIn50);
33+
var collection = CreateCollection();
34+
var endDate = DateTime.Parse("2020-01-03Z", null, DateTimeStyles.AdjustToUniversal);
35+
36+
var queryable = collection.Aggregate()
37+
.Sort(Builders<C>.Sort.Ascending(x => x.Id))
38+
.Project(x => new { Days = endDate.Subtract(x.StartDate, DateTimeUnit.Day) });
39+
40+
var stages = Translate(collection, queryable);
41+
AssertStages(
42+
stages,
43+
"{ $sort : { _id : 1 } }",
44+
"{ $project : { Days : { $dateDiff : { startDate : '$StartDate', endDate : ISODate('2020-01-03T00:00:00Z'), unit : 'day' } }, _id : 0 } }");
45+
46+
var results = queryable.ToList();
47+
results.Should().HaveCount(2);
48+
results[0].ShouldBeEquivalentTo(new { Days = 2 });
49+
results[1].ShouldBeEquivalentTo(new { Days = 1 });
50+
}
51+
52+
[Fact]
53+
public void Queryable_Select_should_work()
54+
{
55+
RequireServer.Check().Supports(Feature.DateOperatorsNewIn50);
56+
var collection = CreateCollection();
57+
var endDate = DateTime.Parse("2020-01-03Z", null, DateTimeStyles.AdjustToUniversal);
58+
59+
var queryable = collection.AsQueryable()
60+
.OrderBy(x => x.Id)
61+
.Select(x => new { Days = endDate.Subtract(x.StartDate, DateTimeUnit.Day) });
62+
63+
var stages = Translate(collection, queryable);
64+
AssertStages(
65+
stages,
66+
"{ $sort : { _id : 1 } }",
67+
"{ $project : { Days : { $dateDiff : { startDate : '$StartDate', endDate : ISODate('2020-01-03T00:00:00Z'), unit : 'day' } }, _id : 0 } }");
68+
69+
var results = queryable.ToList();
70+
results.Should().HaveCount(2);
71+
results[0].ShouldBeEquivalentTo(new { Days = 2 });
72+
results[1].ShouldBeEquivalentTo(new { Days = 1 });
73+
}
74+
75+
private IMongoCollection<C> CreateCollection()
76+
{
77+
var collection = GetCollection<C>();
78+
79+
var documents = new[]
80+
{
81+
new C { Id = 1, StartDate = DateTime.Parse("2020-01-01Z", null, DateTimeStyles.AdjustToUniversal) },
82+
new C { Id = 2, StartDate = DateTime.Parse("2020-01-02Z", null, DateTimeStyles.AdjustToUniversal) }
83+
};
84+
CreateCollection(collection, documents);
85+
86+
return collection;
87+
}
88+
89+
private class C
90+
{
91+
public int Id { get; set; }
92+
public DateTime StartDate { get; set; }
93+
}
94+
}
95+
}

0 commit comments

Comments
 (0)