Entity framework Linq group by in sum列from with where子句
我的问题是电气预算是ApprovedAmount的总和,其中CategoriesID==1Entity framework Linq group by in sum列from with where子句,entity-framework,linq,Entity Framework,Linq,我的问题是电气预算是ApprovedAmount的总和,其中CategoriesID==1 powrplantname是测试等等…您的错误是您将其交换了 public class PowerPlantsBudgetUsage { public int PowerPlantID { get; set; } public int TotalWork { get; set; } public int ElectricalWorkNo { get; set; }
powrplantname是测试等等…您的错误是您将其交换了
public class PowerPlantsBudgetUsage
{
public int PowerPlantID { get; set; }
public int TotalWork { get; set; }
public int ElectricalWorkNo { get; set; }
public int MechanicalWorkNo { get; set; }
public int CivilWorkNo { get; set; }
public int AdminWorkNo { get; set; }
public int VehicleWorkNo { get; set; }
[DisplayFormat(DataFormatString = "{0:N}")]
public decimal ElectricalBudgetOnly { get; set; }
public decimal Total { get; set; }
public string PowerPlantName { get; set; }
}
public IActionResult Total()
{
var query = _context.REHPData.Include(r => r.PowerPlants).Include(r => r.WorkCategories).GroupBy(r => r.PowerPlantID).Select(s => new PowerPlantsBudgetUsage
{
PowerPlantID = s.Key,
PowerPlantName = s.Select(p => p.PowerPlants.PowerPlantName).First(),
TotalWork = s.Count(),
ElectricalWorkNo = s.Count(x => x.WorkCategoriesID == 1),
MechanicalWorkNo = s.Count(x => x.WorkCategoriesID == 2),
CivilWorkNo = s.Count(x => x.WorkCategoriesID == 3),
AdminWorkNo = s.Count(x => x.WorkCategoriesID == 4),
VehicleWorkNo = s.Count(x => x.WorkCategoriesID == 6),
Total = s.Sum(x => x.ApprovedAmount),
ElectricalBudgetOnly = s.Sum(x => x.ApprovedAmount).Where(x=>x.WorkCategoriesID==1) /*this column result is s.Sum(x => x.ApprovedAmount).Where(WorkCategoriesID == 1) */
}).ToList();
return View(query);
}
首先应用where,然后求where结果的和请帮助我,先生,我想要的最后一条评论和简单的查询
//...
ElectricalBudgetOnly = s.Where(x => x.WorkCategoriesID == 1).Sum(x => x.ApprovedAmount)