How to: Filter Data in a LightSwitch Application by Using Code

By using the <EntitySet>_Filter query method, you can display a subset of records for each user based on permissions. For example, you might want to allow each employee to display only their salary information.

To apply a filter

  1. In Solution Explorer, open the shortcut menu for the entity or table to which you want to apply a filter, 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. On the command bar in the Data Designer, in the Write Code list, choose EntitySet**_Filter**.

  3. In the Code Editor, add code to the method.

    The following code example filters the Employees entity so that the current user can display only the records that contain their Employee Name:

    Private Sub Employees_Filter(ByRef filter As System.Linq.Expressions.Expression(Of System.Func(Of Employee, Boolean)))
       filter = Function(e) e.EmployeeName = Me.Application.User.Name
    End Sub
    
    partial void Employees_Filter(ref Expression<Func<Employee, bool>> filter)
            {
                  filter = e => e.EmployeeName == this.Application.User.Name;
            }
    

See Also

Tasks

How to: Handle Data Events

Concepts

Working with Data-Related Objects in Code