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

C#中DynamoDB QueryAsync查询结果无法正常显示问题及修复方案

首次在C#项目中使用DynamoDB,对相关操作逻辑不熟悉,在处理查询返回结果时遇到异常:

  • 尝试将查询结果直接写入文件时,文件中仅输出如下内容:
System.Threading.Tasks.Task`1[System.Collections.Generic.IEnumerable`1[ConnectDB.Tables.Reply]]
  • 未做转换直接将结果赋值给字符串类型变量时,会抛出如下编译错误:
CS0029: Cannot implicitly convert type 'System.Threading.Tasks.Task<System.Collections.Generic.IEnumerable<ConnectDB.Tables.Reply>>' to 'string'

对应的查询方法实现代码如下:

public async Task<IEnumerable<Reply>> Get(string Id = "001")
{
         AmazonDynamoDBClient client = new AmazonDynamoDBClient(awsAccessKeyId: "ihgowijrlejsafjaklsjgo", awsSecretAccessKey: "hiowajflekahglajsgaijkgilw", Amazon.RegionEndpoint.ap-east-1);
         DynamoDBContext context = new DynamoDBContext(client);

         return await context.QueryAsync<Reply>(Id).GetRemainingAsync();
}
更新:问题已修复

问题根源为异步方法结果的调用、序列化展示逻辑错误,感谢Ermiya Eskandary的帮助,修复后的实现代码如下:

public Getitems new_obj = new Getitems();
protected async void buttonclick(object sender, EventArgs e)
{
        string path = @"file.json";
        StreamWriter OurStream;
        OurStream = File.CreateText(path);
        var results = await new_obj.Get();
        var options = new JsonSerializerOptions { WriteIndented = true };
        string jsonString = JsonSerializer.Serialize(results, options);
        OurStream.WriteLine(jsonString);     
        OurStream.Close();
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.28 14:51:18