2017-01-13 1 views
0

.NET 용 AWS SDK의 DynamoDBv2 라이브러리를 사용하여 업데이트 요청을 통해 DynamoDB 문서의 속성을 빈 목록으로 설정하려고합니다.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", 
... 

가 어떻게 이것을 달성 할 수

내가 성공하지, 명백한 업데이트 표현을 시도했습니다?

답변

2

AWS SDK 소스 코드를 파고 들어 대답을 찾았습니다. 핵심은 IsLSet 속성을 설정할 수 있다는 것입니다. 이 방법 - 의도 한 효과가없는 new AttributeValue { L = new List<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; 
} 

:

public static void SetIsSet<T>(bool isSet, ref List<T> field) 
{ 
    if (isSet) 
     field = new AlwaysSendList<T>(field); 
    else 
     field = new List<T>(); 
} 

당신의 AttributeValue가 초기화 여부를 결정이며, 다음과 같은 코드를 사용 :이 다음 코드를 호출 Count이 0이므로 false를 반환합니다. 그러나 IsLSet 속성을 설정하면 AlwaysSendList 유형의 검사가 true를 반환합니다.

... 
ExpressionAttributeValues = new Dictionary<string, AttributeValue> { 
    { ":empty", new AttributeValue { IsLSet = true } }, 
}, 
UpdateExpression = "SET #P.MyList = :empty", 
... 
위로 코드에

는 대답은 다음과 같은 사용하는 것입니다
관련 문제