[英]c# System.OutOfMemoryException How can I solve it?
嗨,当我尝试计算超过24个元素的组合时,我使用此函数来计算对象“ Ricerca”列表的所有可能组合,但出现System.OutOfMemory异常。 有什么办法可以解决这个问题?
这是我使用的功能:
private static List<List<Ricerca>> GetAllCombos(List<Ricerca> list)
{
int comboCount = (int)Math.Pow(2, list.Count) - 1;
List<List<Ricerca>> result = new List<List<Ricerca>>();
for (int i = 1; i < comboCount + 1; i++)
{
// make each combo here
result.Add(new List<Ricerca>());
for (int j = 0; j < list.Count; j++)
{
if ((i >> j) % 2 != 0)
result.Last().Add(list[j]);
}
}
return result;
}
谢谢你的帮助
您是否真的需要一次将它们全部保存在内存中?还是一次获取每种组合就足够了,以便您可以做点什么?
private static IEnumerable<List<Ricerca>> GetAllCombos(IReadOnlyCollection<Ricerca> list)
{
var comboCount = (int)Math.Pow(2, list.Count) - 1;
for (var i = 1; i < comboCount + 1; i++)
{
// make each combo here
yield return list.Where((t, j) => (i >> j) % 2 != 0).ToList();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.