You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用.NET SDK执行简单KQL查询时出现语义错误求助

问题原因

报错的核心原因是表名包含连字符-,KQL语法中,这类包含特殊字符的标识符(表名、列名等)如果直接使用,会被解析为算术表达式(比如df-edata-marley会被当成df - edata - marley),而非表名,因此触发语义错误。

解决方法

将包含特殊字符的表名用**方括号[]或反引号`**包裹,明确告诉KQL这是一个标识符而非表达式。

修改后的代码示例

代码示例1(修正后)

using (var cslQueryProvider = KustoClientFactory.CreateCslQueryProvider(kustoConnectionString))  
{  
    // 用方括号包裹表名
    var query = $"[df-edata-marley] | count";  
    // 或用反引号包裹:var query = $"`df-edata-marley` | count";
    var results = cslQueryProvider.ExecuteQuery<long>(config.DatabaseName, query);  
}

代码示例2(修正后)

// 用反引号包裹表名
string query = @"`df-edata-marley` | take 2";
// 或用方括号包裹:string query = @"[df-edata-marley] | take 2";
using (var response = queryClient.ExecuteQuery(configDatabaseName, query, null))
{
    int columnNoStartTime = response.GetOrdinal("StartTime");
    int columnNoState = response.GetOrdinal("State");
    int columnNoDailyDamage = response.GetOrdinal("DailyDamage");

    Console.WriteLine("Daily tornado damages over 100,000,000$:");
    
    while (response.Read())
    {
        Console.WriteLine("{0}",response.GetValue(0)); 
        // 注:原代码格式字符串多了一个冗余的},已一并修正
    }
}
验证说明

修改后重新执行查询,KQL会正确识别表名,避免将其解析为算术表达式,即可消除“Scalar is not expected in the current context”异常。

内容的提问来源于stack exchange,提问作者James Baldwinson

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.07 09:43:13