我想知道在 LINQ 中是否有可能实现以下目标:
newList: { [1], [2] }
oldList: { [2], [3], [4], [5] }
resultantList = { [1], [2, 2], [3], [4], [5] }
好吧,那是过于简单化了。让我们说:
Class A
{
public string Name;
public IList<B> Items;
public bool Equals(A obj)
{
return obj.Name == Name;
}
}
newList<A> = {{ Name = "A", Items[1]}, { Name = "B", Items[1] }}
oldList<A> = {{ Name = "C", Items[2]}, { Name = "A", Items[2] }, { Name = "D", Items[1] }, { Name = "E", Items[1] },}
mergedList<A> = {{ Name = "A", Items[3]}, { Name = "B", Items[1]}, { Name = "C", Items[2]}, { Name = "D" , Items[1]}, { Name = "E" , Items[1]}}
请注意,对于 Name="A" 的实例,该列表实际上是两个列表的合并列表。顺序无关紧要,实际上平等更复杂。
我希望在类型上实现这一点(即以下工作,但效率低下):
var fragments = newGeometryFragments.Except(oldGeometryFragments).ToList();
fragments.AddRange(oldGeometryFragments.Except(newGeometryFragments).ToArray());
var mergedFragments = (from newGeometry in newGeometryFragments
from oldGeometry in oldGeometryFragments
where newGeometry.Equals(oldGeometry)
select MergeGeometryFragments(newGeometry, oldGeometry)).ToArray();
fragments.AddRange(mergedFragments);