【问题标题】:How to join multiple documents?如何加入多个文档?
【发布时间】:2017-10-06 05:47:33
【问题描述】:

我想在 MongoDB 中加入多个文档。我使用了以下代码,但它不起作用。是不是我想错了。。我添加了包含 Id 的基本实体模型。

var products = _productRepository.GetCollection(_dbContext);
var prodWarhoseMapping = _productWarehouseMapRepository.GetCollection(_dbContext);
var warhouses = _warehouseRepository.GetCollection(_dbContext);

public class Product:BaseEntity
{
    public string product_code { get; set; }
    public decimal fat_tax { get; set; }
}

public class ProductWarehouseMap :BaseEntity
{
    public ObjectId product_id { get; set; }
    public ObjectId warehouse_id { get; set; }
    public int qty { get; set; }
    public decimal price { get; set; }
}
public class Warehouse :BaseEntity
{
    public ObjectId supplier_id { get; set; }
    public string warehouse_code { get; set; }
    public string name { get; set; }
    public string email { get; set; }
    public string phone { get; set; }
    public string street_address { get; set; }
    public string city { get; set; }
    public string postal_code { get; set; }
    public string region { get; set; }
    public string country { get; set; }
    public string manager_name { get; set; }
    public string fax { get; set; }
}

//works proper
var query1 = (from p in products
            join pm in prodWarhoseMapping on p._id equals pm.product_id
                        select new
                        {
                            Products = p,
                            ProductMapping = pm,
                        }).ToList();

//Getting error 
var query = (from p in products
    join pm in prodWarhoseMapping on p._id equals pm.product_id
    join wh in warhouses on pm.warehouse_id equals wh._id
    select new
    {
         Products = p,
         ProductMapping = pm,
         Warehouse = wh
     }).ToList();

Bellow 是基础实体 我忘了添加基础实体模型

public class BaseEntity
{
    [BsonIgnoreIfDefault]
    public ObjectId _id { get; set; }

}

下面是错误信息

方法“System.Linq.IQueryable1[Project.Communication.EntityModels.ProductWarehouseMap] Where[ProductWarehouseMap](System.Linq.IQueryable1[Project.Communication.EntityModels.ProductWarehouseMap]”的“System.Collections.Generic.IEnumerable1[Project.Communication.EntityModels.ProductWarehouseMap]' cannot be used for parameter of type 'System.Linq.IQueryable1[Project.Communication.EntityModels.ProductWarehouseMap]”类型的表达式, System.Linq.Expressions.Expression1[System.Func2[Project.Communication.EntityModels.ProductWarehouseMap,System.Boolean]])' 参数名称:arg0

【问题讨论】:

  • 我们需要的不仅仅是 Getting error 来帮助您,例如错误消息/代码。此外,_id 属性在您的类定义中也无处可见。
  • @KeyurPATEL 请检查上面的代码,我添加了包含 id 的基本实体模型。
  • 再一次,错误消息/代码会很有帮助,_id 定义看起来不错。
  • @KeyurPATEL 我添加了错误消息。请检查

标签: c# mongodb linq join iqueryable


【解决方案1】:

您的错误消息看起来似乎使用了Where,但实际上并非如此(假设您显示的代码正是您尝试编译的代码)。不过,您可以尝试一些变体,例如

使用 IEnumerable 在内存中执行 Select():

var query = (from p in products
    join pm in prodWarhoseMapping on p._id equals pm.product_id
    join wh in warhouses on pm.warehouse_id equals wh._id)
    .AsEnumerable()
    .Select(s => new 
    {
         Products = p,
         ProductMapping = pm,
         Warehouse = wh
    }).ToList();

或使用AsQueryable()

var query = (from p in products.AsQueryable()
    join pm in prodWarhoseMapping.AsQueryable() on p._id equals pm.product_id
    join wh in warhouses.AsQueryable() on pm.warehouse_id equals wh._id
    select new
    {
         Products = p,
         ProductMapping = pm,
         Warehouse = wh
    }).ToList();

编辑:如果数据集很大,上述方法将花费很长时间并使用大量内存。

为了找到根本原因,你能不能试试这个查询,让我知道它返回什么:

var query2 = (from pm in prodWarhoseMapping
        join wh in warhouses on pm.warehouse_id equals wh._id
                    select new
                    {
                        ProductMapping = pm,
                        Warehouse = wh,                            
                    }).ToList();

【讨论】:

  • 您的答案是正确的,但是,Product 和 ProdWarhoseMapping 表可能缺少数据,我还需要添加更多连接。那么它会正常工作还是会出现任何与内存相关的问题?
  • 如果您的意思是十万,是的,使用这种方法可能会出现问题。我们需要找到根本原因并解决它。试试我通过编辑添加的代码,让我知道它是否有效,它将有助于调试。
猜你喜欢
  • 2020-11-23
  • 2018-06-05
  • 2020-10-29
  • 2014-05-28
  • 2014-07-31
  • 2023-03-24
  • 2020-09-17
  • 2021-11-20
  • 2016-12-24
相关资源
最近更新 更多