I'm trying to merge two nodes. Basically, the idea is to take all children in left and new ones in right. Is there any way to get rid of toAdd
variable and do it in a clean way?
I don't want to convert any IEnumerable<T>
to List
or Array
in this merge process.
public IEnumerable<TChild> NodeMerge<TNode, TChild, TKey>(TNode left, TNode right,
Func<TNode, IEnumerable<TChild>> getChildren, Func<TChild, TKey> getKey)
{
var lChildren = getChildren(left);
var rChildren = getChildren(right);
IEnumerable<TChild>[] toAdd = new IEnumerable<TChild>[2];
// Common Keys between left and right
toAdd[0] = lChildren.Where(s => rChildren.Select(p => getKey(p)).Contains(getKey(s)));
// new keys added to right
toAdd[1] = rChildren.Where(s => lChildren.Select(p => getKey(p)).Contains(getKey(s)) == false);
return toAdd.SelectMany(s => s);
}