This documentation is archived and is not being maintained.

Creating a Basic Query

This lesson shows how to create a LightSwitch query to return a subset of customers.

A query returns data that meets a set of criteria. For example, a query might return a list of customers who are located in the United States. In this example, customers are the data, and located in United States is the criterion.

Note Note

This lesson builds on the MyFirstApplication project that is developed in the Working with Data in LightSwitch (Guided Tour) lessons and modified in the Creating Screens lessons. If you have completed the prerequisite lessons, open the project now; otherwise, complete the prerequisites and then return to this lesson.

To create a query

  1. In Solution Explorer, double-click the OrdersByCustomer node.

  2. In the left pane of the Screen Designer, select Customers.

  3. Click the Edit Query link.

    The Query Designer appears.

  4. In the Query Designer, click Add Filter.

    A filter condition is added to the Filter section. Several drop-down lists and a text box appear next to the filter condition.

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

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

  7. In the third drop-down list, select = (equals).

  8. In the fourth drop-down list, select Literal.

  9. In the text box, type USA.

  10. Click Add Filter again.

  11. In the first drop-down list, select Or.

  12. In the second drop-down list, select Country.

  13. In the third drop-down list, select = (equals).

  14. In the fourth drop-down list, select Literal.

  15. In this text box, type Canada.

    This creates a query expression that will return all records from Customers where the value of the Country field equals either the literal value "USA" or the literal value "Canada".

  16. Click Add Sort.

  17. In the first drop-down list, select City.

    This will sort the query by City in alphabetical order.

    The Query designer
  18. Press F5 to run the application.

    When you open the Orders by Customers screen, notice that only customers from the USA or Canada are displayed, and that they are sorted by city.

This lesson showed how to create a query that returns a subset of customers. The filter section of the query specified a condition, and the sort section specified how to order the results. Translated to natural language, the query says, "Give me a list of all customers in the USA or Canada, sorted by city."

In the first filter drop-down list, you could select either Where or WhereNot. Changing the clause to WhereNot would change the query to return customers from everywhere except the USA or Canada.

The third filter drop-down list contains operators that you can apply to the query. For example, if you had chosen a numeric field in the second drop-down list, you could select > (is greater than) to return only records that exceed a specified amount.

The fourth filter drop-down list contains three choices: Literal, Property, and Parameter. As demonstrated in this lesson, Literal means that the criterion is a literal value. If you had chosen Property, a list of the fields in the Customers entity would have been displayed so that you could filter results by property. For example, you could return all customers where the ContactName matches the CompanyName. The Parameter option is described in the Creating a Parameterized Query lesson.

The sort section of a query determines the order in which the results are first displayed on a screen in the running application. A user can then change the sort order.

In the next lesson, you will learn how to create a screen based on a query.

Next lesson: Creating a Screen Based on a Query