gpt4 book ai didi

c# - 对象字典返回不正确的值

转载 作者:行者123 更新时间:2023-11-30 13:44:59 25 4
gpt4 key购买 nike

我正在构建一个程序来解析纯文本数据库文件并创建关联,以便我可以从系统中导出附件。

        Dictionary<string, string> AttachementsDictionary = new Dictionary<string, string>();
Dictionary<string, string> BacklogLookupDictionary = new Dictionary<string, string>();
Dictionary<string, BacklogItem> BacklogItemDictionary = new Dictionary<string, BacklogItem>();
BacklogItem CurrentBacklogItem = new BacklogItem();
Dictionary<string, Product> ProductDictionary = new Dictionary<string, Product>();
Product CurrentProduct = new Product();
string StoryScrumId = "";

Console.WriteLine("Loading Data From ScrumWorks Database");
//You need access to this server to run this locally.
string[] lines = System.IO.File.ReadAllLines(@"\\dxScrum01v\ScrumWorksPro\scrumworks\data\hypersonic\scrumworks.script");

Console.WriteLine("Creating Object Dictionaries");
foreach (string line in lines)
{
if (line.Contains("INSERT INTO ATTACHMENT"))
{
AttachementsDictionary.Add(line.Split('(', ',')[1], line.Split('\'', '\'')[1]);
}
if (line.Contains("INSERT INTO BACKLOGITEM_ATTACHMENT"))
{
if (!BacklogLookupDictionary.ContainsKey(line.Split('(', ',')[1]))
{
BacklogLookupDictionary.Add(line.Split(',', ')')[1], line.Split('(', ',')[1]);

}
}
if (line.Contains("INSERT INTO BACKLOGITEMEJB VALUES"))//-324048562862518297
{
CurrentBacklogItem.ProductScrumId = Regex.Split(line, ",(?=(?:[^']*'[^']*')*[^']*$)")[7];
CurrentBacklogItem.StoryTitle = Regex.Split(line, ",(?=(?:[^']*'[^']*')*[^']*$)")[12];
string test = line.Split('(', ',')[1];
BacklogItemDictionary.Add(line.Split('(', ',')[1], CurrentBacklogItem);
}
if (line.Contains("INSERT INTO PRODUCTEJB VALUES"))
{
CurrentProduct.ProductName = line.Split('\'', '\'')[1].Replace(@"'", string.Empty);
CurrentProduct.StoryPrefix = Regex.Split(line, ",(?=(?:[^']*'[^']*')*[^']*$)")[4].Replace(@"'", string.Empty);

ProductDictionary.Add(line.Split('(', ',')[1], CurrentProduct);
}
}

上面是创建字典的代码,我用它来处理SQL数据库中的关联关系。

当它运行时,它显示为 BacklogItemDictionary 正在正确填充:

添加到字典中的每个积压项目都是唯一的:

enter image description here

enter image description here

然而,一旦 foreach 循环结束,每个值都会更改为相同的错误值:

enter image description here

每个 BacklogItem 对象的每个键都与上面相同。我要做什么才能使词典以这种方式运行?我该如何解决?

最佳答案

与其在循环之前创建一个 BacklogItem 实例,然后再重用,不如在循环中创建 BacklogItem。否则,您只是在每次迭代中更改单个实例的属性,实际上最后一行获胜。

所以代替:

BacklogItem CurrentBacklogItem = new BacklogItem();
foreach (string line in lines)
{
.... (changes properties everytime and adds the same instance to the dictionary)
}

这...

foreach (string line in lines)
{
BacklogItem CurrentBacklogItem = new BacklogItem();
.... (initializes this object and adds it to the dictionary)
}

关于c# - 对象字典返回不正确的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33370791/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com