|
| 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.Linq; |
| 17 | +using FluentAssertions; |
| 18 | +using MongoDB.Driver.Linq; |
| 19 | +using Xunit; |
| 20 | + |
| 21 | +namespace MongoDB.Driver.Tests.Linq.Linq3ImplementationTests.Jira |
| 22 | +{ |
| 23 | + public class CSharp4304Tests : Linq3IntegrationTest |
| 24 | + { |
| 25 | + [Fact] |
| 26 | + public void Example_should_work() |
| 27 | + { |
| 28 | + var collection = GetCollection<Parent>(); |
| 29 | + var database = collection.Database; |
| 30 | + |
| 31 | + var queryable = GetChildrenQueryable<Child>(database, "Child"); |
| 32 | + |
| 33 | + var stages = Translate(collection, queryable); |
| 34 | + AssertStages( |
| 35 | + stages, |
| 36 | + "{ $project : { _outer : '$$ROOT', _id : 0 } }", |
| 37 | + "{ $lookup : { from : 'Child', localField : '_outer.ChildId', foreignField : '_id', as : '_inner' } }", |
| 38 | + "{ $unwind : '$_inner' }", |
| 39 | + "{ $project : { _v : '$_inner', _id : 0 } }"); |
| 40 | + |
| 41 | + CreateParentCollection(); |
| 42 | + CreateChildCollection(); |
| 43 | + var results = queryable.ToList(); |
| 44 | + results.OrderBy(r => r.Id).Select(r => r.Id).Should().Equal("11", "22"); |
| 45 | + } |
| 46 | + |
| 47 | + private IMongoQueryable<TChild> GetChildrenQueryable<TChild>(IMongoDatabase db, string childCollectionName) where TChild : IEntity |
| 48 | + { |
| 49 | + var parentCollection = db.GetCollection<Parent>("Parent"); |
| 50 | + var childCollection = db.GetCollection<TChild>(childCollectionName).AsQueryable(); |
| 51 | + |
| 52 | + return parentCollection |
| 53 | + .AsQueryable() |
| 54 | + .Join( |
| 55 | + childCollection, |
| 56 | + p => p.ChildId, |
| 57 | + c => c.Id, |
| 58 | + (_, c) => c); |
| 59 | + } |
| 60 | + |
| 61 | + private void CreateParentCollection() |
| 62 | + { |
| 63 | + var parentCollection = GetCollection<Parent>("Parent"); |
| 64 | + |
| 65 | + CreateCollection( |
| 66 | + parentCollection, |
| 67 | + new Parent { Id = "1", ChildId = "11" }, |
| 68 | + new Parent { Id = "2", ChildId = "22" }); |
| 69 | + } |
| 70 | + |
| 71 | + private void CreateChildCollection() |
| 72 | + { |
| 73 | + var childCollection = GetCollection<Child>("Child"); |
| 74 | + |
| 75 | + CreateCollection( |
| 76 | + childCollection, |
| 77 | + new Child { Id = "11" }, |
| 78 | + new Child { Id = "22" }); |
| 79 | + } |
| 80 | + |
| 81 | + public interface IEntity |
| 82 | + { |
| 83 | + string Id { get; set; } |
| 84 | + } |
| 85 | + |
| 86 | + public class Parent : IEntity |
| 87 | + { |
| 88 | + public string Id { get; set; } |
| 89 | + public string ChildId { get; set; } |
| 90 | + } |
| 91 | + |
| 92 | + public class Child : IEntity |
| 93 | + { |
| 94 | + public string Id { get; set; } |
| 95 | + } |
| 96 | + } |
| 97 | +} |
0 commit comments