Paboka
Paboka

Reputation: 442

How to distinct a DerivedCollection's content in ReactiveUI

I use ReactiveUI and have a ReactiveList instance. For example, it contains items:

{ 1, 2, 3, 4, 1, 2, 5 }

How can I create a distincted DerivedCollection, containing items like:

{ 1, 2, 3, 4, 5 }

?

Upvotes: 0

Views: 225

Answers (1)

Jon G Stødle
Jon G Stødle

Reputation: 3904

This is not the most performant code but I got this working in a simple Console app

var list = new ReactiveList<int>();
IReactiveDerivedList<int> dlist = null;
dlist = list.CreateDerivedCollection(x => x, x =>
{
    var contains = dlist?.Contains(x) ?? false;
    return !contains;
});
list.AddRange(new[] { 1, 2, 3, 4, 5, 6, 5, 4, 3, 2, 1 });
Console.WriteLine(string.Join(", ", dlist.Select(x => x.ToString())));
Console.ReadKey();

// Outputs: 1, 2, 3, 4, 5, 6

This basically filter the collection based on whether the new element is already there.

Upvotes: 1

Related Questions