Adding a Computed Property in a LightSwitch Application
This lesson shows how to add a computed property to a data entity 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.
To add a computed property
In Solution Explorer, open the shortcut menu for the Orders.lsml node, and then choose Open.
In the Data Designer, replace the <Add Property> placeholder with OrderTotal.
In the Type column, open the list, and then choose Money.
In the Properties window, the IsComputed property is active.
In the Properties window, choose the Edit Method link.
The Code Editor opens and displays the OrderTotal_Compute method.
In the OrderTotal_Compute method, 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.
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 UnitPrice field by the Quantity field and then stores the accumulated total in the result variable. The final line of code adds the value of the 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 resemble the following example.
For a 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 OrderTotal property.
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 in a LightSwitch Application