Skip to main content
.NET 6.0+

XPQuery<T>.WithDeleted() Method

Creates an XPQuery<T> based on the current XPQuery<T> instance. The new query retrieves objects specified by the original query and also selects objects marked as deleted that match that query.

Namespace: DevExpress.Xpo

Assembly: DevExpress.Xpo.v23.2.dll

NuGet Package: DevExpress.Xpo

Declaration

public XPQuery<T> WithDeleted()

Returns

Type Description
XPQuery<T>

An XPQuery<T> instance.

Remarks

The following example demonstrates how to employ the WithDeleted method in an application:

unitOfWork.Query<Employee>()
    .WithDeleted()
    .Where(e => e.Name == null).ToList();

new XPQuery<Employee>(unitOfWork)
     .WithDeleted()
     .Where(e => e.Name == null).ToList();

To use the WithDeleted feature, make sure to initialize the XPQuery<T> with the XPQuery(Session) or XPQuery(Session, Boolean) constructor.

If you use a constructor that does not take the Session argument, the XPQuery<T> cannot get objects marked for deletion and the following error is displayed: “The WithDeleted option is not supported because XPQuery was initialized with DataLayer. To use the WithDeleted option, employ the XPQuery(Session) or XPQuery(Session, Boolean) constructor.

The WithDeleted method is supported only when it is applied to a parent query, and it is not suitable for getting deleted objects only in a sub query.

// This approach does not work
var subQuery = queryDeletedContacts.WithDeleted().Select(ff => ff.Oid);
var dataQuery = 
    queryTasks
    .Where(ff=> !ff.Deleted)
    .Where(ff => subQuery.Contains(ff.AssignedTo.Oid));

// This approach works
var subQuery = uow.Query<SelectSubDeletedClass>();
var dataQuery = 
    uow.Query<SelectDeletedClass>()
    .WithDeleted()
    .Where(p=> !p.Deleted)
    .Where(p=> subQuery.Contains(p.AssignedTo.Oid));

In scenarios where the value of loading remote objects in the sub-query differs from the main one, we recommend filtering the objects as follows:

  1. Add a property with a persistent alias to the business object:

    [PersistentAlias("Not IsNull([GCRecord])")]
    public bool Deleted => IsDeleted;
    
  2. Always add WithDeleted to a parent request. In this case, all sub requests will be loaded with deleted objects.

  3. Filter objects that should not load deleted objects by the ‘Deleted’ property. For example:

    var dataQuery = uow.Query<SelectDeletedClass>().WithDeleted().Where(p=> !p.Deleted).Select(p=> {YourSubQuery});
    

In this case, filtering by deleted objects will not be applied to the sub query.

See Also