0

我需要帮助才能sum在复杂的环境中使用 linq

这是实体

public class Product : EntityBase
    {
        [Key]
        public int ProductId { get; set; }
        public string Description { get; set; }
        public string ReportText1 { get; set; }
        public string ReportText2 { get; set; }
        public bool Standard { get; set; }
        public int ProductGroupId { get; set; }
        public decimal? Surcharge1 { get; set; }
        public decimal? Surcharge2 { get; set; }
        public decimal? Surcharge3 { get; set; }
        public decimal? Surcharge4 { get; set; }
        public decimal PriceIn { get; set; }
        public decimal PriceOut { get; set; }
        public decimal PriceArtisanIn { get; set; }
        public decimal PriceArtisanOut { get; set; }
        public decimal PriceTotalIn { get; set; }
        public decimal PriceTotalOut { get; set; }
        public decimal PriceTotalOutVat { get; set; }
        public decimal PriceAdjustment { get; set; }
        public bool Calculate { get; set; }
        public string Notes { get; set; }
        public virtual ProductGroup ProductGroup { get; set; }
        public virtual ICollection<ProductArticle> ProductArticles { get; set; }
    }

public class ProductArticle : EntityBase
{
    [Key]
    public int ProductArticleId { get; set; }
    public int ProductId { get; set; }
    public int ArticleId { get; set; }
    public decimal Qty { get; set; }
    public decimal PriceIn { get; set; }
    public bool Primary { get; set; }
    public virtual Product Product { get; set; }
    public virtual Article Article { get; set; }
}

public class Article : EntityBase
{
    [Key]
    public int ArticleId { get; set; }
    public string ErpId { get; set; }
    public string Supplier { get; set; }
    public string Description { get; set; }
    public string Specification { get; set; }
    public string Unit { get; set; }
    public string Account { get; set; }
    public decimal PriceIn { get; set; }
    public virtual ICollection<ArticleTime> ArticleTimes { get; set; }
}

public class ArticleTime : EntityBase
{
    [Key]
    public int ArticleTimeId { get; set; }   
    public int ArticleId { get; set; }      
    public int TimeId { get; set; }
    public virtual Article Article { get; set; }
    public virtual Time Time { get; set; }

}

public class Time : EntityBase
{
    [Key]
    public int TimeId { get; set; }
    public int ArtisanId { get; set; }
    public string Paragraph { get; set; }
    public string Description { get; set; }
    public string Unit { get; set; }
    public decimal PriceIn { get; set; }
    public bool System { get; set; }
    public decimal Qty { get; set; }
}

从产品我想要及时的总和数量。首先从 Product 中迭代所有 ProductArticle,然后在 Article 中迭代 ArticleTime,然后在 this 中进入 Time 并求和。

或者最好在产品名称 TotalQty 中创建一个新列,并在保存方法中求和?问题是当我向文章添加时间时,产品实体不会自动更新。有任何想法吗?

4

1 回答 1

1

如果我理解正确,您总共需要数量

var sum = dbContext
          .Products
          .Sum(p => p.ProductArticles
                     .Sum(pa => pa.Article
                                  .ArticleTimes
                                  .Sum(at => at.Qty)));
于 2013-09-12T07:23:37.307 回答