Using LINQ to perform queries

Rx allow developers to use the IObservable interface that represents synchronous data streams to write queries using LINQ. To recap, Rx can be thought of as consisting of three sections:

  • Observables: The interface that brings together and represents all these data streams
  • Language-Integrated Query (LINQ): The ability to use LINQ to query these multiple data streams
  • Schedulers: Parametrizing concurrency using schedulers

In this recipe, we will be looking at the LINQ functionality of Rx in more detail.

Getting ready

As observables are just data streams, we can use LINQ to query them. In the following recipe, we will output text to the screen based on a LINQ query.

How to do it…

  1. Start by adding a new Windows Forms project to your solution:
    How to do it…
  2. Call the project winformRx and click on the OK button:
    How to do it…
  3. In Toolbox, search for the TextBox control and add it to your form:
    How to do it…
  4. Finally, add a label control to your form:
    How to do it…
  5. Right-click on your winformRx project and select Manage NuGet Packages… from the context menu:
    How to do it…
  6. In the search text box, enter System.Reactive to search for the NuGet package and click on the Install button:
    How to do it…
  7. Visual Studio will ask you to review the changes it's about to make to your project. Click on the OK button:
    How to do it…
  8. Before the installation starts, you might need to accept the license agreement by clicking on the I Accept button:
  9. After the installation completes, you should see the newly added references to your winformRx project if you expand the References for the project:
    How to do it…
  10. Finally, right-click on the project and set winformRx as your startup project by clicking on Set as StartUp Project from the context menu:
    How to do it…
  11. Create the form load event handler for the form by double-clicking anywhere on the Windows Form. To this form, add the Observable keyword. You will notice that the keyword is immediately underlined. This is because you are missing the reference to the LINQ assembly of System.Reactive:
    How to do it…
  12. To add this, press Ctrl + . (period) to bring up the possible suggestions to fix the issue. Select to add the using System.Reactive.Linq namespace to your project:
    How to do it…
  13. Continue adding the following code to your form load event. Basically, you are using LINQ and telling the compiler that you want to select the text from the event pattern that matches the text changed event of the text box on the form called textBox1. After you have done that, add a subscription to the variable and tell it to output whatever it finds in the text to the label on the form called label1:
    private void Form1_Load(object sender, EventArgs e)
    {
        var searchTerm = Observable.FromEventPattern<EventArgs>(textBox1, "TextChanged")
        .Select(x => ((TextBox)x.Sender).Text);
    
        searchTerm.Subscribe(trm => label1.Text = trm);
    }

    Note

    When we added the text box and label to our form, we left the control names as default. If, however, you changed the default names, you would need to specify those names instead of textBox1 and label1 for the controls on the form.

  14. Click on the run button to run your application. The Windows Form will be displayed with the text box and label on it:
    How to do it…
  15. Notice that the text is output to the label on the form as you type:
    How to do it…
  16. Let's jazz things up a bit by adding a Where condition to the LINQ statement. We will specify that the text string must only select the text when it ends with a period. This means that the text will only be displayed in the label after each full sentence. As you can see, we aren't doing anything special here. We are merely using standard LINQ to query our data stream and return the results to our searchTerm variable:
    private void Form1_Load(object sender, EventArgs e)
    {
        var searchTerm = Observable.FromEventPattern<EventArgs>(textBox1, "TextChanged")
        .Select(x => ((TextBox)x.Sender).Text) 
        .Where(text => text.EndsWith("."));
    
        searchTerm.Subscribe(trm => label1.Text = trm);
    }
  17. Run your application and start typing in a line of text. You will see that nothing is output to the label control as you type, as was evident in the previous example before we added in our Where condition:
    How to do it…
  18. Add a period and start adding a second line of text:
    How to do it…
  19. You will see that only after each period, the text entered is added to the label. Our Where condition is, therefore, working perfectly:
    How to do it…

How it works…

The LINQ aspect of Rx allows developers to construct observables. Here are some examples:

  • Observable.Empty<>: Returns an empty observable sequence
  • Observable.Return<>: Returns an observable sequence containing a single element
  • Observable.Throw<>: Returns an observable sequence terminating with an exception
  • Observable.Never<>: Returns a non-terminating observable sequence infinite in duration

The use of LINQ in Rx allows the developer to manipulate and filter the data stream to return exactly what they need.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset