1

Using ReactiveUI 6.5, I'm trying achieve something like a SelectMany LINQ expression with the CreateDerivedCollection feature of RxUI. The objects in my source collection (type 'A') have an IsSelected property, as well as another collection property where the items are of type 'B'. I want to end up with an IReactiveDerivedList<B> which is a flattened list of all of the B objects from the A's that are selected. Hopefully that makes sense.

To make it a little more concrete, let's use an example of an app for browsing log files. Say we have a LogFileViewModel type and our main screen's view model has a list of these. Each instance in the list represents a log file on the system, and we present a list of these that the user can select. It's a multi-select list so they can select more than one at a time.

The LogFileViewModel then has an IsSelected boolean property, which will be set to true/false as the user selects or deselects the corresponding item in the list. And it has a property which is a List<LogEntry>. Each LogEntry object of course represents one entry in the corresponding log file.

What I want to do then is have a reactive list in the main view model which is a list of all of the LogEntry objects for all of the currently selected LogFileViewModel objects. The ReactiveList of selected log files is easy, but I'm stuck on the second part. Here's what the main view model would basically look like:

public class MainViewModel
{
    public MainViewModel()
    {
        //This gets initialized with the log files somehow, doesn't matter
        LogFiles = new List<LogFileViewModel(...);

        SelectedLogFiles = LogFiles.CreateDerivedCollection(l => l, l => l.IsSelected);
        SelectedLogFileEntries = ? //How to create this one?
    }

    public List<LogFileViewModel> LogFiles { get; private set; }
    public IReactiveDerivedList<LogFileViewModel> SelectedLogFiles { get; private set; }
    public IReactiveDerivedList<LogEntry> SelectedLogFileEntries { get; private set; }
}

Is there a known way to do this that I'm just not seeing? If not, any clever ideas to achieve this behavior? :-)

Edit : Looks like I missed this question in my initial search. Paul provided the "clever" solution to this problem about 2 years ago. So my question now ... is this still the best way to achieve this behavior?

Community
  • 1
  • 1
Kevin Kuebler
  • 384
  • 1
  • 13
  • You can try BLinq or CLinq instead, as they do implement `.SelectMany` – Aron Aug 19 '15 at 16:47
  • or even maybe OLinq. – Aron Aug 19 '15 at 16:56
  • @Aron. Thanks, those projects look interesting. Had never seen those before. For now though, I'd like to stay in the realm of Rx and Rx-UI, especially since there seems to be a way to do it (see link I posted in my edit). I'm just wondering now if there's a simpler/cleaner way to do it in the latest version of Rx-UI. – Kevin Kuebler Aug 19 '15 at 17:27

0 Answers0