2015-06-22 98 views
0

我有兩本詞典,我想在一個單獨的字典中加入並保存匹配指數像這樣:LINQ加入上非常大的字典/內存溢出異常

public class MatchedPairs 
    { 
     public List<int> index1; 
     public List<int> index2; 

     public MatchedPairs() 
     { 
      this.index1 = new List<int>(); 
      this.index2 = new List<int>(); 
     } 
    } 

Dictionary<int, string> file1Dictionary = new Dictionary<int, string>(); 
Dictionary<int, string> file2Dictionary = new Dictionary<int, string>(); 

//Fill dictionaries with data from flat files 
//... 

var matchedKeys = file1Dictionary.Join(file2Dictionary, x => x.Value, y => y.Value, (x, y) => new { k1 = x.Key, k2 = y.Key }); 

Dictionary<int, MatchedPairs> matches = new Dictionary<int, MatchedPairs>(); 

foreach (var match in matchedKeys) 
{ 
    matches.index1.Add(match.k1); 
    matches.index2.Add(match.k2); 
} 

我收到

內存溢出異常

執行該代碼時,因爲file1Dictionaryfile2Dictionary對象都有數以百萬計的條目在他們中。

有什麼我可以做的,以便能夠匹配內存中的這些大對象/在C#中。我的替代方案是將數據加載到SQL數據庫中並在那裏進行加入。謝謝。

+2

如果你可以在數據庫中做到這一點,這是最好的選擇。 –

+0

你是否將其作爲64位應用運行? – Enigmativity

+1

其實,你能把字典加載到內存中嗎?它是否引發了內存不足異常? – Enigmativity

回答

-1

我覺得你的字典應該是字典< string,MatchedPairs> matches(not integer)。

class Program 
    { 
     static void Main(string[] args) 
     { 

      Dictionary<int, string> file1Dictionary = new Dictionary<int, string>(); 
      Dictionary<int, string> file2Dictionary = new Dictionary<int, string>(); 

      //Fill dictionaries with data from flat files 
      //... 
      Dictionary<string, List<int>> reverseDict1 = file1Dictionary.Keys.AsEnumerable() 
       .Select(x => new { value = x, keys = file1Dictionary[x] }) 
       .GroupBy(x => x.keys, y => y.value) 
       .ToDictionary(x => x.Key, y => y.ToList()); 

      Dictionary<string, List<int>> reverseDict2 = file1Dictionary.Keys.AsEnumerable() 
       .Select(x => new { value = x, keys = file2Dictionary[x] }) 
       .GroupBy(x => x.keys, y => y.value) 
       .ToDictionary(x => x.Key, y => y.ToList()); 

      Dictionary<string, MatchedPairs> matches = new Dictionary<string, MatchedPairs>(); 
      foreach(string key in reverseDict1.Keys) 
      { 
       matches.Add(key, new MatchedPairs(reverseDict1[key], reverseDict2[key])); 
      } 

     } 
    } 
    public class MatchedPairs 
    { 
     public List<int> index1 { get; set; } 
     public List<int> index2 { get; set; } 

     public MatchedPairs(List<int> l1, List<int> l2) 
     { 
      this.index1 = new List<int>(l1); 
      this.index2 = new List<int>(l2); 
     } 
    } 
+1

這究竟是如何解決OOM異常? – Aron