Col*_*nee 1 c# amazon-dynamodb aws-sdk
我正在尝试使用适用于.NET的AWS开发工具包中的DynamoDBv2库,通过更新请求将DynamoDB文档中的属性设置为空列表。
我尝试了显而易见的更新表达式,但没有成功:
// Fails, expression attribute values cannot contain an empty list
...
ExpressionAttributeValues = new Dictionary<string, AttributeValue> {
{ ":empty", new AttributeValue { L = new List<AttributeValue> { } } },
},
UpdateExpression = "SET #P.MyList = :empty",
...
Run Code Online (Sandbox Code Playgroud)
我该如何实现?
小智 5
深入研究AWS开发工具包源代码后,我找到了答案。关键是该IsLSet属性是可设置的。这将调用以下代码:
public static void SetIsSet<T>(bool isSet, ref List<T> field)
{
if (isSet)
field = new AlwaysSendList<T>(field);
else
field = new List<T>();
}
Run Code Online (Sandbox Code Playgroud)
在确定是否初始化AttributeValue时,将使用以下代码:
public static bool GetIsSet<T>(List<T> field)
{
if (field == null)
return false;
if (field.Count > 0)
return true;
var sl = field as AlwaysSendList<T>;
if (sl != null)
return true;
return false;
}
Run Code Online (Sandbox Code Playgroud)
这也说明了为什么使用new AttributeValue { L = new List<AttributeValue> { } }没有达到预期的效果-当方法Count为0时,此方法将返回false。但是,AlwaysSendList如果设置了IsLSet属性,则对特殊类型的检查将返回true 。
回到您的代码,答案是使用以下命令:
...
ExpressionAttributeValues = new Dictionary<string, AttributeValue> {
{ ":empty", new AttributeValue { IsLSet = true } },
},
UpdateExpression = "SET #P.MyList = :empty",
...
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1822 次 |
| 最近记录: |