我已经看到了这个问题的几个答案,但没有一个对我有用。
我们有多个 mySql 表: 出货量 顾客 供应商 重新计费审核
在我的查询中,我想提取他们的客户和供应商的发货情况以及最新的重新计费审核记录。这就是我所拥有的:
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() 但收到错误。
谢谢, 萨默尔
最佳答案
使用我的 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();
关于mysql - 带有左外连接的 LINQ 查询只需要最近的记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58982695/