我做错了什么,因为之后执行myData的循环更新的静止包含空白IDS对象。为什么不在以下foreach循环中更新myData对象,以及如何修复它?为什么我对象被
我认为这可能是因为我没有被引用传递对象,但增加了一个ref关键字并且还移动到主方法和我仍然示出所述对象不被更新。
其他信息
在foreach循环,用户正在更新对象,但myData的列表并不反映我看到更新被应用到用户对象。
**解决方案** 我并没有创建一个List,而是一个Enumerable,每当我通过一个foreach列表中的myData时,它都拉动了json。添加一个ToList()修复了我的问题。
public class MyData
{
public string ID { get; set; }
public Dictionary<string, string> Properties { get; set; }
}
int index = 0;
// Does not allow me to up, creates an IEnumerable
//IEnumerable<MyData> myData = JObject.Parse(json)["Users"]
// .Select(x => new MyData()
// {
// ID = x["id"].ToString(),
// Properties = x.OfType<JProperty>()
// .ToDictionary(y => y.Name, y => y.Value.ToString())
// });
//Works allows me to update the resulting list.
IEnumerable<MyData> myData = JObject.Parse(json)["Users"]
.Select(x => new MyData()
{
ID = x["id"].ToString(),
Properties = x.OfType<JProperty>()
.ToDictionary(y => y.Name, y => y.Value.ToString())
}).ToList();
foreach (var user in myData) // Also tried myData.ToList()
{
if (string.IsNullOrEmpty(user.ID))
{
user.ID = index.ToString();
user.Properties["id"] = index.ToString();
}
index++;
}
您是否尝试过使用'如果(string.IsNullOrEmpty(user.ID))'呢?你确定ID不是空的(而不是空的)? –
mydata在哪里更新? –
这最后'user.ID'应该可能是'index' –