Mongo Driver Linq Query抛出异常

S P*_*S P 7 c# linq mongodb-.net-driver

我正在使用:

  • Asp.net核心1.1
  • MongoDB驱动程序2.4.4

我正在执行以下LINQ查询:

        var collection = _db.GetCollection<TableA>("TableAs");
        var collectionTableB = _db.GetCollection<TableB>("TableBs");
        var collectionTableC = _db.GetCollection<TableCs>("TableCs");


        var query = from c in collection.AsQueryable()
                    join i in (from f in collectionTableB.AsQueryable()
                               join p in collectionTableC.AsQueryable() 
                                    on f.PcbaId equals p.PcbaId into i
                               from x in i.DefaultIfEmpty()
                               select new { f, x })
                  on c.AssemblyId equals i.f.AssemblyId into cap
                    from i in cap.DefaultIfEmpty()
                    select new ConfigurableItemDto {

                    };
Run Code Online (Sandbox Code Playgroud)

当我执行它时,它会抛出以下异常:

类型'System.Collections.Generic.IEnumerable`1 [TableB]'的表达式不能用于方法'System.Linq.IQueryable`1 [<> f__AnonymousType2'的类型'System.Linq.IQueryable`1 [TableB]'的参数`2 [TableB,System.Collections.Generic.IEnumerable`1 [TableC]]] GroupJoin [Assembly,Pcba,String,<> f__AnonymousType2`2](System.Linq.IQueryable`1 [TableB],System.Collections.Generic .IEnumerable`1 [TableC],System.Linq.Expressions.Expression`1 [System.Func`2 [TableB,System.String]],System.Linq.Expressions.Expression`1 [System.Func`2 [TableC, System.String]],System.Linq.Expressions.Expression`1 [System.Func`3 [TableB,System.Collections.Generic.IEnumerable`1 [TableC],<> f__AnonymousType2`2 [TableB,System.Collections.Generic .IEnumerable`1 [TableC]]]])'参数名称:arg0

我的查询有问题吗?也许MongoDB驱动程序不支持我的查询?

Ger*_*old 2

MongoDb 驱动程序确实支持GroupJoin,但似乎不支持GroupJoin匿名类型的集合。幸运的是,您的查询可以重写为GroupJoins 之间的MongoCollections:

var query = from a in collection.AsQueryable()
            join b in collectionTableB.AsQueryable()
                on a.AssemblyId equals b.AssemblyId into bj
            from b in bj.DefaultIfEmpty()
            join c in collectionTableC.AsQueryable() 
                on b.PcbaId equals c.PcbaId into cj
            from c in cj.DefaultIfEmpty()
            select new ConfigurableItemDto {
                 a.SomeAProperty,
                 b.SomeBProperty,
                 c.SomeCProperty,
            };
Run Code Online (Sandbox Code Playgroud)

我没有运行 MongoDb,所以我无法尝试此查询的结果(我通常会这样做)。请试一试。