使用实体框架的复杂总和

本文关键字:复杂 框架 实体 | 更新日期: 2023-09-27 18:30:58

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

这是实体

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; }
}

从产品我想要总和数量在时间。首先从产品,迭代所有产品文章,然后在文章中迭代文章时间,然后在里面转到时间并求和。

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

使用实体框架的复杂总和

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

var sum = dbContext
          .Products
          .Sum(p => p.ProductArticles
                     .Sum(pa => pa.Article
                                  .ArticleTimes
                                  .Sum(at => at.Qty)));