为什么这个 Linq 左外连接不能按预期工作?

发布于 2024-11-27 19:25:37 字数 3899 浏览 0 评论 0原文

我有两个列表,都使用相同的类(部分)。第一个列表是“主”列表 (engineerParts),第二个列表 (partDefaults) 是这些部件的属性值列表。

第一个列表包含 263 个项目,第二个列表包含 238 个项目。因此,最终列表应该包含 263 个项目。然而事实并非如此,它包含 598 个!

var partsWithDefaults = from a in engineerParts
                                    join b in partDefaults on
                                    new { a.PartNumber, a.Revision }
                                    equals
                                    new { b.PartNumber, b.Revision } into j1
                                    from j2 in j1.DefaultIfEmpty()
                                    select new Part()
                                    {
                                        NewPart = isNewPart(a.PartNumber, PadRevision(a.Revision), concatenateRevision),
                                        PartNumber = concatenateRevision == true ? string.Concat(a.PartNumber, PadRevision(a.Revision)) : a.PartNumber,
                                        Revision = concatenateRevision == true ? "" : a.Revision,
                                        ParentPart = a.ParentPart,
                                        Description = a.Description,
                                        BoMQty = a.BoMQty,
                                        UoM = (j2 != null) ? j2.UoM : null,
                                        ClassId = (j2 != null) ? j2.ClassId : null,
                                        ShortDescription = (j2 != null) ? j2.ShortDescription : null,
                                        ABCCode = (j2 != null) ? j2.ABCCode : null,
                                        BuyerId = (j2 != null) ? j2.BuyerId : null,
                                        PlannerId = (j2 != null) ? j2.PlannerId : null,
                                        OrderPolicy = (j2 != null) ? j2.OrderPolicy : null,
                                        FixedOrderQty = (j2 != null) ? j2.FixedOrderQty : null,
                                        OrderPointQty = (j2 != null) ? j2.OrderPointQty : null,
                                        OrderUpToLevel = (j2 != null) ? j2.OrderUpToLevel : null,
                                        OrderQtyMin = (j2 != null) ? j2.OrderQtyMin : null,
                                        OrderQtyMax = (j2 != null) ? j2.OrderQtyMax : null,
                                        OrderQtyMultiple = (j2 != null) ? j2.OrderQtyMultiple : null,
                                        ReplenishmentMethod = (j2 != null) ? j2.ReplenishmentMethod : null,
                                        ItemShrinkageFactor = (j2 != null) ? j2.ItemShrinkageFactor : null,
                                        PurchasingLeadTime = (j2 != null) ? j2.PurchasingLeadTime : null,
                                        MFGFixedLeadTime = (j2 != null) ? j2.MFGFixedLeadTime : null,
                                        PlanningTimeFence = (j2 != null) ? j2.PlanningTimeFence : null,
                                        FulfillMethod = (j2 != null) ? j2.FulfillMethod : null,
                                        ItemStatus = (j2 != null) ? j2.ItemStatus : null,
                                        TreatAsEither = (j2 != null) ? j2.TreatAsEither : false,
                                        AltItem1 = (j2 != null) ? j2.AltItem1 : null,
                                        AltItem2 = (j2 != null) ? j2.AltItem2 : null,
                                        AltItem3 = (j2 != null) ? j2.AltItem3 : null,
                                        AltItem4 = (j2 != null) ? j2.AltItem4 : null,
                                        AltItem5 = (j2 != null) ? j2.AltItem5 : null,
                                        DesignAuthority = (j2 != null) ? j2.DesignAuthority : null,
                                        Level = a.Level
                                    };

            return partsWithDefaults.ToList<Part>();

I have two lists, both using the same class (Part). The first list is the "master" list (engineerParts), the second (partDefaults) is the list of attribute values for those parts.

The first list contains 263 items, the second, 238. So, the final list should contain 263. However it doesn't, it contains 598!!

var partsWithDefaults = from a in engineerParts
                                    join b in partDefaults on
                                    new { a.PartNumber, a.Revision }
                                    equals
                                    new { b.PartNumber, b.Revision } into j1
                                    from j2 in j1.DefaultIfEmpty()
                                    select new Part()
                                    {
                                        NewPart = isNewPart(a.PartNumber, PadRevision(a.Revision), concatenateRevision),
                                        PartNumber = concatenateRevision == true ? string.Concat(a.PartNumber, PadRevision(a.Revision)) : a.PartNumber,
                                        Revision = concatenateRevision == true ? "" : a.Revision,
                                        ParentPart = a.ParentPart,
                                        Description = a.Description,
                                        BoMQty = a.BoMQty,
                                        UoM = (j2 != null) ? j2.UoM : null,
                                        ClassId = (j2 != null) ? j2.ClassId : null,
                                        ShortDescription = (j2 != null) ? j2.ShortDescription : null,
                                        ABCCode = (j2 != null) ? j2.ABCCode : null,
                                        BuyerId = (j2 != null) ? j2.BuyerId : null,
                                        PlannerId = (j2 != null) ? j2.PlannerId : null,
                                        OrderPolicy = (j2 != null) ? j2.OrderPolicy : null,
                                        FixedOrderQty = (j2 != null) ? j2.FixedOrderQty : null,
                                        OrderPointQty = (j2 != null) ? j2.OrderPointQty : null,
                                        OrderUpToLevel = (j2 != null) ? j2.OrderUpToLevel : null,
                                        OrderQtyMin = (j2 != null) ? j2.OrderQtyMin : null,
                                        OrderQtyMax = (j2 != null) ? j2.OrderQtyMax : null,
                                        OrderQtyMultiple = (j2 != null) ? j2.OrderQtyMultiple : null,
                                        ReplenishmentMethod = (j2 != null) ? j2.ReplenishmentMethod : null,
                                        ItemShrinkageFactor = (j2 != null) ? j2.ItemShrinkageFactor : null,
                                        PurchasingLeadTime = (j2 != null) ? j2.PurchasingLeadTime : null,
                                        MFGFixedLeadTime = (j2 != null) ? j2.MFGFixedLeadTime : null,
                                        PlanningTimeFence = (j2 != null) ? j2.PlanningTimeFence : null,
                                        FulfillMethod = (j2 != null) ? j2.FulfillMethod : null,
                                        ItemStatus = (j2 != null) ? j2.ItemStatus : null,
                                        TreatAsEither = (j2 != null) ? j2.TreatAsEither : false,
                                        AltItem1 = (j2 != null) ? j2.AltItem1 : null,
                                        AltItem2 = (j2 != null) ? j2.AltItem2 : null,
                                        AltItem3 = (j2 != null) ? j2.AltItem3 : null,
                                        AltItem4 = (j2 != null) ? j2.AltItem4 : null,
                                        AltItem5 = (j2 != null) ? j2.AltItem5 : null,
                                        DesignAuthority = (j2 != null) ? j2.DesignAuthority : null,
                                        Level = a.Level
                                    };

            return partsWithDefaults.ToList<Part>();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜未央樱花落 2024-12-04 19:25:37

原来是数据问题。向我发送第二个列表的 Excel 数据的人在其中留下了重复项!!!!

Turns out it was a data issue. The person that had sent me the data in Excel for the second list had left duplicates in it!!!!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文