I want to know if it's possible in LINQ to achieve something along the lines of:
newList: { [1], [2] }
oldList: { [2], [3], [4], [5] }
resultantList = { [1], [2, 2], [3], [4], [5] }
Ok that was an oversimplification. Lets say:
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]}}
Notice that for the instance with Name="A" the list is actually the merged list of both lists. Order doesn't matter and in reality the equality is more complex.
I wish to achieve this on types (i.e. below works, but would be inefficient):
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);