Querying and Filtering Data Using LightSwitch in VisualStudio 2012

I have described in my previous article LightSwitch Basic Application about how easily we can create a LightSwitch application using Visual Studio LightSwitch 2012.

Introduction

This article describes how to add, remove and modify a query. We also describe filtering, sorting and the use of parameters.

A query helps in providing necessary information of an application that is developed by a user.

1. Add a Query

Step 1

First, open the Solution Explorer.

Image 1

solutionExplorer.jpg

Step 2

In Solution Explorer, expand the "Data Sources". We will see our Datatable (ContactTable). Right-click on the table; we will have the "Add Query" option.

Image 2

lightquery.jpg

Step 3

The "Query1" property window will be displayed . Query1 can be renamed in the Properties window.

Image 3

lightquery5.jpg

Step 4

Click on the Query Option, a query is added to the Solution Explorer and a query opens in the "Query Designer." Suppose, a Query1 is added to the "ContactTables" as shown.

Image 4

querylight1.jpg

Step 5

As we hover over "Add Filter" we will see a add new filter condition.


Image 5

querylight8.jpg

Step 6

Click on the "Add Filter" Button. It will show the following.

Image 6

lightquery2.jpg

Step 7

In the drop-down list we can choose any of the options. Depending on the choice, we can also add parameters.

Image 7

lightquery3.jpg

Step 8

After selecting the data from the drop-down list we have:

Image 8

querylight10.jpg

Step 9

Now we will sort the list as it is always important to display data to the user in a sorted manner, either alphabetically or in some other way. For that click on the "Add Sort" Button. It will show the following.

Image 9

query 10.jpg

Step 10

In the following step we will click on "Add parameter".

Image 10

lightquery4.jpg

2. Remove a Query

First, open the Solution Explorer, expand the Data Source; we will see our "ContactTable" on expanding the Data Source as shown above. Then we "Add Query". We will have a query. Right-click the query that you want to remove, and then click "Delete".

Image 11

querylight6.jpg

3. Modify a Query

Open the Solution Explorer, expand the Data Source; we will see our "ContactTable" on expanding the Data Source as shown above. Then we "Add Query". We will have a query.

We can perform two operations to modify a Query:

  • Right-click the query that you want to modify, and then click Rename.
  • Double-click on the query. The query opens in the Query Designer.

Image 11

query11.jpg

Next Recommended Readings