【发布时间】: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