Composed Query and Scalar Query in LightSwitch Beta 2

Prologue:

In my previous article, we discussed queries in LightSwitch. As we know how to create queries in LightSwitch, today we shall discuss the two different types of queries called Composed and Scalar queries based on their functionality.
 
What is Composed Query?

A Composed query operates on the results obtained from the source query i.e. these type of queries will take another query as their source.

There are some benefits in using a query as a source of another query. The composed query reuses the logic built on the designer and the code written in the PreprocessQuery () method of the source query.

In addition, it helps in maintainability. It is easier to update a single query than make changes on multiple queries.

What is Scalar Query?

A Scalar query returns only one result i.e. if you need to get a single result from thousands of records you can use the Scalar queries.

Let us see the demo application which will use the Composed and Scalar queries to demonstrate how to create and use it.

Preparing the Solution:

Start VS 2010; create a LightSwitch desktop application named "ComposedScalarQueryInLSBeta2" as shown in the figure below.

CSQuery1.gif

Follow the No: # to create the Desktop LightSwitch application.

The Entity Designer:

Let us create a Table.

CSQuery2.gif

This new screen for Beta 2 is not available in Beta 1. Select the option shown rounded to create a new table.

After clicking on the "Create new table" option, the Entity Designer will be displayed to create the Entity required.

CSQuery3.gif

To change the Table name simply click on the Header of the Entity Designer and give the Name. In our sample application we have given the name Person.

Adding Composed Query:

CSQuery4.gif

To add a query,

  1. Right-Click on the Entity People.
  2. We will be getting a menu with Add Query option. Choose it.

Forming Query:

After clicking on the Add Query, we will be having a Query Designer as shown in the figure.

CSQuery5.gif

Here in the above figure:
  1. Create a query as shown in the above figure.
  2. For our convenience just create a Sort. For more details about creating the Query please read this Article.

    CSQuery6.gif

The above figure shows how to create a Composed Query. As we have discussed just now, add a Create a query and change the Source from People to PeopleFromTN. The PeopleFromTN is a query and this new query uses the existing query as source.

CSQuery7.gif

Name the Composed query YoungPeople as this query will return the people whose age is below 25.

Multi-Level Composition on Query:

Multi-Level Composition is nothing but creating a query from a composed query. For example we are going to create a Scalar query which takes the composed query as that source.

Adding Scalar Query:

CSQuery8.gif

Add one more query with the name PersonFromCity and form the query as shown in the above figure.

CSQuery9.gif

Go to the property page of PersonFromCity query. Change the "Number of Results Returned" option as One as shown in the figure.

That's It. We have done our job.

Let us create the UI.

Creating the Search Data Screen:

There are two ways to create a screen. One is by selecting the Screen... button on the Entity Designer screen top.

CSQuery10.gif

Another way is to right-click on the Screens section in the Solution Explorer.

Here:
  1. Select the Search Data Screen template from Template panel.
  2. Select the Screen Data as People.

This screen is used to create data and also will display all the record without any filter.

CSQuery11.gif

Add another screen as shown in the figure. Here:
  1. Select the Search Data Screen Template.
  2. Select the Screen Data as PeopleFromTN.

Adding Composed Query with Screen:

To add the composed query, just click on the "Add Data Item" from the Top of the Screen.

CSQuery12.gif

After clicking the Add Data Item, we will have a Dialog window as shown below.

CSQuery13.gif

Here,

Select the Query option as Screen Member Type.

Select the YoungPeople composed query and give it the name YoungPeople.

To include these two queries into the SearchPeopleFromTN screen, just have a look at the following screen.

CSQuery14.gif

Here,
  1. Select Root Node "Search People From TN" and change the Layout as Column Layout.
  2. Add a new group and name it Composed Scalar Query Screen.
  3. Select the new added group and Press the Add dropdown list. Now select the Young People Screen Data which is a composed query.

Adding Scalar Query with Screen:

As like the composed query, just add the PersonFromCity Scalar query to the Screen.

CSQuery15.gif

Now we may see the two added queries in the Left Side panel of the screen. To include the PersonFromCity Scalar query, just follow the figure.

CSQuery16.gif

Here:
  1. Select the "Composed Scalar Query Screen".
  2. Click on Add dropdown list.
  3. Select "Person From City" Screen Data from the dropdown list.
  4. Now we may see the all the Composed and Scalar query in the Screen's Lift Side panel as shown in the following figure.

    CSQuery17.gif

Now we may see the all the Composed and Scalar queries in the Screen's Lift Side panel as shown in the following figure.

The Solution Explorer:

CSQuery18.gif

In the above figure:
  1. PeopleFromTN is a normal Query.
  2. PersonFromCity is the Scalar Query which uses the source as YoungPeople.
  3. YoungPeople is the composed query which uses the Source as PeopleFromTN.
  4. In screen section, the SearchPeople is a screen which will displays all the records which we have created.
  5. SearchPeopleFromTN is the Screen which will have the Composed and Scalar queries.

Now we are ready to Hit F5. Press Play button to see the Screens.

Application in Action:

CSQuery19.gif

The above screen is displaying the all the three records as we have created only three.

In the following screen:
  1. The group with the name "People From TN" is displaying TWO records which have the State as TN.
  2. The group with name "Young People" is displaying TWO records processed by composed query in which the Age is < 25.
  3. Below to the "Young People" group, Screen has the single record processed by Scalar query which has the City as Erode.

    CSQuery20.gif

Summary:

In this article, we have seen how to create and use the Composed Query and Scalar Query in a LightSwitch application.

Thanks for spending your precious time here. Please provide your valuable feedbacks and comments, which enable me to give a better article the next time.
 
Thanks.

Up Next
    Ebook Download
    View all
    Learn
    View all