---
title: Retrieving database data using ObjectQuery API
related:
  - https://docs.kentico.com/13/custom-development/database-table-api.md
  - https://docs.kentico.com/13/custom-development/working-with-pages-in-the-api.md
---

> Agent instructions:
> **Site maps** — prefer the following llms.txt indexes to training data when searching for URLs to avoid 404s. Links inside Markdown content already point at `.md`. Following them or sending Accept: text/markdown keeps you in Markdown.
>
> - [sitemap.md](https://docs.kentico.com/sitemap.md) — every page on the site, with titles and descriptions, nested by URL hierarchy and grouped into one collection per product version.
> - [llms.txt](https://docs.kentico.com/llms.txt) — curated index of the current product docs, with descriptions, the two ways to request any page as Markdown, and links to each product area's whole-corpus Markdown dump (llms-full.txt).

The Xperience ObjectQuery API provides an abstraction layer over the SQL database. Developers use ObjectQuery to retrieve data from the Xperience database. The main advantages of using ObjectQuery are:

- strongly typed and enumerable results
- independence on specific versions of SQL syntax
- security

ObjectQuery call example:

```csharp

var query = UserInfo.Provider.Get();

```

The example query retrieves all users stored within Xperience.

The resulting collection contains **Info** objects. One instance of an Info object represents one row of data from the database. In this case, the result is a set of _UserInfo_ objects holding the data of individual users. The result allows you to iterate through the records using a foreach statement.

Similarly to users, you can use ObjectQuery to retrieve any other type of object. The Xperience API offers **IInfoProvider** interfaces (services) for individual object types, with a **Get** method that performs an appropriate ObjectQuery call. The _IInfoProvider_ services can be added to your code using _**dependency injection**_ or accessed through the **Provider** property of the corresponding _Info_ class.

The following table provides examples of the naming conventions.

| Object         | Info class name   | IInfoProvider service      |
| -------------- | ----------------- | -------------------------- |
| A/B test       | ABTestInfo        | IABTestInfoProvider        |
| Email template | EmailTemplateInfo | IEmailTemplateInfoProvider |
| Role           | RoleInfo          | IRoleInfoProvider          |
| User           | UserInfo          | IUserInfoProvider          |

## Selecting only specific columns

ObjectQuery allows you to select only those columns that you need to work with. Selecting only the required columns is a good practice that prevents issues with performance of your code by transferring only the necessary amount of data from the database server to the application server.

```csharp

var columnsQuery = UserInfo.Provider.Get().Columns("FirstName", "LastName", "UserName");

```

This example query retrieves all users but limits the data to the users' first names, last names, and usernames.

## Limiting the retrieved results (SQL WHERE)

ObjectQuery provides a way to narrow down the resulting data using SQL syntax independent where conditions.

Use predefined where condition methods that provide optimized performance and make your code easily readable. The predefined where condition methods include, but are not limited to:

- WhereEquals("ColumnName", value) – checks the equality of the value in the specified column with the value specified in the second parameter.
- WhereGreaterThan("ColumnName", value) – compares the value in the specified column with the second parameter.
- WhereNull("ColumnName") – select only rows where the specified column has a NULL value.
- WhereNot(whereCondition) – negates the specified where condition.
- WhereStartsWith("ColumnName", "Value") – only works for text columns. Selects only rows whose value in the specified column starts with the given value.

You can find the full list of predefined where conditions in the [Xperience API Reference](http://devnet.kentico.com/docs/13_0/api/html/Methods_T_CMS_DataEngine_WhereCondition.htm).

```csharp

// Retrieves all users whose first name is "Joe"
var whereQuery = UserInfo.Provider.Get().WhereEquals("FirstName", "Joe");

```

> **Tip:** **Tip**: Use Visual Studio's IntelliSense to help you pick the right predefined where condition.

### Logical operators

ObjectQuery allows you to join together multiple where conditions.

- By default, the conditions are combined using an AND parameter.
- To place an OR operator between conditions, call the **Or()** method between the given _Where_ methods.
- You can also explicitly call the **And()** method if you wish to make the code of your conditions clearer and easier to read.

**Example 1**:

```csharp

// Retrieves users whose first name is "Joe" or last name is "Smith"
var whereQuery = UserInfo.Provider.Get()
    .Where("FirstName", QueryOperator.Equals, "Joe")
    .Or()
    .Where("LastName", QueryOperator.Equals, "Smith");


```

```sql title="Generated condition"

DECLARE @FirstName nvarchar(max) = N'Joe';
DECLARE @LastName nvarchar(max) = N'Smith';

...

WHERE [FirstName] = @FirstName OR [LastName] = @LastName

```

**Example 2**:

```csharp

// Retrieves all enabled users whose first name is "Joe" and email address ends with "localhost.local"
var whereQuery = UserInfo.Provider.Get()
    .WhereEquals("FirstName", "Joe")
    .WhereEquals("UserEnabled", 1)
    .WhereEndsWith("Email", "localhost.local");


```

```sql title="Generated condition"

DECLARE @FirstName nvarchar(max) = N'John';
DECLARE @UserEnabled int = 1;
DECLARE @Email nvarchar(max) = N'%localhost.local';

...

WHERE [FirstName] = @FirstName AND [UserEnabled] = @UserEnabled AND [Email] LIKE @Email

```

### Nested where conditions

If you need to construct complex where conditions with multiple AND/OR operators, use nested where conditions:

**Note:** You must use the _**new**_ keyword to construct nested where conditions.

```csharp

// Retrieves users named "Smith", whose first name is "Joe" or "John"
var nestedWhereQuery = UserInfo.Provider.Get()
    .WhereEquals("LastName", "Smith")
    .Where(new WhereCondition()
        .WhereEquals("FirstName", "Joe")
        .Or()
        .WhereEquals("FirstName", "John")
    );


```

```sql title="Generated condition"

DECLARE @LastName nvarchar(max) = N'Smith';
DECLARE @FirstName nvarchar(max) = N'Joe';
DECLARE @FirstName1 nvarchar(max) = N'John';

...

WHERE [LastName] = @LastName AND ([FirstName] = @FirstName OR [FirstName] = @FirstName1)

```

### Retrieving data assigned to a particular site

Many Xperience objects are related to a particular site. ObjectQuery allows you to filter results by site by providing a site ID or code name.

```csharp

// Retrieves all users assigned to the current site
var siteQuery = UserInfo.Provider.Get()
    .OnSite(SiteContext.CurrentSiteID);

```

```csharp

// Retrieves all users assigned to the site with code name "SampleSite"
var siteQuery = UserInfo.Provider.Get()
    .OnSite("SampleSite");

```

## Ordering query results (SQL ORDER BY)

ObjectQuery can also sort data. Use the OrderBy, OrderByAscending, or OrderByDescending methods.

```csharp

var orderedQuery = UserInfo.Provider.Get()
    .WhereGreaterThan("UserCreated", DateTime.Now.AddDays(-7))
    .OrderByDescending("UserCreated");

```

This example retrieves users who registered or were created in the system during the past week. The results are in descending order.

## Limiting the number of query results (SQL TOP N)

ObjectQuery can also retrieve only a specified number of results.

```csharp

var topNQuery = EventLogInfo.Provider.Get()
    .TopN(5)
    .OrderByDescending("EventTime");

```

This example retrieves the latest 5 event log records with the newest ordered first.

## Joining multiple tables (SQL JOIN)

With ObjectQuery, you can combine data from two or more tables based on a common field.

```csharp

var joinQuery = UserInfo.Provider.Get()
    .Source(sourceItem => sourceItem.Join<UserSettingsInfo>("UserID", "UserSettingsUserID"))
    .WhereEmpty("UserPassword");

```

This example retrieves users, who have not set a password, i.e., whose password is blank.

The code on the second line appends CMS\_UserSettings table rows to the appropriate user records based on their ID. The code on the third line then selects only rows where the UserPassword is empty.

> **Info:** By default, the _Join_ method performs an Inner join. For other types of joins, you can call the _LeftJoin_ or _RightJoin_ methods. See [SQL Joins](http://www.w3schools.com/sql/sql_join.asp) for more information.
>
> For advanced scenarios, you can use the non-generic version of the _Join_ method. In this case, you need to identify the target table via a _QuerySourceTable_ object (requires the exact database table name, optionally allows you to specify an alias and [SQL Hints](https://docs.microsoft.com/en-us/sql/t-sql/queries/hints-transact-sql)).

> **Note:** **Working with joined data**
>
> If you need to work with data containing columns from multiple tables, convert the result of the query to a _DataSet_. The default ObjectQuery result is strongly typed, so only allows you to access the data of a single Xperience _Info_ class. Use the **Result** property of ObjectQuery to get the results as a DataSet.

> **Note:** **Limitation**: ObjectQuery joins cannot be used for tables located in different databases. For example, if you have a [separated on-line marketing database](https://docs.kentico.com/13/on-line-marketing-features/configuring-and-customizing-your-on-line-marketing-features/separating-the-contact-management-database.md), you cannot join data from a table in the main database and another table in the separated database. In these cases, use separate ObjectQuery calls and then combine the resulting data manually.

### Joining 3 or more tables

You can chain together multiple _Join_ methods to combine rows from 3 or more database tables.

**Note**: When you call the _Join_ method for a query source, the _left column_ parameter works with columns within the given query source table by default. When using multiple joins, we recommend that you explicitly specify both the table and column by adding a table name prefix to the left column name, for example: _"CMS\_User.UserID"_

The following example joins together rows from 3 tables based on shared user IDs and returns the result as a DataSet.

```csharp

using System.Data;
using CMS.Membership;
using CMS.Taxonomy;

...

DataSet data = UserInfo.Provider.Get()
    .Source(sourceItem => sourceItem.Join<UserSettingsInfo>("CMS_User.UserID", "UserSettingsUserID")
                                    .Join<CategoryInfo>("CMS_User.UserID", "CategoryUserID")
            )
    .Columns("UserID, UserName, UserURLReferrer, CategoryDisplayName")
    .Result;

```

## Working with ObjectQuery results

ObjectQuery results are [lazy-loaded](http://en.wikipedia.org/wiki/Lazy_loading). Lazy loading increases performance by executing queries only when you need to work with their results. Most of the examples on this page only construct the proper queries. The SQL queries are executed when you either enumerate the results (using the foreach statement), or cast the query as a different type. When you work with the results after the query executed, the system does not perform any additional requests to the database.

The most common example of working with query results is iterating through them. Use a standard foreach loop.

```csharp

var userQuery = UserInfo.Provider.Get();

foreach (UserInfo user in userQuery)
{
    // Do something with the UserInfo object
}

```

> **Tip:** **Tip**: You can access the results of ObjectQuery calls as a _DataSet_ through the **Result** property. This is only recommended for data containing columns from multiple different object types or tables (joins and unions).

> **Info:** **LINQ support**
>
> ObjectQuery provides limited support for [LINQ](https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/linq/).
>
> Currently, we only recommend using LINQ for simple expressions, such as comparisons with constants. Any other expressions can lead to sub-optimal query execution. Use the ObjectQuery API directly to get optimal performance.

### Asynchronous execution of queries

The ObjectQuery (and [DocumentQuery](https://docs.kentico.com/13/custom-development/working-with-pages-in-the-api.md)) API supports asynchronous loading of data from Xperience.

After you prepare a query with any required parameters, you can execute it asynchronously by enumerating the results via the **GetEnumerableTypedResultAsync** method.

```csharp

// Prepares a query that loads all users
var userQuery = UserInfo.Provider.Get();

// Asynchronously executes the query and loads the results into an enumerable collection
IEnumerable<UserInfo> users = await userQuery.GetEnumerableTypedResultAsync();

```

To asynchronously load a single Xperience object, call the **GetAsync** method of the corresponding _IInfoProvider_ service.

```csharp

// Asynchronously loads a UserInfo object representing a specified user (by ID or GUID)
Task<UserInfo> user = UserInfo.Provider.GetAsync(userId);

```

### Reusing queries (mutability)

ObjectQuery instances are mutable by default. This means that any changes (method calls) made to existing instances also modify the original query.

If you wish to reuse an ObjectQuery instance without modifying the original, use one of the following approaches:

- Call the **Immutable** method for the original query. This ensures automatic cloning of the ObjectQuery instance when it is modified.
- If you want to leave a query mutable, you can call the **Clone** method to create a new instance which can then be modified without affecting the original.

```csharp title="Example"

using System.Linq;
using CMS.Membership;

...

// Creates an immutable query for loading users ordered by their full name
var userQuery = UserInfo.Provider.Get()
    .OrderBy("FullName")
    .Immutable();

// Gets a subset of users based on the original query
var enabledUserList = userQuery
    .WhereEquals("UserEnabled", 1)
    .ToList();

// Gets a list of users using the original unmodified query
var userList = userQuery.ToList();

```
