Skip to content

Replace Select and Merge SelectMany

oriches edited this page Mar 24, 2013 · 15 revisions

[Back to Code Analysis & Inspection] (https://github.com/oriches/Resharper.ReactivePlugin/wiki/Code-Analysis-&-Inspection)

Adds a hint highlight to Rx method calls Select & Merge which can be replaced by a single call to SelectMany- it is short hand for calling Select then followed by Merge.

source.Select(selector).Merge();

can be replaced by

source.SelectMany(selector);

So imagine we have to following method with a chain of Rx methods containing a call to Select followed by Merge:

public class ExampleClass
{
    public IObservable<int> Method()
    {
        return new List<int> {1, 2, 3}
            .ToObservable(Scheduler.Immediate)
            .Timeout(TimeSpan.FromSeconds(10), Scheduler.Immediate)
            .Select(GenerateNumbers)
            .Merge();
    }
}

With the plugin installed this would be annotated with a Resharper hint as follows:

![Replace Select & Merge with SelectMany highlght] (http://oi49.tinypic.com/2iqzdoh.jpg)

To resolve the highlight you simply replace the calls to Select & Merge with SelectMany:

Fixed Select & Merge with SelectMany

There is a Resharper 'quick fix' associated with this [highlight] (https://github.com/oriches/Resharper.ReactivePlugin/wiki/Replace-Select-And-Merge-With-SelectMany).

Clone this wiki locally