C# 如何排除从DynamoDB QueryRequest返回的数据类型描述符?
我正在学习DynamoDB,并尝试使用.NETSDK for AWS运行查询。我使用定义的KeyConditionExpression设置了一个QueryRequest,以便可以搜索分区键C# 如何排除从DynamoDB QueryRequest返回的数据类型描述符?,c#,amazon-web-services,amazon-dynamodb,aws-sdk,dynamodb-queries,C#,Amazon Web Services,Amazon Dynamodb,Aws Sdk,Dynamodb Queries,我正在学习DynamoDB,并尝试使用.NETSDK for AWS运行查询。我使用定义的KeyConditionExpression设置了一个QueryRequest,以便可以搜索分区键 var request = new QueryRequest { TableName = "MyTable", KeyConditionExpression = "ControlNumber = :cn",
var request = new QueryRequest
{
TableName = "MyTable",
KeyConditionExpression = "ControlNumber = :cn",
ExpressionAttributeValues =
new Dictionary<string, AttributeValue> {{":cn", new AttributeValue { S = "123456789" } }}
};
var response = await client.QueryAsync(request);
看起来,除了字符串s之外,我还可以为每种可能返回的数据类型获取一个值。我认为这些被称为数据类型描述符,它们都是空的,除了字符串数据类型之外,它包含了我真正想看到的数据。为什么会这样?我怎样才能得到数据,而不需要那些我不关心的空值
例如,我希望看到:
{ "Status": "Ready", "Type":"OrderStatus"....}
更新:我可以通过AWS CLI运行一个类似的查询,甚至是一个get项,并按预期返回json。因此,.NETSDK似乎正在添加我不想看到的其他数据类型描述符。此外,我正在使用Mulesoft 4,它的功能与.NET SDK相同 如果您使用文档模型,它就是这样工作的 如果您查看他们的示例,您将看到他们正在通过调用PrintDocument打印所有值。看看这段代码,它正在循环并打印每个属性和值
private static void PrintDocument(Document document)
{
// count++;
Console.WriteLine();
foreach (var attribute in document.GetAttributeNames())
{
string stringValue = null;
var value = document[attribute];
if (value is Primitive)
stringValue = value.AsPrimitive().Value.ToString();
else if (value is PrimitiveList)
stringValue = string.Join(",", (from primitive
in value.AsPrimitiveList().Entries
select primitive.Value).ToArray());
Console.WriteLine("{0} - {1}", attribute, stringValue);
}
}
如果您希望继续沿着这条路走下去,您需要构建自己的映射来处理此问题。因此,假设我们有一个InventoryItem对象,该对象由一个状态和类型组成,为简单起见,它的类型为字符串,那么最终会有一个如下的映射函数:
{
"scannedCount": 5,
"count": 2,
"lastEvaluatedKey": null,
"consumedCapacity": null,
"items": [
{
"Status": {
"nullvalue": null,
"ss": null,
"b": null,
"bool": null,
"ns": null,
"l": null,
"m": null,
"n": null,
"bs": null,
"s": "READY"
},
"Type": {
"nullvalue": null,
"ss": null,
"b": null,
"bool": null,
"ns": null,
"l": null,
"m": null,
"n": null,
"bs": null,
"s": "OrderStatus"
}, ......
private InventoryItem Map(Dictionary<string, AttributeValue> result)
{
return new InventoryItem
{
Id = Convert.ToInt32(result["Id"].N),
Status = result["Status"].S,
Type = result["Type"].S
};
}
[DynamoDBTable("Inventory")]
public class InventoryItem
{
[DynamoDBHashKey]
public int Id { get; set; }
public string Status{ get; set; }
public string Type{ get; set; }
}
为了得到那个东西?好吧,你只需要这样做:
InventoryItem inventoryItem = context.Load<InventoryItem>("123456789");
我更喜欢这种方法。你不能用它做那么多的操作,但它更像我习惯的Django的ORM
希望这有帮助 我不使用.Net SDK,但文档模型有帮助吗?看见