一个表中的“金额”总和,然后显示在另一个表中

时间:2019-08-08 06:52:26

标签: linq asp.net-mvc-4 linq-to-sql entity-framework-6

我有两个名为:

的表

tblPatientBill, enter image description here

tblPatientBillDetails

enter image description here

我想做的是在ID为“ 66”的tblPatientBill中,在tblPatientBillDetail中有两条记录。从第二个表中,我想添加金额值并使用linq作为总和值显示在主表中,为此我要这样做:

    public ActionResult Edit(PatientViewModel model)
            {
               List<tblPatientBillDetail> lst = new List<tblPatientBillDetail>();
               tblPatientBill tmp = new tblPatientBill();

               tmp = db.tblPatientBill.Where(x=>x.ID == PatientBillID && x.is_active == true).ToList();


                        lst = db.tblPatientBillDetails.Where(x=>x.PatientBillID == PatientBillID && x.is_active == true).ToList();

                        foreach (var test in lst)
                        {

                            model.Amount += lst.Amount;
                        }
                        Session["templist"] = lst;
}

,然后将此会话值提供给tblPatientBill表的amount列。

这是我的PatientViewModel:

public class PatientViewModel
    {
        public int ID { get; set; }
        public Nullable<int> PatientBillID { get; set; }
        public Nullable<double> Amount { get; set; }
        public Nullable<System.DateTime> CreatedAt { get; set; }
        public string CreatedBy { get; set; }
        public string Description { get; set; }
        public Nullable<bool> is_active { get; set; }
    }

tblPatientBillDetail:

   public partial class tblPatientBillDetail
    {
        public int ID { get; set; }
        public Nullable<int> PatientBillID { get; set; }
        public Nullable<double> Amount { get; set; }
        public Nullable<System.DateTime> CreatedAt { get; set; }
        public string CreatedBy { get; set; }
        public string Description { get; set; }
        public Nullable<bool> is_active { get; set; }

        public virtual tblPatientBill tblPatientBill { get; set; }
    }

tblPatientBill:

  public partial class tblPatientBill
    {
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
        public tblPatientBill()
        {
            this.tblPatientBillDetails = new HashSet<tblPatientBillDetail>();
        }

        public int ID { get; set; }
        public Nullable<int> PatientAppointmentID { get; set; }
        public string BillNo { get; set; }
        public Nullable<double> Amount { get; set; }
        public Nullable<double> Discount { get; set; }
        public Nullable<System.DateTime> CreatedAt { get; set; }
        public string CreatedBy { get; set; }
        public string Description { get; set; }
        public Nullable<bool> is_active { get; set; }

        public virtual tblPatientAppointment tblPatientAppointment { get; set; }
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
        public virtual ICollection<tblPatientBillDetail> tblPatientBillDetails { get; set; }
    }

如何使用linq来实现这一目标。谢谢

1 个答案:

答案 0 :(得分:1)

针对特定的PatientBill

var tblPatientBill = db.tblPatientBill.FirstOrDefault(x=>x.ID == PatientBillID && x.is_active == true);

if (tblPatientBill != null) {
    var lst = db.tblPatientBillDetails
          .Where(x=>x.ID == PatientBillID && x.is_active == true)
          .ToList();


    tblPatientBill.Amount = lst.Select(c => c.Amount).Sum();
}

针对所有患者账单

var tblPatientBills = db.tblPatientBill.Where(x=> x.is_active == true).ToList();

if (tblPatientBills.Count != 0) {
    var lst = db.tblPatientBillDetails
          .Where(x=>x.PatientBillID == PatientBillID && x.is_active == true)
          .ToList();

    foreach(var patientBill in tblPatientBills)
    {
        patientBill.Amount = lst.Where(b => b.PatientBillId == PatientBillID).Select(c => c.Amount).Sum();
    }
}