|
| 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 FluentAssertions; |
| 19 | +using MongoDB.Bson; |
| 20 | +using Xunit; |
| 21 | + |
| 22 | +namespace MongoDB.Driver.Tests.Linq.Linq3ImplementationTests.Jira |
| 23 | +{ |
| 24 | + public class CSharp2308Tests |
| 25 | + { |
| 26 | + [Fact] |
| 27 | + public void Nested_Select_should_work() |
| 28 | + { |
| 29 | + var client = DriverTestConfiguration.Linq3Client; |
| 30 | + var database = client.GetDatabase("FooBar"); |
| 31 | + var collection = database.GetCollection<FooNest>("Foos"); |
| 32 | + |
| 33 | + database.DropCollection("Foos"); |
| 34 | + |
| 35 | + collection.InsertOne( |
| 36 | + new FooNest |
| 37 | + { |
| 38 | + Name = "Parent", |
| 39 | + NestedCollection = new[] { |
| 40 | + new FooNest { |
| 41 | + Name = "Child" |
| 42 | + } |
| 43 | + } |
| 44 | + }); |
| 45 | + |
| 46 | + var queryable = collection.AsQueryable() |
| 47 | + .Select(top => top.NestedCollection.Select(child => new { ParentName = top.Name, child.Name })); |
| 48 | + |
| 49 | + var stages = Linq3TestHelpers.Translate(collection, queryable); |
| 50 | + var expectedStages = new[] |
| 51 | + { |
| 52 | + "{ $project : { _v : { $map : { input : '$NestedCollection', as : 'child', in : { ParentName : '$Name', Name : '$$child.Name' } } }, _id : 0 } }" |
| 53 | + }; |
| 54 | + Linq3TestHelpers.AssertStages(stages, expectedStages); |
| 55 | + |
| 56 | + var pipelineDefinition = new BsonDocumentStagePipelineDefinition<FooNest, BsonDocument>(stages); |
| 57 | + var resultAsDocument = collection.Aggregate(pipelineDefinition).ToList().Single(); |
| 58 | + resultAsDocument.Should().Be("{ _v : [{ ParentName : 'Parent', Name : 'Child' }] }"); |
| 59 | + |
| 60 | + var result = queryable.ToList().Single().ToList(); |
| 61 | + result.Should().HaveCount(1); |
| 62 | + result[0].ParentName.Should().Be("Parent"); |
| 63 | + result[0].Name.Should().Be("Child"); |
| 64 | + } |
| 65 | + |
| 66 | + public class FooNest |
| 67 | + { |
| 68 | + public string Name; |
| 69 | + public IEnumerable<FooNest> NestedCollection; |
| 70 | + } |
| 71 | + } |
| 72 | +} |
0 commit comments