我想在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; }
}
Bellow是错误消息
表达式'System.Collections.Generic.IEnumerable 1[Project.Communication.EntityModels.ProductWarehouseMap]' cannot be used for parameter of type 'System.Linq.IQueryable
1 [Project.Communication.EntityModels.ProductWarehouseMap]'方法'System.Linq.IQueryable 1[Project.Communication.EntityModels.ProductWarehouseMap] Where[ProductWarehouseMap](System.Linq.IQueryable
1 [Project.Communication.EntityModels .ProductWarehouseMap],System.Linq.Expressions.Expression 1[System.Func
2 [Project.Communication.EntityModels.ProductWarehouseMap,System.Boolean]])'
参数名称:arg0
答案 0 :(得分: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();