Skip to content

Commit 83ec087

Browse files
craiggwilsonrstam
authored andcommitted
CSHARP-1621: added support for $range.
1 parent 12d18e1 commit 83ec087

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

src/MongoDB.Driver/Linq/Translators/AggregateLanguageTranslator.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
using System;
1818
using System.Collections;
1919
using System.Collections.Generic;
20+
using System.Linq;
2021
using System.Linq.Expressions;
2122
using System.Reflection;
2223
using System.Text;
@@ -342,6 +343,12 @@ private BsonValue TranslateMethodCall(MethodCallExpression node)
342343
{
343344
return result;
344345
}
346+
347+
if (node.Method.DeclaringType == typeof(Enumerable)
348+
&& TryTranslateStaticEnumerableMethodCall(node, out result))
349+
{
350+
return result;
351+
}
345352
}
346353
else
347354
{
@@ -778,6 +785,28 @@ private bool TryTranslateMinResultOperator(PipelineExpression node, out BsonValu
778785
return false;
779786
}
780787

788+
private bool TryTranslateStaticEnumerableMethodCall(MethodCallExpression node, out BsonValue result)
789+
{
790+
result = null;
791+
switch (node.Method.Name)
792+
{
793+
case "Range":
794+
var start = TranslateValue(node.Arguments[0]);
795+
result = new BsonDocument("$range", new BsonArray
796+
{
797+
start,
798+
new BsonDocument("$add", new BsonArray
799+
{
800+
start,
801+
TranslateValue(node.Arguments[1])
802+
})
803+
});
804+
return true;
805+
}
806+
807+
return false;
808+
}
809+
781810
private bool TryTranslateStaticMathMethodCall(MethodCallExpression node, out BsonValue result)
782811
{
783812
result = null;

tests/MongoDB.Driver.Tests/Linq/Translators/AggregateProjectTranslatorTests.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -800,6 +800,18 @@ public void Should_translate_pow()
800800
result.Value.Result.Should().Be(161051);
801801
}
802802

803+
[SkippableFact]
804+
public void Should_translate_range()
805+
{
806+
RequireServer.Where(minimumVersion: "3.3.4");
807+
808+
var result = Project(x => new { Result = Enumerable.Range(x.C.E.F, 3) });
809+
810+
result.Projection.Should().Be("{ Result: { \"$range\": [\"$C.E.F\", { \"$add\": [\"$C.E.F\", 3] } ] }, _id: 0 }");
811+
812+
result.Value.Result.Should().BeEquivalentTo(11, 12, 13);
813+
}
814+
803815
[SkippableFact]
804816
public void Should_translate_reverse()
805817
{

0 commit comments

Comments
 (0)