我遇到了一個問題,並不確定發生了什麼,希望有人可以幫助!問題與字典項目
我收集來自數據庫的條目並將它們放置在列表集合中,我使用此列表集合來填充「活動」事物的字典,我將該列表用作模板並將「列表」作爲項目都將被操縱,直到一個「主動」事物被刪除,並且一個新的「主動」事件實例被從列表集合中插入到字典中。我看到的問題是列表集合中的項目正在更新以及字典項目。
這對我來說至少是個問題。我可能做了一些可怕的錯誤,希望有人能提供更好的解決方案。
例如:
public class DataEntry
{
public string DataOne { get; set; }
public int DataTwo { get; set; }
}
public static List<DataEntry> dataCollection = new List<DataEntry>();
public static Dictionary<int, DataEntry> ActiveList = new Dictionary<int, DataEntry>();
private static int activeIndex = 0;
public static void LoadListFromDB()
{
dataCollection.Add(new DataEntry() { DataOne = "Lakedoo", DataTwo = 25 });
foreach (DataEntry de in dataCollection)
{
ActiveList.Add(activeIndex++, de);
}
for (int i = 0; i < 5; i++)
{
ActiveList[0].DataTwo -= 2;
}
if (ActiveList[0].DataTwo < 25)
{
ActiveList.Remove(0);
}
foreach (DataEntry de in dataCollection)
{
ActiveList.Add(activeIndex++, de);
}
}
作爲參考..... –
呃,我不清楚你想要達到什麼目的。 –
而不是每次查詢數據庫的數據模型,我只是想把它放在一個集合中,然後用一個可以操縱的數據的「實例」填充我的字典,直到不再需要該實例,然後插入字典中原始數據的新實例。 – lakedoo