Skip to content

Commit 4643231

Browse files
rstamDmitryLukyanov
authored andcommitted
CSHARP-4562: Use PartialEvaluator when translating an Expression to a field.
1 parent 51b9751 commit 4643231

File tree

2 files changed

+77
-0
lines changed

2 files changed

+77
-0
lines changed

src/MongoDB.Driver/Linq/Linq3Implementation/LinqProviderAdapterV3.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ internal override RenderedFieldDefinition TranslateExpressionToField<TDocument>(
7979
IBsonSerializer<TDocument> documentSerializer,
8080
IBsonSerializerRegistry serializerRegistry)
8181
{
82+
expression = (LambdaExpression)PartialEvaluator.EvaluatePartially(expression);
8283
var parameter = expression.Parameters.Single();
8384
var context = TranslationContext.Create(expression, documentSerializer);
8485
var symbol = context.CreateSymbol(parameter, documentSerializer, isCurrent: true);
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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.Collections.Generic;
17+
using MongoDB.Driver.Linq;
18+
using MongoDB.TestHelpers.XunitExtensions;
19+
using Xunit;
20+
21+
namespace MongoDB.Driver.Tests.Linq.Linq3ImplementationTests.Jira
22+
{
23+
public class CSharp4562Tests : Linq3IntegrationTest
24+
{
25+
[Theory]
26+
[ParameterAttributeData]
27+
public void SortBy_using_constant_should_work(
28+
[Values(LinqProvider.V2, LinqProvider.V3)] LinqProvider linqProvider)
29+
{
30+
var collection = CreateCollection(linqProvider);
31+
32+
var aggregate = GetTranslationsSortedByEnglish(collection);
33+
34+
var stages = Translate(collection, aggregate);
35+
AssertStages(stages, "{ $sort : { 'Text.en' : 1 } }");
36+
}
37+
38+
[Theory]
39+
[ParameterAttributeData]
40+
public void SortBy_using_parameter_should_work(
41+
[Values(LinqProvider.V2, LinqProvider.V3)] LinqProvider linqProvider)
42+
{
43+
var collection = CreateCollection(linqProvider);
44+
var language = "sp";
45+
46+
var aggregate = GetSortedTranslations(collection, language);
47+
48+
var stages = Translate(collection, aggregate);
49+
AssertStages(stages, "{ $sort : { 'Text.sp' : 1 } }");
50+
}
51+
52+
private IMongoCollection<Translation> CreateCollection(LinqProvider linqProvider)
53+
{
54+
var collection = GetCollection<Translation>("test", linqProvider);
55+
return collection;
56+
}
57+
58+
private class Translation
59+
{
60+
public int Id { get; set; }
61+
public Dictionary<string, string> Text { get; set; }
62+
}
63+
64+
private static IAggregateFluent<Translation> GetSortedTranslations(IMongoCollection<Translation> collection, string language)
65+
{
66+
return collection.Aggregate()
67+
.SortBy(c => c.Text[language]);
68+
}
69+
70+
private static IAggregateFluent<Translation> GetTranslationsSortedByEnglish(IMongoCollection<Translation> collection)
71+
{
72+
return collection.Aggregate()
73+
.SortBy(c => c.Text["en"]);
74+
}
75+
}
76+
}

0 commit comments

Comments
 (0)