C# 超大字典上的LINQ连接/内存不足异常

C# 超大字典上的LINQ连接/内存不足异常,c#,linq,C#,Linq,我有两个字典,我正在尝试连接它们,并将匹配索引保存在一个单独的字典中,如下所示: public class MatchedPairs { public List<int> index1; public List<int> index2; public MatchedPairs() { this.index1 = new List<int>();

我有两个字典,我正在尝试连接它们,并将匹配索引保存在一个单独的字典中,如下所示:

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);
}
我收到一封信

内存不足异常

执行此代码时,因为file1Dictionary和file2Dictionary对象中有数百万个条目


我可以做些什么来匹配内存/C中的这些大型对象。我的替代方法是将数据加载到SQL数据库中并在那里进行连接。谢谢。

我认为你的字典应该是dictionary匹配而不是整数

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

如果你能在数据库中完成,这是最好的选择。你是否将其作为64位应用程序运行?实际上,你能将字典加载到内存中吗?抛出内存不足异常的是连接吗?下面是关于类似问题的一些信息。这样预分配可能会解决问题:Dictionary file1Dictionary=newdictionary 40000000;将限制设置为40000000个条目,如果需要增加,请按@Enigmativity建议的64位运行应用程序,同时将gcAllowVeryLargeObjects设置为true检查这如何准确修复OOM异常?