与实体框架的复杂总和

时间:2013-09-12 07:08:43

标签: c# linq entity-framework

我需要帮助在复杂的环境中使用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; }
}

从产品中我想要时间和数量。 首先从Product,迭代所有ProductArticle然后在Article迭代ArticleTime内,然后在这里转到Time and Sum this。

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

1 个答案:

答案 0 :(得分:1)

如果我理解正确你需要总数量

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