Skip to main content
.NET Framework 4.5.2+

Session.GetObjectsByKeyFromQuery(XPClassInfo, Boolean, String) Method

Returns reloaded persistent objects from a data store, with key property values obtained via the specified SQL query.

Namespace: DevExpress.Xpo

Assembly: DevExpress.Xpo.v23.1.dll

NuGet Package: DevExpress.Xpo


public ICollection GetObjectsByKeyFromQuery(
    XPClassInfo classInfo,
    bool alwaysGetFromDataStore,
    string sql


Name Type Description
classInfo XPClassInfo

An XPClassInfo object which contains the metadata information of the persistent objects that are retrieved, based on the query’s result set.

alwaysGetFromDataStore Boolean

true to reload persistent objects from storage, if they are found in memory; otherwise, false.

sql String

A String value that specifies a SQL query.


Type Description

A collection of persistent objects with specified key property values.


The GetObjectsByKeyFromQuery method works as follows:

  1. Executes the specified SQL query and loads key property values.
  2. Searches memory for persistent objects with retrieved key property values. If such objects are found, they are not reloaded.

To get up-to-date objects from storage, set the alwaysGetFromDb parameter to true.

The GetObjectsByKeyFromQuery method may throw the InvalidCastException if a keys’ value and property type do not match. To avoid this error, select key values from a column mapped to the same key property type. The following article describes how XPO maps a CLR type to a database type: Data Types Supported by XPO.

ADO.NET data readers may use different mappings between CLR and database types. XPO works seamlessly with the following key property types: Int32, Int64, Guid. To work with other key property types, ensure that the corresponding ADO.NET data reader returns values of the same type as used in the data model. Use the ExecuteQuery and GetObjectsByKey methods instead of GetObjectsByKeyFromQuery when the type conversion is required.


The GetObjectsByKeyFromQuery method does not find objects, until they are saved to a data store.

For more informatioon about executing SQL queries in XPO, refer to Direct SQL Queries.

See Also