Share via


How to: Add a Computed Field in a LightSwitch Database

 

For the latest documentation on Visual Studio 2017, see Visual Studio 2017 Documentation.

You can create fields that derive their values from the values of other fields in a database for a Visual Studio LightSwitch application. For example, you can add a field that's named Subtotal to an Order_Details entity. You can specify that the value of the Subtotal field is derived by multiplying the value of the UnitPrice field by the value of the OrderQuantity field.

Note

You can't include a computed field as part of a filter condition or sort term in a query. Also, you can't sort information in a screen by choosing the column heading of a computed field.

link to video For a related video demonstration, see How Do I: Write business rules for validation and calculated fields in a LightSwitch Application?.

To define a computed field

  1. In Solution Explorer, open the shortcut menu for an entity or table, and then choose Open.

    The entity or table opens in the Data Designer.

    Note

    For applications that have been upgraded to Visual Studio 2012 Update 2, on the Perspective bar, choose the Server tab.

  2. In the Data Designer, on the command bar, choose Computed Property.

    A new field appears in the bottom row of the entity or table.

  3. In the Name column, enter a name for the new field (for example, Subtotal).

  4. In Type column, choose a data type for the new field (for example, Money).

  5. In the Properties window, choose the Edit Method link.

    The Code Editor opens and generates a method that's named FieldName_Compute.

  6. Add code to the FieldName_Compute method that sets the value of the result parameter. The following example sets the value of the Subtotal field by multiplying the value of the UntiPrice field by the value of the OrderQuantity field.

            partial void Subtotal_Compute(ref decimal result)
            {
                result = this.Quantity * this.UnitPrice;
            }
    
            Private Sub Subtotal_Compute(ByRef result As Decimal)
                result = Me.Quantity * Me.UnitPrice
    

A computed field isn't saved to the data source. A computed field appears only in screens that consume the entity or table. In the data designer, a small icon that resembles a calculator appears next to computed fields. This icon indicates that the field is used for display purposes only and doesn't affect the data source of the entity or table.

In most cases, the value of a computed field is recalculated based on changes to any field that you use to derive the value of the computed field. If the value of the computed field doesn't update, users can refresh the screen to view the updated value. You can also write custom code that refreshes the screen when certain events occur. See How to: Handle Data Events.

See Also

Data: The Information Behind Your Application
How to: Define Data Fields
How to: Create a List of Values for a Field