Skip to main content
.NET 6.0+

Current Object Parameter

  • 5 minutes to read

This topic describes the purpose of the Current Object Parameter and includes a usage example.

You may need a Current Object Parameter to filter a List View in a Lookup Property Editor. To apply a filter, set the DataSourceCriteria attribute for the corresponding reference property. The attribute sets a condition that may need access to business object property values. Use the Current Object Parameter to obtain those values.

Important

You cannot use the Current Object Parameter feature in scenarios not described in this article (for example, with XPCollection, appearance rules or security permissions).

Consider an example with 3 classes: Employee, Position, and Department. Both Employee and Position expose the Department reference property. Additionally, Employee exposes the Position reference property.

using DevExpress.Persistent.Base;
using DevExpress.Persistent.BaseImpl.EF;

namespace YourApplicationName.Module.BusinessObjects;

[DefaultClassOptions]
public class Employee : BaseObject {

    public virtual string Name { get; set; }

    private Department department;

    public virtual Department Department { get; set; }

    public virtual Position Position { get; set; }
}

[DefaultClassOptions]
public class Position : BaseObject {

    public virtual string Title { get; set; }

    public virtual Department Department { get; set; }
}

[DefaultClassOptions]
public class Department : BaseObject {

    public virtual string Title { get; set; }

    public virtual IList<Position> Positions { get; set; } = new ObservableCollection<Position>();

    public virtual IList<Employee> Employees { get; set; } = new ObservableCollection<Employee>();
}

An XAF UI displays reference properties with the help of the Lookup Property Editor:

Unfiltered Lookup Property Editor, DevExpress

You may need to filter the drop-down list in a Lookup Property Editor. For example, you may want to display only those positions that correspond to the department where the employee works. To do so, apply the DataSourceCriteria attribute to the Employee.Position property and specify the filtering criterion as displayed in the following code snippet:

using DevExpress.Persistent.Base;
using DevExpress.Persistent.BaseImpl.EF;

namespace YourApplicationName.Module.BusinessObjects;

[DefaultClassOptions]
public class Employee : BaseObject {

    public virtual string Name { get; set; }

    private Department department;

    public virtual Department Department { get; set; }

    [DataSourceCriteria("Department = '@This.Department'")]
    public virtual Position Position { get; set; }
}

Without Current Object Parameter, the filter condition can only compare Position properties to constant/predefined values. Conditions may also use Function Criteria Operators. To define a criterion that uses Employee property values, use the Current Object Parameter. Note the string that starts with \@This in the example above.

Now the drop-down List View of the Lookup Property Editor in this example displays only positions that fit the specified criterion:

Filtered Lookup Property Editor, DevExpress

Another example of the Current Object Parameter implementation is available in the MainDemo.Module\BusinessObjects\Employee.cs file of the MainDemo application shipped with XAF.

Tip

The MainDemo application is installed in %PUBLIC%\Documents\DevExpress Demos 23.1\Components\XAF\MainDemo.NET.EFCore by default. The ASP.NET Core Blazor version is available online at https://demos.devexpress.com/XAF/BlazorMainDemo.

The following example specifies a filter condition for the Employee.Manager property. The dropdown list only displays employees whose Position is “Manager”. The Current Object Parameter helps exclude the current employee.

namespace MainDemo.Module.BusinessObjects;

[DefaultClassOptions]
public class Employee : Person {
    // ...
    [DataSourceCriteria("Position.Title = 'Manager' AND ID != '@This.ID'")]
    public virtual Employee Manager { get; set; }
    // ...
}

You can use the Current Object Parameter in the following expressions/conditions:

In DataSourceCriteria, \@This refers to the master object (for which the lookup editor is displayed) even if the lookup editor displays a complex property. For example, if the property is Task.Project.Manager, then \@This refers to Task and not to Project.

See Also