[CSHARP-2046] consecutive SelectMany calls always return empty list Created: 27/Sep/17  Updated: 28/Oct/23  Resolved: 30/Jan/19

Status: Closed
Project: C# Driver
Component/s: Linq
Affects Version/s: None
Fix Version/s: 2.8.0

Type: Bug Priority: Critical - P2
Reporter: ime Assignee: Dmitry Lukyanov (Inactive)
Resolution: Fixed Votes: 1
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
is related to CSHARP-2158 Incorrect $unwind query when using Se... Closed

 Description   

using System;
using System.Collections.Generic;
using MongoDB.Bson.Serialization.Attributes;
using MongoDB.Bson.Serialization.IdGenerators;
using MongoDB.Driver;
using MongoDB.Driver.Linq;
 
namespace Mongo2
{
    class Group
    {
        [BsonId(IdGenerator = typeof(GuidGenerator))]
        public Guid ID { get; set; }
        public string Name { get; set; }
        public List<Brand> Brands { get; set; }
    }
 
    class Brand
    {
        public string Name { get; set; }
        public List<Model> Models { get; set; }
    }
 
    class Model
    {
        public string Name { get; set; }
        public int Produced { get; set; }
    }
 
    class Program
    {
        static void Main(string[] args)
        {
            MongoClient client = new MongoClient("mongodb://127.0.0.1:32768");
            var db = client.GetDatabase("test");
            var collection = db.GetCollection<Group>("groups");
 
            var fca = new Group { Name = "FCA" };
 
            var alfaRomeo = new Brand { Name = "Alfra Romeo" };
            var jeep = new Brand { Name = "Jeep" };
            var ferrari = new Brand { Name = "Ferrari"};
 
            var laFerrari = new Model { Name = "LaFerrari", Produced = 4 };
 
            var wrangler = new Model { Name = "Wrangler", Produced = 3 };
            var compass = new Model { Name = "Compass", Produced = 8 };
 
            var giulietta = new Model { Name = "Giulietta", Produced = 7 };
            var giulia = new Model { Name = "Giulia", Produced = 8 };
            var _4c = new Model { Name = "4C", Produced = 6 };        
 
            fca.Brands = new List<Brand> { ferrari, alfaRomeo, jeep };
 
            ferrari.Models = new List<Model> { laFerrari };
            jeep.Models = new List<Model> { wrangler, compass };
            alfaRomeo.Models = new List<Model> { giulietta, giulia, _4c };
 
            collection.InsertOne(fca);                 
 
            Console.WriteLine("press enter to continue");
            Console.ReadLine();
 
            var models = collection.AsQueryable().SelectMany(g => g.Brands).SelectMany(b => b.Models).ToList();
            Console.WriteLine(models.Count);  //returns 0 instead of 6
            Console.ReadLine();
 
        }
    }
}


Generated at Wed Feb 07 21:41:28 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.