[英]LINQ query with left outer join
I saw several of these examples and I think I am doing the syntax correctly, but I am getting an error that is not intuitive (to me anyway).我看到了其中几个例子,我认为我的语法是正确的,但是我得到了一个不直观的错误(无论如何对我来说)。
The customersIds
come in as a string that is a comma separated list and here is my code: customersIds
以逗号分隔列表的字符串形式出现,这是我的代码:
customerIds = "," + customerIds + ",";
var customerRebills = context.Shipments.AsNoTracking().Where(x => customerIds.IndexOf("," + x.CustomerId + ",") >= 0)
.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()
.Join(context.MatchingAssignments.AsNoTracking(),
im => new {im.Shipments.TrackingNumber, im.Shipments.CarrierInvoiceNumber},
ma => new { ma.TrackingNumber, ma.CarrierInvoiceNumber},
(im, ma) => new { Shipments = im.Shipments, Suppliers = im.Suppliers, Customers = im.Customers, MatchingAssignments = ma }).DefaultIfEmpty()
.Join(context.MatchResolutions.AsNoTracking(),
ma => ma.MatchingAssignments.MatchReasonId,
mr => mr.MatchResolutionId,
(ma, mr) => new { MatchingAssignments = ma.MatchingAssignments, Shipments = ma.Shipments, Suppliers = ma.Suppliers, Customers = ma.Customers, MatchResolutions = mr }).DefaultIfEmpty()
.GroupJoin(context.RebillingNotes.AsNoTracking(),
im => new {im.Shipments.TrackingNumber, im.Shipments.CarrierInvoiceNumber},
rn => new {rn.TrackingNumber, rn.InvoiceNumber},
(im, rn) => new { MatchingAssignments = im.MatchingAssignments, Shipments = im.Shipments, Suppliers = im.Suppliers, MatchResolutions = im.MatchResolutions, Customers = im.Customers, RebillingNotes = rn })
.SelectMany(
x => x.RebillingNotes.DefaultIfEmpty(),
(x, y) => new {MatchingAssignments = x.MatchingAssignments, Shipments = x.Shipments, Suppliers = x.Suppliers, MatchingResolutions = x.MatchResolutions, Customers = x.Customers, RebillingNotes = y})
This code works fine when the last join is on one field:当最后一个连接在一个字段上时,此代码可以正常工作:
.GroupJoin(context.RebillingNotes.AsNoTracking(),
im => im.Shipments.TrackingNumber,
rn => rn.TrackingNumber,
(im, rn) => new { MatchingAssignments = im.MatchingAssignments, Shipments = im.Shipments, Suppliers = im.Suppliers, MatchResolutions = im.MatchResolutions, Customers = im.Customers, RebillingNotes = rn })
.SelectMany(
x => x.RebillingNotes.DefaultIfEmpty(),
(x, y) => new {MatchingAssignments = x.MatchingAssignments, Shipments = x.Shipments, Suppliers = x.Suppliers, MatchingResolutions = x.MatchResolutions, Customers = x.Customers, RebillingNotes = y})
I thought it was a matter of creating objects containing the multiple fields.我认为这是创建包含多个字段的对象的问题。
The error I am receiving is:我收到的错误是:
CS0411 The type arguments for method 'Queryable.GroupJoin(IQueryable, IEnumerable, Expression>, Expression>, Expression, TResult>>)' cannot be inferred from the usage. CS0411 无法从用法中推断方法“Queryable.GroupJoin(IQueryable, IEnumerable, Expression>, Expression>, Expression, TResult>>)”的类型 arguments。 Try specifying the type arguments explicitly.尝试明确指定类型 arguments。
Thank you谢谢
I figured out the issue, it was the field names in the joins.我发现了问题,它是连接中的字段名称。
This was causing the issue:这导致了这个问题:
im => new {im.Shipments.TrackingNumber, im.Shipments.CarrierInvoiceNumber},
rn => new {rn.TrackingNumber, rn.InvoiceNumber},
This corrected the issue:这更正了问题:
im => new {im.Shipments.TrackingNumber, im.Shipments.CarrierInvoiceNumber},
rn => new {rn.TrackingNumber, CarrierInvoiceNumber = rn.InvoiceNumber},
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.