|
| 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.Driver.Linq; |
| 20 | +using Xunit; |
| 21 | + |
| 22 | +namespace MongoDB.Driver.Tests.Linq.Linq3ImplementationTests.Jira |
| 23 | +{ |
| 24 | + public class CSharp3236Tests : Linq3IntegrationTest |
| 25 | + { |
| 26 | + [Fact] |
| 27 | + public void Select_should_work() |
| 28 | + { |
| 29 | + var collection = CreateCollection(); |
| 30 | + |
| 31 | + var queryable = collection |
| 32 | + .AsQueryable() |
| 33 | + .Select(p => new |
| 34 | + { |
| 35 | + Id = p.Id, |
| 36 | + Comments = p.Comments.Where(c => c.Text.Contains("test")) |
| 37 | + }); |
| 38 | + |
| 39 | + var stages = Translate(collection, queryable); |
| 40 | + AssertStages( |
| 41 | + stages, |
| 42 | + "{ $project : { Id : '$_id', Comments : { $filter : { input : '$Comments', as : 'c', cond : { $gte : [{ $indexOfCP : ['$$c.Text', 'test'] }, 0] } } }, _id : 0 } }"); |
| 43 | + |
| 44 | + var result = queryable.Single(); |
| 45 | + result.Id.Should().Be(1); |
| 46 | + result.Comments.Select(c => c.Id).Should().Equal(1, 3); |
| 47 | + } |
| 48 | + |
| 49 | + private IMongoCollection<Post> CreateCollection() |
| 50 | + { |
| 51 | + var collection = GetCollection<Post>("C"); |
| 52 | + |
| 53 | + CreateCollection( |
| 54 | + collection, |
| 55 | + new Post |
| 56 | + { |
| 57 | + Id = 1, |
| 58 | + Comments = new List<Comment> |
| 59 | + { |
| 60 | + new Comment { Id = 1, Text = "this is a test comment" }, |
| 61 | + new Comment { Id = 2, Text = "this is not" }, |
| 62 | + new Comment { Id = 3, Text = "and this is another test comment" } |
| 63 | + } |
| 64 | + }); |
| 65 | + |
| 66 | + return collection; |
| 67 | + } |
| 68 | + |
| 69 | + private class Post |
| 70 | + { |
| 71 | + public int Id { get; set; } |
| 72 | + public List<Comment> Comments { get; set; } |
| 73 | + |
| 74 | + } |
| 75 | + |
| 76 | + public class Comment |
| 77 | + { |
| 78 | + public int Id { get; set; } |
| 79 | + public string Text { get; set; } |
| 80 | + } |
| 81 | + } |
| 82 | +} |
0 commit comments