获取总数并传递给 View Model

Get the Total and pass to View Model

在我的 ASP.NET MVC 应用程序中,我想创建一个请求详细信息视图和每个请求的总金额。

这就是我创建视图模型的方式

public class ApprovedMedicals
    {
        public int ReqId { get; set; }
        public int  EmpNo { get; set; }
        public string Company { get; set; }
        public string EmpName { get; set; }
        public string ApprovedMedicalNo { get; set; }
        public decimal TotalAmount { get; set; }
        public DateTime CreatedDate { get; set; }
        public DateTime? ApprovedDate { get; set; }
    }  

要获取数据并将其分配给此数据,我使用了 3 个数据库 table。 AppRequest 是主要的 Medical 通过 AppRequestMedicalExpences 的外键连接通过 Medical 连接。 所以一个请求可能在MedicalExpencestable中有1条或多条记录。所以我想从 table 中获取 AmountClaimed 的总和并将其分配给 Total Amount 视图模型 属性。我陷入了如何计算总和并将其分配给列部分的问题。你能帮我一下吗?

List < ApprovedMedicals > report = (from a in db.AppRequest 
                                    join m in db.Medical on a.Id equals m.Req_Id 
                                    join me in db.MedicalExpences on m.Id equals me.Medic_Id 
                                    join e in db.CreateEmployee on a.Create_By equals e.Id 
                                    join c in db.CreateCompany on e.CompanyId equals c.Id 
                                    where m.HodApproval == true && m.HodApprovedDate <= 
                                    ToDate && m.HodApprovedDate >= FromDate

                                    select new ApprovedMedicals {
                                    EmpNo = e.EmpNo,
                                    EmpName = e.EmpName,
                                    Company = c.CompanyName,
                                    ApprovedMedicalNo = m.ApproveNumber,
                                    ApprovedDate = m.HodApprovedDate,
                                    CreatedDate = a.Created_Date,
                                    ReqId = a.Id,
                                    TotalAmount = // Here I want to get the Total of me.Amount

                                    }).OrderByDescending(x => x.ReqId).ToList();

尝试:

List < ApprovedMedicals > report = (from a in db.AppRequest 
                                    join m in db.Medical on a.Id equals m.Req_Id 
                                    join me in db.MedicalExpences on m.Id equals me.Medic_Id 
                                    join e in db.CreateEmployee on a.Create_By equals e.Id 
                                    join c in db.CreateCompany on e.CompanyId equals c.Id 
                                    where m.HodApproval == true && m.HodApprovedDate <= 
                                    ToDate && m.HodApprovedDate >= FromDate

                                    select new ApprovedMedicals {
                                    EmpNo = e.EmpNo,
                                    EmpName = e.EmpName,
                                    Company = c.CompanyName,
                                    ApprovedMedicalNo = m.ApproveNumber,
                                    ApprovedDate = m.HodApprovedDate,
                                    CreatedDate = a.Created_Date,
                                    ReqId = a.Id,
                                    TotalAmount = 0

                                    }).OrderByDescending(x => x.ReqId).ToList();


foreach (var item in report)
{
    var Id = db.Medical.Where(x => x.Req_Id == item.ReqId).Select(x => x.Id).FirstOrDefault();
    item.TotalAmount = db.MedicalExpences.Where(x => x.Medic_Id == Id).Sum(x => x.Amount);
}