Uploaded image for project: 'C# Driver'
  1. C# Driver
  2. CSHARP-2046

consecutive SelectMany calls always return empty list

    • Type: Icon: Bug Bug
    • Resolution: Fixed
    • Priority: Icon: Critical - P2 Critical - P2
    • 2.8.0
    • Affects Version/s: None
    • Component/s: Linq
    • None

      Unable to find source-code formatter for language: csharp. Available languages are: actionscript, ada, applescript, bash, c, c#, c++, cpp, css, erlang, go, groovy, haskell, html, java, javascript, js, json, lua, none, nyan, objc, perl, php, python, r, rainbow, ruby, scala, sh, sql, swift, visualbasic, xml, yaml
      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();
      
              }
          }
      }
      

            Assignee:
            dmitry.lukyanov@mongodb.com Dmitry Lukyanov (Inactive)
            Reporter:
            prezime ime
            Votes:
            1 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              Resolved: