---
title: "Querying client"
slug: "querying-client"
abstract: "Use the Querying client to fetch entities or IDs, handle large result sets with iterators, and manage queries efficiently in Content Hub."
---
# Querying client

The Querying client can be used to execute queries.

> [!warning]
> If multiple items match a query, an `InvalidOperationException` is thrown. When the iterator goes beyond the configured maximum number of results, the server throws an exception. In this case, a scroll is necessary.

> [!note]
> You can also search for specific entities in Content Hub [using a `SearchAfterQuery` query](https://doc.sitecore.com/ch/en/developers/cloud-dev/create-a-searchafterquery-query.html "Create a SearchAfterQuery query").

## Create a query { .notintoc }

You can use the Querying client to [create queries](https://doc.sitecore.com/ch/en/developers/cloud-dev/linq-queries-1523418.html "LINQ queries") and then run any of the following queries or iterate on results.

## View query results { .notintoc }

After running a query, an `IQueryResult` is returned. This result has the following properties:

- `Items` - the entities or IDs that matched the query, which can be a subset of the total results.
- `TotalNumberOfResults` - the number of items that matched the query in Sitecore Content Hub (even if not all of them are in `Items`).
- `Offset` - the number of items that were skipped.

## Query for a single entity { .notintoc }

When it is certain that a query only matches a single entity, use the `SingleAsync` method.

```csharp
IEntity entity = await MClient.Querying.SingleAsync(<QUERY>);

```

## Query for a single entity ID { .notintoc }

When it is certain that a query only matches a single entity, use the `SingleIdAsync` method.

```csharp
long? entityId = await MClient.Querying.SingleIdAsync(<QUERY>);

```

## Query for one or more entities { .notintoc }

To query for one or more entities:

```csharp
IEntityQueryResult queryResult = await MClient.Querying.QueryAsync(<QUERY>);

```

The `IEntityQueryResult.Items` contains the returned entities.

## Query for one or more entity IDs { .notintoc }

To query for one or more entity ids:

```csharp
IIdQueryResult queryResult = await MClient.Querying.QueryIdsAsync(<QUERY>);

```

The `IIdQueryResult.Items` method contains the returned IDs (in `long` format).

## Iterate query result entities { .notintoc }

When the query returns a small to medium size of results (the default configuration is 10,000 results maximum), it is easy to batch process the entities using an iterator.

In the following code snippet, an iterator is created:

```csharp
IEntityIterator iterator = MClient.Querying.CreateEntityIterator(<QUERY>);

```

In the following code snippet, the iterator is used to batch process entities:

```csharp
while (await iterator.MoveNextAsync())
{
  var entities = iterator.Current.Items;
  // Do something with the entities
}

```

The *while* loop automatically ends when the iterator has finished processing all the batches of entities.

## Iterate query result entity IDs { .notintoc }

When the query returns a small to medium size of results (the default configuration is 10,000 results maximum), it is easy to batch process the entity IDs using an iterator.

In the following snippet, an iterator is created:

```csharp
IIdIterator iterator = MClient.Querying.CreateIdIterator(<QUERY>);

```

In the following code snippet, the iterator is used to batch process entity IDs:

```csharp
while (await iterator.MoveNextAsync())
{
  var ids = iterator.Current.Items;
  // Do something with the ids
}

```

The *while* loop automatically ends when the iterator has finished processing all the batches of entities.

> [!warning]
> When the iterator goes beyond the configured maximum number of results, the server throws an exception. In that case, a scroll is necessary.

## Deprecation notes { .notintoc }

On the `Query` class it is possible to set the `EntityLoadOptions` property and the `LoadConfiguration` property. The first property was used by the old WebApiClient SDK and was deprecated in 3.0. In 3.1 the `Query` class and the load configurations were separated. We recommend that you do not use these properties, but pass the load configuration as an extra parameter when passing the query object, when applicable.