Skip to content

Commit e235279

Browse files
rstamBorisDog
authored andcommitted
CSHARP-4047: Nested where clause in select only works with IEnumerable<T>.
1 parent f8bc005 commit e235279

File tree

2 files changed

+84
-1
lines changed

2 files changed

+84
-1
lines changed

src/MongoDB.Driver/Linq/Linq3Implementation/Translators/ExpressionToAggregationExpressionTranslators/MethodTranslators/WhereMethodToAggregationExpressionTranslator.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
using MongoDB.Driver.Linq.Linq3Implementation.Ast.Expressions;
1818
using MongoDB.Driver.Linq.Linq3Implementation.Misc;
1919
using MongoDB.Driver.Linq.Linq3Implementation.Reflection;
20+
using MongoDB.Driver.Linq.Linq3Implementation.Serializers;
2021

2122
namespace MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToAggregationExpressionTranslators.MethodTranslators
2223
{
@@ -37,11 +38,13 @@ public static AggregationExpression Translate(TranslationContext context, Method
3738
var predicateSymbol = context.CreateSymbol(predicateParameter, predicateParameterSerializer);
3839
var predicateContext = context.WithSymbol(predicateSymbol);
3940
var predicateTranslation = ExpressionToAggregationExpressionTranslator.Translate(predicateContext, predicateLambda.Body);
41+
var itemSerializer = ArraySerializerHelper.GetItemSerializer(sourceTranslation.Serializer);
42+
var enumerableSerializer = IEnumerableSerializer.Create(itemSerializer);
4043
var ast = AstExpression.Filter(
4144
sourceTranslation.Ast,
4245
predicateTranslation.Ast,
4346
predicateParameter.Name);
44-
return new AggregationExpression(expression, ast, sourceTranslation.Serializer);
47+
return new AggregationExpression(expression, ast, enumerableSerializer);
4548
}
4649

4750
throw new ExpressionNotSupportedException(expression);
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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 System.Linq;
18+
using Xunit;
19+
20+
namespace MongoDB.Driver.Tests.Linq.Linq3ImplementationTests.Jira
21+
{
22+
public class CSharp4047Tests : Linq3IntegrationTest
23+
{
24+
[Fact]
25+
public void Nested_where_should_work_with_array()
26+
{
27+
var collection = GetCollection<C>();
28+
29+
var queryable = collection.AsQueryable()
30+
.Select(x => new { R = x.A.Where(i => i == 1)});
31+
32+
var stages = Translate(collection, queryable);
33+
var expectedStages = new[]
34+
{
35+
"{ $project : { R : { $filter : { input : '$A', as : 'i', cond : { $eq : ['$$i', 1] } } }, _id : 0 } }"
36+
};
37+
AssertStages(stages, expectedStages);
38+
}
39+
40+
[Fact]
41+
public void Nested_where_should_work_with_IEnumerable()
42+
{
43+
var collection = GetCollection<C>();
44+
45+
var queryable = collection.AsQueryable()
46+
.Select(x => new { R = x.E.Where(i => i == 1) });
47+
48+
var stages = Translate(collection, queryable);
49+
var expectedStages = new[]
50+
{
51+
"{ $project : { R : { $filter : { input : '$E', as : 'i', cond : { $eq : ['$$i', 1] } } }, _id : 0 } }"
52+
};
53+
AssertStages(stages, expectedStages);
54+
}
55+
56+
[Fact]
57+
public void Nested_where_should_work_with_List()
58+
{
59+
var collection = GetCollection<C>();
60+
61+
var queryable = collection.AsQueryable()
62+
.Select(x => new { R = x.L.Where(i => i == 1) });
63+
64+
var stages = Translate(collection, queryable);
65+
var expectedStages = new[]
66+
{
67+
"{ $project : { R : { $filter : { input : '$L', as : 'i', cond : { $eq : ['$$i', 1] } } }, _id : 0 } }"
68+
};
69+
AssertStages(stages, expectedStages);
70+
}
71+
72+
private class C
73+
{
74+
public int Id { get; set; }
75+
public int[] A { get; set; }
76+
public IEnumerable<int> E { get; set; }
77+
public List<int> L { get; set; }
78+
}
79+
}
80+
}

0 commit comments

Comments
 (0)