如何从C#返回字符串Kusto Query的结果

时间:2019-02-23 16:50:17

标签: c# azure c#-4.0 kusto

var kusto = string.Format("let MyData = CompanyMydata" +
                " | where ID == 'Z123' | top 1 by dateTimeUtc desc");

            var reader = client.ExecuteQuery(kusto);
            while (reader.Read())
            {
                //how can i return coming result as list of string or json string?
            } 

while loop中,我可以使用逐个返回单个列值 string state = reader.GetString(1);,但我想返回完整的结果,而不是一栏一栏的值。 这样我就可以对特定的班级做JsonConvert.DeserializeObject<T>(resultString);

1 个答案:

答案 0 :(得分:1)

假设您使用的是here所述的客户端库,则应该能够执行以下操作:

    static void Main(string[] args)
    {
        var kcsb = new KustoConnectionStringBuilder("https://help.kusto.windows.net").WithAadUserPromptAuthentication();
        var databaseName = "Samples";

        using (var queryProvider = KustoClientFactory.CreateCslQueryProvider(kcsb))
        {
            var clientRequestProperties = new ClientRequestProperties() { ClientRequestId = "Sample;" + Guid.NewGuid() };
            var query = "StormEvents | summarize count(), max(EndTime) by State";
            var result = queryProvider.ExecuteQuery<MyType>( // focus on this part
                databaseName,
                query,
                clientRequestProperties)
                .ToList();

            foreach (var row in result)
            {
                Console.WriteLine($"State = {row.State}, Count = {row.Count}, MaxEndTime = {row.MaxEndTime}");
            }
        }
    }

    class MyType
    {
        public string State;
        public long Count;
        public DateTime MaxEndTime;
    }