Concatenate multiple collections together but keep their order using DynamicData package

32 Views Asked by At

I want to concatenate multiple collections (SourceLists) together while retaining element order of each collection.

Initially this works fine, but if I add an item to any list the final collection will have the item at the end, this is undesired. How can I make the final list appear to simply be a sequence of items of the 3 lists appended to eachother?

Code example:

var list1 = new SourceList<string>();
list1.AddRange(new[] { "list1 value 1", "list1 value 2", "list1 value 3" });
var list2 = new SourceList<string>();
list2.AddRange(new[] { "list2 value 1", "list2 value 2", "list2 value 3" });
var list3 = new SourceList<string>();
list3.AddRange(new[] { "list3 value 1", "list3 value 2", "list3 value 3" });
var combineOperation = list1.Connect().Or(list2.Connect()).Or(list3.Connect())
    .Bind(out var combineResult)
    .Subscribe();
Debug.WriteLine(string.Join("\n", combineResult));
list1.Add("list1 value 4");
Debug.WriteLine("");
Debug.WriteLine(string.Join("\n", combineResult));
combineOperation.Dispose();

Giving the output:

list1 value 1
list1 value 2
list1 value 3
list2 value 1
list2 value 2
list2 value 3
list3 value 1
list3 value 2
list3 value 3

list1 value 1
list1 value 2
list1 value 3
list2 value 1
list2 value 2
list2 value 3
list3 value 1
list3 value 2
list3 value 3
list1 value 4 <<<< issue here

But I need my output to instead be:

list1 value 1
list1 value 2
list1 value 3
list2 value 1
list2 value 2
list2 value 3
list3 value 1
list3 value 2
list3 value 3

list1 value 1
list1 value 2
list1 value 3
list1 value 4 <<<< new entry here
list2 value 1
list2 value 2
list2 value 3
list3 value 1
list3 value 2
list3 value 3

The underlying reason I need this is because I'm sorting the underlying lists and I lose the data required to sort them after combining.

0

There are 0 best solutions below