private static void TestGroupBy()
{
var rxList = new ReactiveList<int>();
Observable.Merge(rxList.ItemsAdded, rxList.ItemChanged.Select(x => x.Sender) ).GroupBy(i => i%3)
.Subscribe(group =>
{
Console.WriteLine(string.Format("--> Group {0} is created", group.Key) );
int child = 0;
group.Subscribe(c =>
{
Console.WriteLine(string.Format(" ------> Adding child {0} to Group {1} - ChildCnt: {2}", c, group.Key, ++child) );
});
});
Console.WriteLine("Add 1 to 10... ");
rxList.AddRange(new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 });
Console.WriteLine("Change item 0 to 11, expect to be moved from group 0, to Group 2 ");
rxList[0] = 11;
Console.WriteLine("Remove item at 0, expect the item to be removed from the group ...");
rxList.RemoveAt(0);
}
我这里有这段代码。 我希望在项目被删除或项目被新项目替换时触发重新组合。 我如何实现这一点?
我不确定是否可以像您想要的那样使用groupBy
来实现项目删除。看看这张图片:似乎您想更改已推送到"分组"流之一的元素。
在您的情况下,我将简单地创建几个派生集合(与您拥有的组一样多(,具有不同的选择器:
public void Test()
{
var source = new ReactiveList<int>();
var derived1 = source.CreateDerivedCollection(x => x, filter: x => x%3 == 0);
var derived2 = source.CreateDerivedCollection(x => x, filter: x => x%3 == 1);
var derived3 = source.CreateDerivedCollection(x => x, filter: x => x%3 == 2);
}