'Fetch results of unknown Kusto query in C#

I am trying to fetch results from an unknown Kusto query.

Example:

var query = "StormEvents | count | as HowManyRecords; StormEvents | limit 10 | project StartTime, EventType, State | as SampleRecords";

Assuming I don't know anything about this query, is there some way on my C# client side to break apart what Kusto is returning to me (here it will be returning 2 different things) and encapsulate the responses in appropriate C# objects ?



Solution 1:[1]

We can loop through the result set, and for each table use GetSchemaTable()

Here is a quick example.
Please note that in addition to the queries results we get tables that describe the render options, the execution statistics and the names of all tables.

using System.Data;

string query = "PopulationData | take 3; StormEvents | take 5";
string cluster = "https://help.kusto.windows.net/Samples";

using (var client = Kusto.Data.Net.Client.KustoClientFactory.CreateCslQueryProvider($"{cluster};Fed=true"))
{
    using IDataReader reader = client.ExecuteQuery(query);

    do
    {
        Console.WriteLine(new string('*', 40));

        DataTable? schemaTable = reader.GetSchemaTable();

        if (schemaTable != null)
        {
            foreach (DataRow row in schemaTable.Rows)
            {
                Console.WriteLine($"{row["ColumnOrdinal"],3} {row["ColumnName"],-20} {row["ColumnType"],-20}");
            }
        }
    }
    while (reader.NextResult());
}

****************************************
  0 State                string
  1 Population           long
****************************************
  0 StartTime            datetime
  1 EndTime              datetime
  2 EpisodeId            int
  3 EventId              int
  4 State                string
  5 EventType            string
  6 InjuriesDirect       int
  7 InjuriesIndirect     int
  8 DeathsDirect         int
  9 DeathsIndirect       int
 10 DamageProperty       int
 11 DamageCrops          int
 12 Source               string
 13 BeginLocation        string
 14 EndLocation          string
 15 BeginLat             real
 16 BeginLon             real
 17 EndLat               real
 18 EndLon               real
 19 EpisodeNarrative     string
 20 EventNarrative       string
 21 StormSummary         dynamic
****************************************
  0 Value                string
****************************************
  0 Timestamp            datetime
  1 Severity             int
  2 SeverityName         string
  3 StatusCode           int
  4 StatusDescription    string
  5 Count                int
  6 RequestId            guid
  7 ActivityId           guid
  8 SubActivityId        guid
  9 ClientActivityId     string
****************************************
  0 Ordinal              long
  1 Kind                 string
  2 Name                 string
  3 Id                   string
  4 PrettyName           string

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1