带有左外部联接的LINQ查询

时间:2019-11-14 15:20:12

标签: c# linq left-join

我看到了几个这样的示例,我认为我在正确地执行语法,但是我遇到了一个不直观的错误(无论如何对我而言)。

type Unified<T extends Rec> = { [K in OptionalPropertyNames<T>]?: T[K] extends Weak<infer I> ? I : never } & { [K in RequiredPropertyNames<T>]: T[K] extends Strong<infer I> ? I : never; } 是以字符串形式出现的,这是一个用逗号分隔的列表,这是我的代码:

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})

我认为这是创建包含多个字段的对象的问题。

我收到的错误是:

  

CS0411方法'Queryable.GroupJoin(IQueryable,IEnumerable,   表达式>,表达式>,   表达式,TResult >>)'不能为   从用法推断。尝试显式指定类型参数。

谢谢

1 个答案:

答案 0 :(得分:0)

我发现了问题所在,这是联接中的字段名称。

这是导致问题的原因:

im => new {im.Shipments.TrackingNumber, im.Shipments.CarrierInvoiceNumber},
rn => new {rn.TrackingNumber, rn.InvoiceNumber},

此问题已解决:

im => new {im.Shipments.TrackingNumber, im.Shipments.CarrierInvoiceNumber},
rn => new {rn.TrackingNumber, CarrierInvoiceNumber = rn.InvoiceNumber},