簡體   English   中英

LINQ加入非常大的字典/內存不足異常

[英]LINQ Join on very large dictionaries / out of memory exception

我有兩個字典,它們試圖加入並將匹配的索引保存在單獨的字典中,如下所示:

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數據庫中並在那里進行連接。 謝謝。

我認為您的字典應該是Dictionary <字符串,MatchedPairs>匹配項(不是整數)。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM