Adding a Computed Property in a LightSwitch Application
For the latest documentation on Visual Studio 2017 RC, see Visual Studio 2017 RC Documentation.
This lesson shows how to calculate data by using a computed property in a LightSwitch application.
Most data for business applications is stored in a database or other data source, but you can also use a computed property to provide data that's based on a calculation. The data can appear on a screen without being stored in a database.
For example, an order total is a computed property for which you write code to add together line-item amounts, tax, and shipping.
In Solution Explorer, open the shortcut menu for the Orders.lsml node, and then choose Open.
In the Data Designer, on the Perspective bar, choose Server, and then choose the <Add Property> link and enter
In the Type column, choose Money.
In the Properties window, the IsComputed property is checked and an Edit Method link is added.
In the Properties window, choose the Edit Method link.
The Code Editor opens and displays the
OrderTotal_Computemethod, add the following code.
For Each Order_Detail In Order_Details result = result + (Order_Detail.UnitPrice * Order_Detail.Quantity) Next result = result + Freight
When an order appears on a screen, this code will run and calculate the order total. See the next section for an explanation of the code.
This lesson showed how to add a computed property to an entity to calculate a value. You may have noticed that when you added the OrderTotal field to the Orders entity, the field was marked as required. In the Properties window, the IsComputed option was also automatically enabled. Computed fields for entities from an attached data source are always both required and computed.
The code example uses a
For… Each construct to loop through the
Order_Detail entity. For each line item, the code multiplies the item’s price (
UnitPrice field) by the number of items ordered (
Quantity field), and then stores the accumulated total in the
result variable. The final line of code adds the value of the shipping charge (
Freight field) to the accumulated total. You could improve this code by verifying whether the
Freight field has a value. As currently written, the
Freight field could cause an exception if a user leaves it empty.
Computed properties don't always involve mathematical calculations. You can also use a computed property to concatenate two strings. For example, you could create a
FullName field that displays a
FirstName field and a
LastName field together. In this case, the code for the
FullName_Compute method would look like this:
FirstName of "Howard" and a
LastName of "Snyder", the
FullName would appear on a screen as "Howard Snyder".
You can also create a computed property that uses the values of other computed properties. For example, you could add a
Tax property to the
Orders entity, and then write code to calculate tax based on a percentage of the computed
Computed properties also have some limitations. You can’t use them in a query, and end users can’t sort or search a computed column.
In the next lesson, you’ll learn how to create relationships between data entities.
Next lesson: Defining Relationships