C# C MongoDB奇数行为和嵌套查询异常

C# C MongoDB奇数行为和嵌套查询异常,c#,mongodb,nested,asqueryable,C#,Mongodb,Nested,Asqueryable,我构建了一个小型的测试项目,可以在 下面是我的JSON文档示例github.com/MyGitHubTests/MongoNestedTests/blob/master/MongoTest/MongoTest/TestJSON/MongoTest2.JSON: { "Cars": [ { "Engine": { "Type": "V8", "Displacement": 4.8 }, "Body": { "Style": "Targa", "Color": "

我构建了一个小型的测试项目,可以在

下面是我的JSON文档示例github.com/MyGitHubTests/MongoNestedTests/blob/master/MongoTest/MongoTest/TestJSON/MongoTest2.JSON:

{
  "Cars": [
    {
      "Engine": { "Type": "V8", "Displacement": 4.8 },
      "Body": { "Style": "Targa", "Color": "White" },
      "Drivers": [
        {
          "Name": { "First": "Bob", "Last": "Fast" },
          "Address": { "Street": "Top Ct", "City": "Seattle" },
          "Contact": { "Phone": "(000) 000-0000", "Email": "bob@email.me" }
        }
      ],
      "Trips": [
        {
      "Note" : "Trip F",
          "Mileage": { "Start": 1000, "End": 2000 },
          "Period": { "Start": "2017-12-01T00:00:00", "End": "2017-12-02T00:00:00" },
          "Cargo": [
            {
              "Size": { "Height": 100, "Wight": 200, "Lenght": 300 },
              "Info": { "Weight": 400, "Name": "Cargo Name F1"}
            },
            {
              "Size": { "Height": 500, "Wight": 600, "Lenght": 700 },
              "Info": { "Weight": 800, "Name": "Cargo Name F2"}
            },
            {
              "Size": { "Height": 900, "Wight": 1000, "Lenght": 1100 },
              "Info": { "Weight": 1200, "Name": "Cargo Name F3"}
            }
          ]
    }
      ]
    }
  ]
}
上面表示JSON的Poco类,正如您所看到的,除了嵌套对象之外,它非常简单和直接:

 public class Cargo
    {
        public Size Size { get; set; }
        public Info Info { get; set; }
    }

    public class Trip
    {
        public string Note { get; set; }
        public Mileage Mileage { get; set; }
        public Period Period { get; set; }
        public List<Cargo> Cargo { get; set; }
    }

    public class Car
    {
        public Engine Engine { get; set; }
        public Body Body { get; set; }
        public List<Driver> Drivers { get; set; }
        public List<Trip> Trips { get; set; }
    }

    public class Poco
    {
        public ObjectId Id { get; set; }
        public List<Car> Cars { get; set; }
    }
我试图使用database.GetCollection.AsQueryable来查询数据,它会产生非常奇怪的结果

这是我的代码github.com/MyGitHubTests/MongoNestedTests/blob/master/MongoTest/MongoTest/Program.cs

    var collection = database.GetCollection<Poco>("cars");
    var queryable = collection.AsQueryable<Poco>();

    var count = queryable.Count();
    Console.WriteLine("Queryable {0} count: {1}", queryable, count);

    // Working example
    var cars = queryable.Select(a => a.Cars);

    foreach (IList<Car> car in cars)
    {
        Console.WriteLine("car.Count {0}", car.Count);
    }

    // Working example
    var qry = queryable.Select(a => a.Cars.Select(b => b.Engine));

    foreach (IList<Engine> engines in qry)
        foreach (Engine engine in engines)
        {
            Console.WriteLine("Engine Type {0}, Displacement {1}", engine.Type, engine.Displacement);
        }

    // Working example
    var result = from a in queryable
                 from b in a.Cars
                 select b.Engine;

    foreach (Engine engine in result)
    {
        Console.WriteLine("Engine Type {0}, Displacement {1}", engine.Type, engine.Displacement);
    }

    // Working example
    // Note: it's not clear why I have to use List here instead of IList as above
    var drivers = queryable.Select(a => a.Cars.Select(b => b.Drivers));

    foreach (List<List<Driver>> listA in drivers)
        foreach (List<Driver> listB in listA)
            foreach (Driver driver in listB)
            {
                Console.WriteLine("Driver Name: {0}, {1}", driver.Name.First, driver.Name.Last);
            }

    // 'System.NotSupportedException' : {"$project or $group does not support {document}."}
    //var resultDriver = from a in queryable
    //             from b in a.Cars
    //             from c in b.Drivers
    //             select c;
    //foreach (Driver driver in resultDriver)
    //{
    //    Console.WriteLine("Driver Name: {0}, {1}", driver.Name.First, driver.Name.Last);
    //}

    // Working example
    // Note: it's not clear why I have to use IEnumerable here instead of List as above
    var cargoList = queryable.Select(a => a.Cars.Select(b => b.Trips.Select(c => c.Cargo)));

    foreach (List<IEnumerable<List<Cargo>>> listA in cargoList)
        foreach (IEnumerable<List<Cargo>> listB in listA)
            foreach (List<Cargo> listC in listB)
                foreach (Cargo cargo in listC)
                {
                    Console.WriteLine("Cargo Name: {0}, Height: {1}", cargo.Info.Name, cargo.Size.Height);
                }
以下是我的问题: 我看最后一个示例,其中返回的cargoList类型是List-我不明白为什么会有List-IEnumerable-List的组合,我的类只使用List,这是我所期望的

2个案例,我可以通过var drivers=queryable选择驱动程序。Selecta=>a.Cars.Selectb=>b.drivers;工作,但如果我尝试使用var resultDriver=从a在a中查询从b在a中。汽车从c在b中。司机选择c;我得到'System.NotSupportedException':{$project或$group不支持{document}

3最后,有没有一种方法可以将上述结果从列表展开到列表