Skip to main content
.NET Framework 4.6.2+

Session.GetObjectsFromQueryAsync(XPClassInfo, List<XPMemberInfo>, String, QueryParameterCollection, CancellationToken) Method

Asynchronously executes a SQL query and returns its result set as a collection of non-persistent objects.

Namespace: DevExpress.Xpo

Assembly: DevExpress.Xpo.v24.2.dll

Declaration

public Task<ICollection> GetObjectsFromQueryAsync(
    XPClassInfo classInfo,
    List<XPMemberInfo> members,
    string sql,
    QueryParameterCollection parameters,
    CancellationToken cancellationToken = default(CancellationToken)
)

Parameters

Name Type Description
classInfo XPClassInfo

An XPClassInfo object which contains the metadata information on a non-persistent class corresponding to the query’s result set. The class must be decorated with NonPersistentAttribute.

members List<XPMemberInfo>

Specifies object members mapped to columns in the result set. The order of list elements should correspond to the order of result set columns.

sql String

Specifies an SQL statement.

parameters QueryParameterCollection

Specifies query parameter values.

Optional Parameters

Name Type Default Description
cancellationToken CancellationToken null

A CancellationToken object that delivers a cancellation notice to the running operation.

Returns

Type Description
Task<ICollection>

A Task<TResult> that returns a collection of non-persistent classInfo objects instantiated with data obtained via the specified SQL query.

Remarks

Use QueryParameterCollection to pass strongly-typed parameters. See examples here: Always Encrypted (SQL Server only).

string sql = "select (FirstName + ' ' + LastName) as Name, City, Country from Employees where City = @p0";
XPClassInfo classInfo = session.GetClassInfo<EmployeeModel>();
List<XPMemberInfo> members = new List<XPMemberInfo>() {
    classInfo.GetMember(nameof(EmployeeModel.Name)),
    classInfo.GetMember(nameof(EmployeeModel.City)),
    classInfo.GetMember(nameof(EmployeeModel.Country))
};
QueryParameterCollection parameters = new QueryParameterCollection() {
    new ParameterValue() { DBTypeName = "nvarchar(15)", Value = "London" }
};
ICollection queryResultData = await session.GetObjectsFromQueryAsync(classInfo, members, sql, parameters);

The connection provider automatically generates sequential parameter names (p0, p1, p2 …). To specify custom parameter names, use the overloaded GetObjectsFromQueryAsync(XPClassInfo, List<XPMemberInfo>, String, String[], QueryParameterCollection, CancellationToken) method.

This method does the following:

  • Executes the specified SQL query and obtains the query result.
  • Creates objects using the specified classInfo and populates their properties with the resulting set’s data The members parameter specifies the mapping between columns in the resulting set and object properties. Note that classInfo should represent a non-persistent class decorated with a NonPersistentAttribute.

Note

The GetObjectsFromQueryAsync method throws an exception if the members array includes any of the following classInfo members:

  • A property that is not defined within the classInfo object.
  • A struct type member.
  • A reference to a class with a struct type member.
  • A collection used in object relations.

To learn more about executing SQL statements in XPO, refer to Direct SQL Queries.

Note

The GetObjectsFromQueryAsync method sends statements directly, so the correct statement syntax and parameter names format depends on a particular database server.

See Also