【发布时间】:2011-08-03 10:28:14
【问题描述】:
我有两个列表,都使用相同的类(部分)。第一个列表是“主”列表 (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>();
【问题讨论】:
-
您为比较所做的匿名类型将不会 Equal 或 ReferenceEqual 彼此。我希望一目了然,你会得到 263 个结果。使用 && 加入多个列,而不是如图所示 here
-
由于 C# 中的 Join 不支持 &&,我想您的意思是删除连接并使用 WHERE 子句?虽然它肯定是在做某种 263+238 = 501 的连接,但我得到了 598 个项目!
-
另外,我需要进行左连接,以确保包含engineerParts 列表中的所有项目,即使partDefaults 列表中没有匹配的部分也是如此。 WHERE 子句将有效地进行连接。
-
公平点。要使您的匿名类型正确匹配,请为字段命名。
{col1 = a.PartNumber, col2 = a.Revision}。这 (IIRC) 将使编译器为两种匿名类型使用相同的 Type。如果之后仍然有问题,请将结果更改为匿名类并将结果类型中的字段数减少到 1,看看问题是否仍然存在。如果没有,请添加第二个字段,然后添加第三个,等等... -
我已经尝试了上面的两个建议,并且都产生了与以前相同的结果,598 项:(