SQL Server 如何将现有的T-SQL代码转换为C# Ling查询

c3frrgcw  于 2022-11-28  发布在  C#
关注(0)|答案(2)|浏览(201)

我有一个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#玲查询

68bkxrlz

68bkxrlz1#

这应该会如预期般运作:

var query = _context.PatientPayments.Join(_context.PatientPaymentHistories
            .Where(x => x.Type != "Refund")
            .GroupBy(x => x.PatientId)
            .Select(x => new
            {
                PatientId = x.Key,
                TotalTreatmentPaidAmount = x.Sum(y => y.PaidAmount)
            }),
        x => x.PatientId,
        y => y.PatientId,
        (x, y) => new
        {
            PatientId = x.PatientId,
            TotalAmount = x.TotalAmount,
            Discount = x.Discount,
            Payable = x.Payable,
            Balance = x.Balance,
            TotalTreatmentPaidAmount = y.TotalTreatmentPaidAmount
        })
    .GroupBy(x => new { PatientId = x.PatientId, 
        TotalTreatmentPaidAmount = x.TotalTreatmentPaidAmount })
    .Select(x => new
    {
        PatientId = x.Key.PatientId,
        TotalTreatmentAmount = x.Sum(y => y.TotalAmount),
        TotalTreatmentDiscount = x.Sum(y => y.Discount),
        TotalTreatmentPayable = x.Sum(y => y.Payable),
        TotalTreatmentBalancce = x.Sum(y => y.Balance),
        TotalTreatmentPaidAmount = x.Key.TotalTreatmentPaidAmount
    });
lsmepo6l

lsmepo6l2#

根据您的T-SQL,尝试如下操作

var query = _context.PatientPayments
            .Join(_context.PatientPaymentHistories
                          .Where(s => s.PaymentType != 'Refund')
                          .GroupBy(p => p.PatientId)
                          .Select(g => select new {
                              PatientId = g.Key,
                              TotalTreatmentPaidAmount = g.Sum(x => x.PaidAmount)
                         }), 
                         p1 => p1.PatientId, 
                         p2 => p2.PatientId, 
                         (p1, p2) => new { PatientPayments = p1, PatientPaymentHistories = p2 })
            .GroupBy(s => new { s.PatientPayments.PatientId, s.PatientsPaymentHistories.TotalTreatmentPaidAmount })
            .Select(gs => new 
            {
                gs.Key.PatientId, 
                TotalTreatmentAmount = gs.Sum(x => x.PatientPayments.TotalAmount),
                TotalTreatmentDiscount = gs.Sum(x => x.PatientPayments.Discount),
                TotalTreatmentPayable = gs.Sum(x => x.PatientPayments.Payable),
                TotalTreatmentBalancce = gs.Sum(x => x.PatientPayments.Balance),
                gs.Key.TotalTreatmentPaidAmount
            };

相关问题