我有一个T-SQL查询,它显示来自表1的名为p1的Sum(TotalAmount)Sum(Discount)Sum(PayableAmount)和来自表2的名为p2的Sum(PaidAmount),
表p1和p2与Forenkay和主键没有任何关系,
所以我不能使用.net核心方法语法_context.Table1.include(a=〉a.table2)来连接两个表,因为这两个表之间没有关系,
因此,我已经将下面的代码编写为T-SQL查询,并获得了我想要的确切结果,但现在我想将相同的T-SQL查询编写为LING查询,并在我的C#.net核心控制器代码中将其用作查询语法或方法语法,
下面是我的T-SQL代码,它显示来自名为p1的表1的Sum(TotalAmount)Sum(Discount)Sum(PayableAmount),以及来自名为p2的表2的Sum(PaidAmount),
SELECT p1.PatientId,
SUM(p1.TotalAmount) as TotalTreatmentAmount,
SUM(p1.Discount) As TotalTreatmentDiscount,
SUM(p1.Payable) as TotalTreatmentPayable,
SUM(p1.Balance) as TotalTreatmentBalancce,
p2.TotalTreatmentPaidAmount
FROM PatientPayments as p1
join (SELECT p2.PatientId, SUM(p2.PaidAmount) as TotalTreatmentPaidAmount
FROM PatientPaymentHistories p2
where p2.PaymentType != 'Refund'
group by p2.PatientId) as p2
ON p2.PatientId = p1.PatientId
group by p1.PatientId, p2.TotalTreatmentPaidAmount
这是我的C#代码,但这段代码不工作,它的错误,只有SQL代码是工作正常
public IActionResult ExportPatientPaymentDataToExcel()
{
var PatientPayments =
from p1 in _context.PatientPayments
join p2 in _context.PatientPaymentHistories on p1.PatientId equals p2.PatientId
select new
{
p1.PatientId,
p1.TotalAmount,
p1.Discount,
p1.Payable,
p2.PaidAmount
};
DataTable PatientPaymentTable = new DataTable("PatientPayment");
PatientPaymentTable.Columns.AddRange(new DataColumn[5]
{
new DataColumn("Patient ID"),
new DataColumn("Total Amount"),
new DataColumn("Total Discount"),
new DataColumn("Total Payable"),
new DataColumn("Total Paid"),
});
foreach (var PatientPayment in PatientPayments)
{
PatientPaymentTable.Rows.Add(
PatientPayment.PatientId,
PatientPayment.TotalAmount,
PatientPayment.Discount,
PatientPayment.Payable,
PatientPayment.PaidAmount
);
}
using (XLWorkbook wb = new XLWorkbook())
{
wb.Worksheets.Add(PatientPaymentTable);
using (MemoryStream stream = new MemoryStream())
{
wb.SaveAs(stream);
return File(stream.ToArray(), "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", GetLocalDateTime().Date.ToShortDateString() + " " + "PatientPayment_Data.xlsx");
}
}
}
请帮我把上面的T-SQL代码改成C#玲查询
2条答案
按热度按时间68bkxrlz1#
这应该会如预期般运作:
lsmepo6l2#
根据您的T-SQL,尝试如下操作