async public void Run(DigitalTwinsClient client) { AsyncPageable <BasicDigitalTwin> asyncPageableResponseWithCharge = client.QueryAsync <BasicDigitalTwin>("SELECT * FROM digitaltwins"); int pageNum = 0; // The "await" keyword here is required, as a call is made when fetching a new page. await foreach (Page <BasicDigitalTwin> page in asyncPageableResponseWithCharge.AsPages()) { Console.WriteLine($"Page {++pageNum} results:"); // Extract the query-charge header from the page if (QueryChargeHelper.TryGetQueryCharge(page, out float queryCharge)) { Console.WriteLine($"Query charge was: {queryCharge}"); } // Iterate over the twin instances. // The "await" keyword is not required here, as the paged response is local. foreach (BasicDigitalTwin twin in page.Values) { Console.WriteLine($"Found digital twin '{twin.Id}'"); } } }
public async Task QueryTwinsAsync() { PrintHeader("QUERY DIGITAL TWINS"); try { Console.WriteLine("Making a twin query and iterating over the results."); #region Snippet:DigitalTwinsSampleQueryTwins // This code snippet demonstrates the simplest way to iterate over the digital twin results, where paging // happens under the covers. AsyncPageable <string> asyncPageableResponse = client.QueryAsync("SELECT * FROM digitaltwins"); // Iterate over the twin instances in the pageable response. // The "await" keyword here is required because new pages will be fetched when necessary, // which involves a request to the service. await foreach (string response in asyncPageableResponse) { BasicDigitalTwin twin = JsonSerializer.Deserialize <BasicDigitalTwin>(response); Console.WriteLine($"Found digital twin: {twin.Id}"); } #endregion Snippet:DigitalTwinsSampleQueryTwins Console.WriteLine("Making a twin query, with query-charge header extraction."); #region Snippet:DigitalTwinsSampleQueryTwinsWithQueryCharge // This code snippet demonstrates how you could extract the query charges incurred when calling // the query API. It iterates over the response pages first to access to the query-charge header, // and then the digital twin results within each page. AsyncPageable <string> asyncPageableResponseWithCharge = client.QueryAsync("SELECT * FROM digitaltwins"); int pageNum = 0; // The "await" keyword here is required as a call is made when fetching a new page. await foreach (Page <string> page in asyncPageableResponseWithCharge.AsPages()) { Console.WriteLine($"Page {++pageNum} results:"); // Extract the query-charge header from the page if (QueryChargeHelper.TryGetQueryCharge(page, out float queryCharge)) { Console.WriteLine($"Query charge was: {queryCharge}"); } // Iterate over the twin instances. // The "await" keyword is not required here as the paged response is local. foreach (string response in page.Values) { BasicDigitalTwin twin = JsonSerializer.Deserialize <BasicDigitalTwin>(response); Console.WriteLine($"Found digital twin: {twin.Id}"); } } #endregion Snippet:DigitalTwinsSampleQueryTwinsWithQueryCharge } catch (Exception ex) { FatalError($"Could not query digital twins due to {ex}"); } }