David Tunnell
David Tunnell

Reputation: 7532

Dictionary returning wrong value for key

My Dictionary does not appear to be returning the correct value:

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

The issue is occurring at:

BacklogItem Story = BacklogItemDictionary[BacklogScrumID];

Here is the simple class:

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

The value being passed is: -324048562862518297

enter image description here

However, instead of returning the associated BacklogItem object it is returning the very 1st one in the dictionary:

enter image description here enter image description here

The key -324048562862518297 != -922008489156936615 so why is the wrong object being returned?

Edit: How the dictionary is populated:

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

Upvotes: 0

Views: 1151

Answers (1)

StriplingWarrior
StriplingWarrior

Reputation: 156469

This is almost certainly a matter of bad inputs. Either you have lines that match more than one of your if statements (because there aren't line breaks between INSERT statements), or there are multiple insert statements that associate a different dictionary key value with the same ProductScrumId and StoryTitle.

Upvotes: 2

Related Questions