-2

I am having 100K items in a Dictionary. And I am trying to search 50K items in it. It works but take 2 min which is bad. I tried couple of thing the best I can think is using linq. below is my code. The while loops stops executing after 2 mins.

public static void Day8(Dictionary<string, string> valuePair)
        {
            Dictionary<string, string> valuePairs = valuePair;
            List<string> dataList = new List<string>();
            string line;
            StreamReader file = new StreamReader("2.txt");
            while ((line = file.ReadLine()) != null)
            {
                var result = valuePairs.FirstOrDefault(a => a.Key == line);
                var data = result.Key == null ? "Not found" : $"{result.Key}={result.Value}";
            }

            dataList.ForEach(Console.WriteLine);
            File.WriteAllLines("3.txt", dataList);
        }
maxspan
  • 13,326
  • 15
  • 75
  • 104
  • Without a good [mcve] it's impossible to know for sure what the problem is. However, most likely you are simply using the dictionary object incorrectly. See marked duplicates. – Peter Duniho Oct 13 '19 at 04:43

1 Answers1

3

FirstOrDefault will cause a linear search to happen, negating the purpose of a Dictionary. You should use TryGetValue i.e.

if(valuePairs.TryGetValue(line, out var result))
   // has been found in dict
else
   // not in here
JohanP
  • 5,252
  • 2
  • 24
  • 34