具有左外部联接的LINQ查询只需要最新记录

时间:2019-11-21 19:47:20

标签: mysql linq left-join

我已经看到此查询的几个答案,但没有一个对我有用。

我们有多个mySql表: 出货量 顾客 供应商 报复审计

在我的查询中,我想与他们的客户和供应商以及最近的rebillingaudit记录一起拉货。这就是我所拥有的:

        var results = context.Shipments.AsNoTracking().Where(x => customerIds.Contains(x.CustomerId.Value) && x.ProcessStageFlag == "REBILL_AUDIT")
            .Join(context.Customers.AsNoTracking(),
                im => im.CustomerId,
                cu => cu.CustomerId,
                (im, cu) => new { Shipments = im, Customers = cu }).DefaultIfEmpty()
            .Join(context.Suppliers.AsNoTracking(),
                im => im.Shipments.SupplierId,
                su => su.SupplierId,
                (im, su) => new { Shipments = im.Shipments, Customers = im.Customers, Suppliers = su }).DefaultIfEmpty()
            .GroupJoin(context.RebillingAudit.AsNoTracking().OrderByDescending(x => x.LastUpdatedOn), //.Take(1),
                im => new {im.Shipments.TrackingNumber, im.Shipments.CarrierInvoiceNumber},
                rn => new {rn.TrackingNumber, CarrierInvoiceNumber = rn.InvoiceNumber},
                (im, rn) => new { MatchingAssignments = im.MatchingAssignments, Shipments = im.Shipments, Suppliers = im.Suppliers, MatchResolutions = im.MatchResolutions, Customers = im.Customers, RebillingAudit = rn })
            .SelectMany(
                x => x.RebillingAudit.DefaultIfEmpty(),
                (x, y) => new { MatchingAssignments = x.MatchingAssignments, Shipments = x.Shipments, Suppliers = x.Suppliers, MatchingResolutions = x.MatchResolutions, Customers = x.Customers, RebillingAudit = y })
            .Where(x => x.MatchingAssignments.IsRebill &&
                        x.MatchingAssignments.IsActive)
            .Select(m => new CustomerRebills()
            {
                TrackingNumber = m.Shipments.TrackingNumber,
                Customer = m.Customers.InternalCustomerName,
                CarrierInvoice = m.Shipments.CarrierInvoiceNumber,
                RebillNotes = m.RebillingAudit == null ? "" : m.RebillingAudit.Notes
            }).ToList();

这是我尝试复制的SQL查询:

 FROM invoice_master im 
  JOIN customer c ON im.VPL_customer_ID = c.Customer_ID
  JOIN supplier s ON im.Supplier_ID = s.Supplier_ID
  LEFT OUTER JOIN rebilling_audit rn ON im.Tracking_Number = rn.tracking_number AND im.Invoice_Number = rn.Invoice_number AND rn.last_updated_on = 
    (SELECT MAX(last_updated_on) FROM rebilling_audit WHERE tracking_number = im.tracking_number AND Invoice_number = im.invoice_number)

我尝试添加.Take(1),但没有得到我期望的结果。 我尝试过.FirstOrDefault(),但出现错误。

谢谢, 萨默(Sammer)

2 个答案:

答案 0 :(得分:0)

使用我的SQL to LINQ Recipe,或多或少直接翻译SQL,我得到了

var results = (from im in context.InvoiceMaster
               join c in context.Customers on im.CustomerId equals c.CustomerId
               join s in context.Suppliers on im.Shipments.SupplierId equals s.SupplierId
               join rn in context.RebillingAudit on new { im.Shipments.TrackingNumber, im.Shipments.CarrierInvoiceNumber } equals new { rn.TrackingNumber, CarrierInvoiceNumber = rn.InvoiceNumber } into rnj
               from rn in rnj.Where(rn => rn.LastUpdatedOn == context.RebillingAudit.Where(ra => ra.TrackingNumber == im.Shipments.TrackingNumber && rn.InvoiceNumber == im.Shipments.CarrierInvoiceNumber).Max(rn => rn.LastUpdatedOn)).DefaultIfEmpty()
               select new CustomerRebills() {
                   TrackingNumber = im.Shipments.TrackingNumber,
                   Customer = c.InternalCustomerName,
                   CarrierInvoice = im.Shipments.CarrierInvoiceNumber,
                   RebillNotes = rn == null ? "" : rn.Notes
               }).ToList();

答案 1 :(得分:0)

这是我获取最新记录的方法:

    var results = context.Shipments.AsNoTracking().Where(x => customerIds.Contains(x.CustomerId.Value) && x.ProcessStageFlag == "REBILL_AUDIT")
        .Join(context.Customers.AsNoTracking(),
            im => im.CustomerId,
            cu => cu.CustomerId,
            (im, cu) => new { Shipments = im, Customers = cu }).DefaultIfEmpty()
        .Join(context.Suppliers.AsNoTracking(),
            im => im.Shipments.SupplierId,
            su => su.SupplierId,
            (im, su) => new { Shipments = im.Shipments, Customers = im.Customers, Suppliers = su }).DefaultIfEmpty()
        .GroupJoin(context.RebillingAudit.AsNoTracking().OrderByDescending(x => x.LastUpdatedOn), //.Take(1),
            im => new {im.Shipments.TrackingNumber, im.Shipments.CarrierInvoiceNumber},
            rn => new {rn.TrackingNumber, CarrierInvoiceNumber = rn.InvoiceNumber},
            (im, rn) => new { MatchingAssignments = im.MatchingAssignments, Shipments = im.Shipments, Suppliers = im.Suppliers, MatchResolutions = im.MatchResolutions, Customers = im.Customers, RebillingAudit = rn })
        .SelectMany(
            x => x.RebillingAudit.DefaultIfEmpty(),
            (x, y) => new { MatchingAssignments = x.MatchingAssignments, Shipments = x.Shipments, Suppliers = x.Suppliers, MatchingResolutions = x.MatchResolutions, Customers = x.Customers, RebillingAudit = y })
        .Where(x => x.MatchingAssignments.IsRebill &&
                    x.MatchingAssignments.IsActive)
        .Select(m => new CustomerRebills()
        {
            TrackingNumber = m.Shipments.TrackingNumber,
            Customer = m.Customers.InternalCustomerName,
            CarrierInvoice = m.Shipments.CarrierInvoiceNumber,
            RebillNotes = m.RebillingAudit == null ? "" : m.RebillingAudit.Notes
        }).ToList().OrderByDescending(x => x.RebillNotesWhen).GroupBy(y => new {y.TrackingNumber, y.CarrierInvoice, y.BillToAccount, y.CustomerId}).Select(z => z.FirstOrDefault()).ToList();

我所有的更改都在最后一行。

我添加了OrderByDescending以使最新记录排在最前面,我添加了GroupBy来收集每个单独的角色,然后我只使用FirstOrDefault来获得第一个角色。

这导致我每个TrackingNumber,CarrierInvoice,BillToAccount和CustomerId获得一条记录,并确保一条记录是最新的。

我希望这对某人有帮助。 萨默(Sammer)