【问题标题】:Mongo Driver Linq Query throws ExceptionMongodb Driver Linq Query 抛出异常
【发布时间】:2017-09-20 10:39:28
【问题描述】:

我正在使用:

  • Asp.net Core 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 {

                    };

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

“System.Collections.Generic.IEnumerable`1[TableB]”类型的表达式不能用于“System.Linq.IQueryable`1[方法的“System.Linq.IQueryable`1[TableB]”类型的参数f__AnonymousType2`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 驱动程序不支持我的查询?

【问题讨论】:

  • 似乎括号中的表达式返回IEnumerable,但连接期望IQueryable

标签: c# linq mongodb-.net-driver


【解决方案1】:

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

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

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

【讨论】:

    猜你喜欢
    • 2022-06-15
    • 1970-01-01
    • 2011-05-25
    • 1970-01-01
    • 2019-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-24
    相关资源
    最近更新 更多