Skip to main content
A newer version of this page is available. .
.NET Framework 4.5.2+

How to: Filter Persistent Objects by Type

  • 2 minutes to read

This topic describes how to filter persistent objects by their type.

ObjectType Field

To filter objects by type, you can use the ObjectType service field (see When and Why XPO Extends the Database Schema).

IsExactType and IsInstanceOfType Function Operators

Alternatively, you can use the IsExactType and IsInstanceOfType function operators (see Criteria Language Syntax).

Assume you have a collection of Employee objects. Employees can either be objects of the LocalEmployee or ForeignEmployee types:

public class LocalEmployee : Employee { /* ... */ }

public class ForeignEmployee : Employee { /* ... */ }

The following code snippet demonstrates how the employees collection can be filtered to contain only foreign employees. The criteria is built with the Simplified Criteria Syntax.

employees.Criteria = PersistentBase.Fields.ObjectType.TypeName == typeof(ForeignEmployee).FullName;
//or
employees.Criteria = CriteriaOperator.Parse("IsExactType(This,?)", typeof(ForeignEmployee).FullName);

For details on the IsExactType and IsInstanceOfType operators, refer to the IsExactTypeFunction and IsInstanceOfTypeFunction class descriptions

Display the Object Type in UI

To display the type in UI, you can implement the following calculated property using the PersistentAliasAttribute:

[PersistentAlias("ObjectType.TypeName")]
public string EntityType { 
    get { 
        return Convert.ToString(EvaluateAlias(nameof(EntityType)));
    }
}
See Also