如何加入多个文件?

How to join multiple documents?

我想在 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();

波纹管是基础实体 我忘了添加基础实体模型

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.Linq.Expressions.Expression1[System.Func2[Project.Communication.EntityModels.ProductWarehouseMap,System.Boolean]])' 类型 'System.Collections.Generic.IEnumerable1[Project.Communication.EntityModels.ProductWarehouseMap]' cannot be used for parameter of type 'System.Linq.IQueryable1[Project.Communication.EntityModels.ProductWarehouseMap]' 的表达式 参数名称:arg0

您的错误消息似乎使用了 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();

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

为了找到根本原因,你能试试这个查询并让我知道它是什么吗returns:

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