我有一个如下所示的设备属性:
“{状态:{ 属性1:“111”, 属性2:“222” } }“
如何使用 C# TwinCollection 类仅更新 Property2?
我使用下面的代码,然后“Property2”属性lastUpdated 值发生变化,即使它没有更新。
var status = new TwinCollection();
status["Property1"] = "111";
var reportedProperties = new TwinCollection();
reportedProperties["status"] = status;
await deviceClient.UpdateReportedPropertiesAsync(reportedProperties);
这个答案是假设您的意思是“更新 Property1”,因为这就是您的代码示例所指出的。
我刚刚创建了一个新的西欧物联网中心,使用您指定的设备孪生创建了一个新设备(“状态”中带有小写的“s”,您的代码指定了小写,但您的问题提到了大写)。
然后我运行了您的代码,生成的元数据是:
"$metadata": {
"$lastUpdated": "2023-08-20T10:05:51.8902538Z",
"status": {
"$lastUpdated": "2023-08-20T10:05:51.8902538Z",
"Property1": {
"$lastUpdated": "2023-08-20T10:05:51.8902538Z"
},
"Property2": {
"$lastUpdated": "2023-08-20T10:05:40.8567787Z"
}
}
}
请注意,
$lastUpdated
在所有情况下都发生了变化,但 Property2
没有变化。