简体   繁体   中英

How can I return result of string Kusto Query from c#

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?
            } 

In while loop I can able to return single column value one by one by using string state = reader.GetString(1); but i want to return complete result instead of one by one column value. so that i can do JsonConvert.DeserializeObject<T>(resultString); to specific class.

assuming you're using the client libraries mentioned here , you should be able to do something like the following:

    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;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM