字典为键

时间:2015-10-22 18:36:22

标签: c# .net dictionary

我的词典似乎没有返回正确的值:

        foreach (string line in lines)
        {
            if (line.Contains("INSERT INTO ATTACHMENT VALUES"))
            {
                string AttachementID = line.Split('(', ',')[1];
                string FileName = AttachementsDictionary[AttachementID];
                string BacklogScrumID = BacklogLookupDictionary[AttachementID];
                BacklogItem Story = BacklogItemDictionary[BacklogScrumID];
                Product Product = ProductDictionary[Story.ProductScrumId];
            }
        }

问题发生在:

BacklogItem Story = BacklogItemDictionary[BacklogScrumID];

这是一个简单的类:

public class BacklogItem
{
    public string ProductScrumId { get; set; }
    public string StoryTitle { get; set; }
}

传递的值是:-324048562862518297

enter image description here

但是,它不是返回关联的BacklogItem对象,而是返回字典中的第一个:

enter image description here enter image description here

键-324048562862518297!= -922008489156936615那么为什么要返回错误的对象?

编辑:如何填充字典:

        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];
                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);
            }
        }

1 个答案:

答案 0 :(得分:2)

这几乎肯定是输入不好的问题。要么您的行匹配多个if语句(因为INSERT语句之间没有换行符),或者有多个插入语句将不同的字典键值与相同的ProductScrumId关联起来StoryTitle。