🚀 Welcome to MDriven Learn –  MDriven is now on Discord!  Don’t miss the latest Release Notes.
OCLOperators ExecutePS
This page was created by Hans.karlsen on 2018-07-13. Last edited by Wikiadmin on 2026-07-29.

ExecutePS lets you run a PSExpression from a ViewModel and return only the objects that match in the database; use it when maintaining an existing ViewModel, and use <Class>.PSEval for new development.

Status

selfVM.ExecutePS() is replaced by <Class>.PSEval, which is available in the context of every class. Do not introduce ExecutePS in new ViewModels unless you need to maintain an existing implementation.

This page documents the purpose and behavior of the legacy method. For a complete working ViewModel example, see Documentation:How to use the ExecutePS function in selfVM.

When to use ExecutePS

Use ExecutePS when an expression must find a small subset of objects from a potentially large database population. The PS-evaluated expression is translated to SQL and evaluated in the database, rather than requiring the ViewModel to traverse every candidate object in memory.

For example, if the database contains one million Thing objects and the user needs objects whose Attribute1 equals the current Thing.Attribute1, execute the database expression and load only the matching objects. This is preferable to loading all Thing objects into the ViewModel and filtering them there.

PSExpressions are also appropriate for database work over large populations, such as finding one object among millions or calculating aggregate values. See Documentation:PSExpression , or how to do things in the DB from MDriven.

Method

The method is called on the ViewModel variable selfVM:

selfVM.ExecutePS(viewmodelclass, viewmodelcolumn)
Argument Meaning
viewmodelclass The ViewModel class that contains the expression column.
viewmodelcolumn The ViewModel column that holds the PS-evaluated expression to run.

The result is a list returned from the PS evaluation. Depending on the expression, it can contain objects or tuple data.

Configure an existing ViewModel

  1. Add a ViewModel column that contains the expression to evaluate in the database.
  2. Keep this expression column separate from UI columns. It does not need to be referenced by any user interface control.
  3. Add variables needed by the expression. For example, assign vValueToMatch from the current root object's Attribute1.
  4. Add an action that calls selfVM.ExecutePS(viewmodelclass, viewmodelcolumn).
  5. Collect the returned objects into a ViewModel list variable, such as vMyList.
  6. Use that list as the root for a grid when the matching objects must be shown to the user.

Example: find objects with the same value

Assume that the ViewModel root is a Thing and that the user needs to see other Thing objects with the same Attribute1 value.

  1. Set vValueToMatch to the root Thing's Attribute1.
  2. Place the database expression in a dedicated column, for example OutOfLoopToAvoidStdFetch.col1.
  3. Call ExecutePS with the ViewModel class and that column.
  4. Collect the resulting Thing objects into vMyList.
  5. Bind a grid to vMyList.

The database evaluates the match and returns the matching objects; the ViewModel does not need to load every Thing first.

Limit the result size with MaxFetch

ExecutePS checks for a MaxFetch tagged value on the root-level expression column and uses it to limit the returned values.

Set MaxFetch when an expression can match many objects. For example, a lookup intended to show a short selection list should have a limit so that an unexpectedly broad search does not return the full object population.

Related database-expression features

A ViewModel column whose name starts with PSExpression_ is treated as a PSExpression column. The selfVM.PSExpression_Refresh() method evaluates all such columns in the ViewModel. Use this pattern when the goal is to refresh PSExpression values rather than explicitly execute one expression and work with its returned list.

For SQL-specific expressions, including stored-procedure calls and tuple results, see Documentation:OCLOperators sqlpassthrough. Prefer OCL-based PSEval rather than SQL passthrough when it meets the requirement.

See also