C#MongoDB奇怪的行为和异常与嵌套查询

时间:2017-05-30 04:28:53

标签: c# mongodb nested asqueryable

我在https://github.com/MyGitHubTests/MongoNestedTests/tree/master/MongoTest

处建立了一个小型测试项目

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

 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; }
    }

表示JSON高于https://github.com/MyGitHubTests/MongoNestedTests/blob/master/MongoTest/MongoTest/Poco.cs的Poco类,您可以看到它非常简单直接,但嵌套对象除外:

    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);
                }

我试图使用database.GetCollection()。AsQueryable()来查询数据并产生相当奇怪的结果。

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

List<IEnumerable<List<Cargo>>>

这是我的问题: 1)查看返回的cargoList类型为List<>的最后一个示例 - 我无法弄清楚为什么有List - IEnumerable - List的组合,我的类只使用{{1}这自然是我所期待的。

2)我可以通过var drivers = queryable.Select(a => a.Cars.Select(b => b.Drivers));选择驱动程序的情况,但如果我尝试使用var resultDriver = from a in queryable from b in a.Cars from c in b.Drivers select c;做同样的事情,我会得到&#39; System.NotSupportedException&#39; :{&#34; $ project或$ group不支持{document}。&#34;}

3)最后有一种方法可以放宽List<IEnumerable<List<Cargo>>>List<Cargo>的结果吗?

0 个答案:

没有答案