Creating a Screen Based on a Query

In this lesson you will create a LightSwitch screen based on a query. By creating a query first, you can limit the data that is shown on the associated screen.

Create a Screen Based on a Query

An earlier lesson, Creating a Basic Query showed how to use a query to filter the results on a screen that was originally created from a Customers entity. You can also create a screen directly based on a query so that the amount of displayed data is restricted and the sort order is controlled.

To create a screen from a query

  1. In Solution Explorer, select the Products node.

  2. On the Project menu, click Add Query.

    The Query Editor appears.

  3. In the Properties window, in the Name field, type CurrentProducts.

  4. In the Query Designer, click Add Filter.

  5. In the first drop-down list, select Where.

  6. In the second drop-down list, select Discontinued.

    Notice that the final drop-down list value changes to False.

  7. In the Query Designer, click Add Sort.

  8. In the first drop-down list, select ProductName.

  9. In the Query Designer, click the Add Screen button on the toolbar.

  10. In the Add New Screen dialog box, in the Select a screen template list, select List and Details Screen.

  11. In the Screen Name box, type CurrentProductList.

  12. In the Screen Data list, select - CurrentProducts and then click OK.

  13. Press F5 to run the application.

    When you open the Current Product List screen, notice that only products that have not been discontinued are displayed, and that they are listed in alphabetical order.

Closer Look

This lesson showed how to create a query and then create a screen to display the results, in this case, products that have not been discontinued. Had you created the screen from the Products entity, the results would have included discontinued products. By creating a query first, you can limit the data that is shown on the associated screen.

This type of query is known as a global query; it can be reused on multiple screens. A global query can also be used as the source for a new global query as long as both return the same entity type.

Next Steps

In the next lesson, you will learn how to create a parameterized query.

Next lesson: Creating a Parameterized Query

See Also

Tasks

How to: Extend a Query by Using Code

Other Resources

Filtering Data with Queries

Queries: Retrieving Information from a Data Source