Share via


QueryClient.CreateAsync<T> Method

Definition

Execute a query on your IoT hub and get an iterable set of the queried items.

public virtual System.Threading.Tasks.Task<Microsoft.Azure.Devices.QueryResponse<T>> CreateAsync<T> (string query, Microsoft.Azure.Devices.QueryOptions options = default, System.Threading.CancellationToken cancellationToken = default);
abstract member CreateAsync : string * Microsoft.Azure.Devices.QueryOptions * System.Threading.CancellationToken -> System.Threading.Tasks.Task<Microsoft.Azure.Devices.QueryResponse<'T>>
override this.CreateAsync : string * Microsoft.Azure.Devices.QueryOptions * System.Threading.CancellationToken -> System.Threading.Tasks.Task<Microsoft.Azure.Devices.QueryResponse<'T>>
Public Overridable Function CreateAsync(Of T) (query As String, Optional options As QueryOptions = Nothing, Optional cancellationToken As CancellationToken = Nothing) As Task(Of QueryResponse(Of T))

Type Parameters

T

The type to deserialize the set of items into. For example, when running a query like "SELECT * FROM devices", this type should be ClientTwin. When running a query like "SELECT * FROM devices.jobs", this type should be ScheduledJob.

Parameters

query
String

The query. See this document for more details on how to build this query.

options
QueryOptions

The optional parameters to execute the query with.

cancellationToken
CancellationToken

Task cancellation token.

Returns

An iterable set of the queried items.

Exceptions

When the provided query is null.

If the provided query is empty or whitespace.

If IoT hub responded to the request with a non-successful status code. For example, if the provided request was throttled, IotHubServiceException with ThrottlingException is thrown. For a complete list of possible error cases, see IotHubServiceErrorCode.

If the HTTP request fails due to an underlying issue such as network connectivity, DNS failure, or server certificate validation.

If the provided cancellation token has requested cancellation.

Examples

QueryResponse<Twin> queriedTwins = await iotHubServiceClient.Query.CreateAsync<Twin>("SELECT * FROM devices"); while (await queriedTwins.MoveNextAsync()) { Twin queriedTwin = queriedTwins.Current; Console.WriteLine(queriedTwin); } QueryResponse<ScheduledJob> queriedJobs = await iotHubServiceClient.Query.CreateAsync<ScheduledJob>("SELECT * FROM devices.jobs"); while (await queriedJobs.MoveNextAsync()) { ScheduledJob queriedJob = queriedJobs.Current; Console.WriteLine(queriedJob); }

Remarks

The kind of iterable items returned by this query will depend on the query provided.

Applies to