Export (0) Print
Expand All
1 out of 2 rated this helpful - Rate this topic

Adding Computed Properties

This lesson shows how to add a computed property to a data entity in a LightSwitch application.

Most of the data for a business application is stored in a database or other data source, but you may want to provide additional data to users. One way to do this is by using a computed property, which provides data based on a calculation. The data can be displayed on a screen but it is not stored in the database.

An order total is an example of a computed property for which you write code to add together line-item amounts, tax, and shipping.

To add a computed property

  1. In Solution Explorer, double-click Orders.

  2. In the Data Designer, click <Add Property> and then type OrderTotal.

  3. In the Type column, select Money.

    Notice that in the Properties window, the IsComputed property is selected.

  4. In the Properties window, click the Edit Method link.

    The Code Editor opens and displays the OrderTotal_Compute method.

  5. In the OrderTotal_Compute method, add the following code.

    foreach (Order_Detail od in Order_Details) {
    
        result = result += (od.UnitPrice * od.Quantity);
    }
    result = result + Freight.Value;
    

    When an order is displayed on a screen, this code will execute 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 selected. 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 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 checking to see whether the Freight field has a value. As currently written, if the Freight field is empty it could cause an exception.

Computed properties do not have to involve mathematical calculations. You can also use a computed property to concatenate two strings. For example, if your entity has a FirstName field and a LastName field, you could create a FullName field to display both names together. In this case, the code for the FullName_Compute method would resemble the following example.

result == FirstName + " " + LastName

For a FirstName of "Howard" and a LastName of "Snyder", the FullName would be displayed 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. They cannot be used in a query, and end users cannot sort or search a computed column.

In the next lesson, you will learn how to create relationships between data entities.

Next lesson: Defining Relationships

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.