我有一个带有实体框架的 ASP.NET MVC4 项目。
如何计算(使用 lambda 表达式)相关 MaterialPackingID = 123 的请求数?
这是我的课程:
public class Request
{
[Key]
public virtual int RequestID { get; set; }
public virtual ICollection<TransportedMaterial> TransportedMaterials { get; set; }
}
public class TransportedMaterial
{
[Key]
public int TransportedMaterialID { get; set; }
public MaterialPacking MaterialPacking { get; set; }
}
public class MaterialPacking
{
public int MaterialPackingID { get; set; }
public string DescriptionFr { get; set; }
public string DescriptionNl { get; set; }
}
所以:
1 请求可能包含几个 TransportedMaterial
1 个 TransportedMaterial 包含 1 个 MaterialPacking
这是我尝试的:
public int CountExistingRequestsFilterByMaterialPacking(int id)
{
using (var unitOfWork = UnitOfWorkFactory.Create())
{
var requestRepository = unitOfWork.Create<Request>();
var materialPackingRepository = unitOfWork.Create<MaterialPacking>();
var materialPacking = materialPackingRepository.SingleOrDefault(x => x.MaterialPackingID == id);
if (materialPacking == null)
throw new FaultException("Packing not found");
return requestRepository.GetAll().Where(x => x..... == materialPacking.MaterialPackingID).Count();
}
}
谢谢。