1

I have an issue with int[] array. I have the following definition of EmailIndex dictionary:

Dictionary<string, int[]> EmailIndex = new Dictionary<string, int[]>();

When there are few members in the dictionary's int[] array the TryGetValue returns array into int[] arr and my method works perfectly. But when I tested it with about 1800 members in the array it returns null. I checked the instance of the EmailIndex dictionary that I use in my method and it has all values.

Here is the code

private void SearchByEmail(string email)
    {
        try
        {
            int[] arr;
            EmailIndex.TryGetValue(email, out arr);
            foreach (int entry in arr)
            {
                string fileName = @"C:\Admin\Q\chunkUsers" + entry.ToString() + ".csv";
                var search = File.ReadLines(fileName)
                .Where(line =>
                {
                    line = line.Trim();
                    return line.Contains(email);
                });
                foreach (string line in search)
                {
                    Console.WriteLine(line);
                }
            }
            Console.ReadLine();
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
    }

The behavior is different when the array is tiny. The TryGetValue returns true. When the array grows then TryGetValue returns false.

UPD: there is no problem with TryGetValue. There was an issue with trailing \" in the email that I passed to the method

IgorM
  • 1,348
  • 1
  • 12
  • 28
  • 1
    `TryGetValue` returns `bool` which is used to determine if an item was successfully found or not. You ignore this value, while I am 99% sure it is false in cases when `arr` is `null`. If you don't expect this item to not exist, then just replace this line with `arr = EmailIndex[email];` and it will know when there is no such item found (by exception message). – Yeldar Kurmangaliyev Oct 28 '18 at 18:55
  • 1
    If you put a breakpoint at the line containing `EmailIndex.TryGetValue` and inspect the locals, what are the contents of `EmailIndex`? What you're doing works fine: https://dotnetfiddle.net/IbFjOC – fuglede Oct 28 '18 at 18:59
  • @fuglede, thanks. The contents of EmailIndex is absolutely ok. Exactly what I expect to find there. The list of emails (strings) and the corresponding array of int per every email. Moreover, everything works perfect when the array is tiny. – IgorM Oct 28 '18 at 19:02
  • @YeldarKurmangaliyev, yes it is false. But when I deal with tiny array it is true. Can't figure out what is the root cause – IgorM Oct 28 '18 at 19:05
  • @fuglede, here is how it looks like at the runtime: https://youtu.be/FiQRhWWDZyA – IgorM Oct 28 '18 at 19:22
  • 1
    @IgorM: You don't show what the value of `email` is in the video, so it is impossible to tell what you are comparing with. However, just looking at it, I'd wager that you're not including the leading and trailing `\"`s in `email`. – fuglede Oct 28 '18 at 19:31
  • And I assume that the 11 people whose emails you show off in the video have agreed to having their addresses shared on StackOverflow? – fuglede Oct 28 '18 at 19:32
  • @fuglede, this is just a test DB with randomly generated fake email addresses. No need to worry about privacy. Make a search and you'll find these email addresses in many other examples. Here is one of them: https://anaconda.org/sean/untitled-ipynb/notebook – IgorM Oct 28 '18 at 19:37
  • 1
    @fuglede, thank you. The trailing `\"` was the issue – IgorM Oct 28 '18 at 19:54
  • 2
    Great, and apologies for jumping on you regarding the addresses. – fuglede Oct 29 '18 at 15:17

0 Answers0