C# 具有实体框架的复和
我需要帮助在复杂环境中使用linq进行求和 这就是实体C# 具有实体框架的复和,c#,linq,entity-framework,C#,Linq,Entity Framework,我需要帮助在复杂环境中使用linq进行求和 这就是实体 public class Product : EntityBase { [Key] public int ProductId { get; set; } public string Description { get; set; } public string ReportText1 { get; set; } public string ReportT
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; }
}
公共类产品:EntityBase
{
[关键]
public int ProductId{get;set;}
公共字符串说明{get;set;}
公共字符串ReportText1{get;set;}
公共字符串ReportText2{get;set;}
公共布尔标准{get;set;}
public int ProductGroupId{get;set;}
公共十进制?附加费1{get;set;}
公共十进制?附加费2{get;set;}
公共十进制?附加费3{get;set;}
公共十进制?附加费4{get;set;}
公共十进制价格{get;set;}
公共十进制价格输出{get;set;}
在{get;set;}中的公共十进制值
公共十进制输出{get;set;}
公共十进制价格总计{get;set;}
公共十进制价格总计{get;set;}
公共十进制价格TotalOutVAT{get;set;}
公共十进制价格调整{get;set;}
公共bool计算{get;set;}
公共字符串注释{get;set;}
公共虚拟产品组产品组{get;set;}
公共虚拟ICollection ProductArticles{get;set;}
}
公共类ProductArticle:EntityBase
{
[关键]
public int ProductArticleId{get;set;}
public int ProductId{get;set;}
公共int ArticleId{get;set;}
公共十进制数量{get;set;}
公共十进制价格{get;set;}
公共布尔主{get;set;}
公共虚拟产品产品{get;set;}
公共虚拟项目{get;set;}
}
公共类文章:EntityBase
{
[关键]
公共int ArticleId{get;set;}
公共字符串ErpId{get;set;}
公共字符串提供程序{get;set;}
公共字符串说明{get;set;}
公共字符串规范{get;set;}
公共字符串单元{get;set;}
公共字符串帐户{get;set;}
公共十进制价格{get;set;}
公共虚拟ICollection ArticleTimes{get;set;}
}
公共类ArticleTime:EntityBase
{
[关键]
public int ArticleTimeId{get;set;}
公共int ArticleId{get;set;}
公共int-TimeId{get;set;}
公共虚拟项目{get;set;}
公共虚拟时间{get;set;}
}
公共类时间:EntityBase
{
[关键]
公共int-TimeId{get;set;}
公共ID{get;set;}
公共字符串段落{get;set;}
公共字符串说明{get;set;}
公共字符串单元{get;set;}
公共十进制价格{get;set;}
公共布尔系统{get;set;}
公共十进制数量{get;set;}
}
从产品我想在时间总和数量。
首先从Product开始,迭代所有ProductArticle,然后在文章内部迭代ArticleTime,然后在此内部转到时间并求和
还是最好在Product name TotalQty中创建一个新列,然后在save方法中进行求和?问题是,当我为文章添加时间时,产品实体不会自动更新。有什么想法吗?如果我没听错,你需要一个总数量
var sum = dbContext
.Products
.Sum(p => p.ProductArticles
.Sum(pa => pa.Article
.ArticleTimes
.Sum(at => at.Qty)));