Hi I am using this function to calculate all the possible combinations of A List of Object "Ricerca" when I try to calculte the combination of more than 24 elemnts I get the System.OutOfMemory Exception. Is there a way I can solve this problem?
this is the function I use:
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;
}
Thanks for your help
Do you really need to hold them all in memory at once or would it be sufficient to get each combination one at a time so you can do something with it?
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();
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.