Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Entity framework Linq group by in sum列from with where子句_Entity Framework_Linq - Fatal编程技术网

Entity framework Linq group by in sum列from with where子句

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

我的问题是电气预算是ApprovedAmount的总和,其中CategoriesID==1
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)