从一个 table 中求和 "Amount" 并显示在另一个 Table 中
Sum "Amount" from one table and display in Another Table
我有两个 table 名为:
tblPatientBill,
tblPatientBillDetails
我想做的是在 tblPatientBill 中的 ID“66”,在 tblPatientBillDetail 中有两条记录。从第二个 table 我想添加金额值并在主要 table 中显示为使用 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 table 的金额列。
这是我的 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.Thanks
实现此目的
针对特定的 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();
}
}
我有两个 table 名为:
tblPatientBill,
tblPatientBillDetails
我想做的是在 tblPatientBill 中的 ID“66”,在 tblPatientBillDetail 中有两条记录。从第二个 table 我想添加金额值并在主要 table 中显示为使用 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 table 的金额列。
这是我的 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.Thanks
实现此目的针对特定的 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();
}
}