C# 如何将DynamoDB属性设置为空列表?
我试图通过更新请求,使用AWS SDK for.NET中的DynamoDBv2库,将DynamoDB文档中的属性设置为空列表 我尝试过明显的更新表达式,但没有成功:C# 如何将DynamoDB属性设置为空列表?,c#,amazon-dynamodb,aws-sdk,C#,Amazon Dynamodb,Aws Sdk,我试图通过更新请求,使用AWS SDK for.NET中的DynamoDBv2库,将DynamoDB文档中的属性设置为空列表 我尝试过明显的更新表达式,但没有成功: // Fails, expression attribute values cannot contain an empty list ... ExpressionAttributeValues = new Dictionary<string, AttributeValue> { { ":empty", new A
// 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",
...
//失败,表达式属性值不能包含空列表
...
ExpressionAttributeValues=新字典{
{”:空,新属性值{L=新列表{},
},
UpdateExpression=“SET#P.MyList=:empty”,
...
我怎样才能做到这一点 我在深入研究AWS SDK源代码后找到了答案。关键是
IsLSet
属性是可设置的。这将调用以下代码:
public static void SetIsSet<T>(bool isSet, ref List<T> field)
{
if (isSet)
field = new AlwaysSendList<T>(field);
else
field = new List<T>();
}
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;
}
我自己也遇到了这个问题。看起来是一个至少从2018年11月开始存在的bug()。我的解决方法是在设置
L
的值后立即手动设置IsLSet=true
例如,以下是如何创建空列表属性:
ExpressionAttributeValues = new Dictionary<string, AttributeValue>
{
{ ":empty", new AttributeValue { L = new List<AttributeValue>(), IsLSet = true }},
},
UpdateExpression = "SET #P.MyList = :empty",
ExpressionAttributeValues=新字典
{
{”:空,新属性值{L=new List(),IsLSet=true},
},
UpdateExpression=“SET#P.MyList=:empty”,
无论列表是否为空,您都应该能够使用上述语法。例如:
ExpressionAttributeValues = new Dictionary<string, AttributeValue>
{
{ ":MyListOfValues", new AttributeValue { L = listValues, IsLSet = true }},
},
UpdateExpression = "SET #P.MyList = :MyListOfValues",
ExpressionAttributeValues=新字典
{
{“:MyListOfValues”,新属性值{L=listValues,IsLSet=true},
},
UpdateExpression=“SET 35; P.MyList=:MyListOfValues”,
ExpressionAttributeValues = new Dictionary<string, AttributeValue>
{
{ ":MyListOfValues", new AttributeValue { L = listValues, IsLSet = true }},
},
UpdateExpression = "SET #P.MyList = :MyListOfValues",