0

我有一个带有实体框架的 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();
        }
    }

谢谢。

4

1 回答 1

0

这样的事情有用吗?

return requestRepository.GetAll().Count(req => 
    req.TransportedMaterials.Any(tm => 
        tm.MaterialPacking.MaterialPackingID == materialPacking.MaterialPackingID)
    );

假设:您需要包含一个或多个 TransportedMaterial 对象的请求计数,这些对象的 MaterialPackingID 与您感兴趣的对象相匹配。

于 2013-01-21T19:27:43.770 回答