I have this class which contains int array
public class Combination { public int[] CombinationSet { get; set; } } There is static List with many instances of this class
public static List<Combination> Combinations = new List<Combination>(); Now I need methods to find combinations on that list so far I have
For 2
public static List<Combination> FindCombinations(int x,int y) { if (x == y) { return Combinations.Where( lenght => lenght.CombinationSet.Length == 2) .Where( data => (data.CombinationSet[0] == x && data.CombinationSet[1] == y) || (data.CombinationSet[1] == x && data.CombinationSet[0] == y) ).ToList(); } else { return Combinations.Where( lenght => lenght.CombinationSet.Length == 2) .Where(data => data.CombinationSet.Contains(x) && data.CombinationSet.Contains(y) ).ToList(); } } Example : if list contains sets : { 1 , 2} , { 1, 3} , { 1 , 2}
and you would call FindCombination(1,2), you would get back list with two instances
It is working fine however for 4 parameters it would be over 24 rows in else statement. I need maximum of 4 I just wonder if there is some more clever way of doing this.
for 3 it looks like this
public static List<Combination> FindCombinations(int x, int y,int z) { if(x == y || x == z || y == z) { return Combinations.Where( lenght => lenght.CombinationSet.Length == 3). Where( inner => ( ( inner.CombinationSet[0] == x && inner.CombinationSet[1] == y && inner.CombinationSet[2] == z) || (inner.CombinationSet[0] == x && inner.CombinationSet[2] == y && inner.CombinationSet[1] == z) || (inner.CombinationSet[1] == x && inner.CombinationSet[0] == y && inner.CombinationSet[2] == z) || (inner.CombinationSet[1] == x && inner.CombinationSet[2] == y && inner.CombinationSet[0] == z) || (inner.CombinationSet[2] == x && inner.CombinationSet[0] == y && inner.CombinationSet[1] == z) || (inner.CombinationSet[2] == x && inner.CombinationSet[1] == y && inner.CombinationSet[0] == z) )).ToList(); } else { return Combinations.Where( length => length.CombinationSet.Length == 3 ).Where(data => data.CombinationSet.Contains(x) && data.CombinationSet.Contains(y) && data.CombinationSet.Contains(z) ).ToList(); } }
FindCombinationsmethod that takes in an array of combination, not just x, y or z if its for 3