// Spludlow Software // Copyright © Samuel P. Ludlow 2020 All Rights Reserved // Distributed under the terms of the GNU General Public License version 3 // Distributed WITHOUT ANY WARRANTY; without implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE // https://www.spludlow.co.uk/LICENCE.TXT // The Spludlow logo is a registered trademark of Samuel P. Ludlow and may not be used without permission // v1.14 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Data; namespace Spludlow { public class Reporting { /// /// Make all the posible cominations from the supplied values /// Values are Convert.ToString() /// DB Nulls will be ignored /// Values are sorted /// public static string[][] MakeCombinations(DataTable table, string[] columnsNames) { string[][] values = new string[columnsNames.Length][]; for (int index = 0; index < columnsNames.Length; ++index) { HashSet valuesSet = new HashSet(); string columnName = columnsNames[index]; foreach (DataRow row in table.Rows) { if (row.IsNull(columnName) == true) continue; string value = Convert.ToString(row[columnName]); if (valuesSet.Contains(value) == false) valuesSet.Add(value); } List valuesList = valuesSet.ToList(); valuesList.Sort(); values[index] = valuesList.ToArray(); } return MakeCombinations(values); } /// /// Make all the posible cominations from the supplied values /// see TestMakeCombinations() makes it clear /// Can be handy in reporting /// public static string[][] MakeCombinations(string[][] values) { string[][] reverseValues = new string[values.Length][]; for (int index = 0; index < values.Length; ++index) reverseValues[index] = values[(values.Length - index) - 1]; values = reverseValues; List result = new List(); int[] indexes = new int[values.Length]; while (MakeCombinationsAddOne(indexes, 0, values, result) == false) { } return result.ToArray(); } private static bool MakeCombinationsAddOne(int[] indexes, int currentIndex, string[][] values, List result) { if (currentIndex == indexes.Length) return true; if (currentIndex == 0) { string[] resultPart = new string[indexes.Length]; for (int index = 0; index < indexes.Length; ++index) resultPart[(values.Length - index) - 1] = values[index][indexes[index]]; result.Add(resultPart); } ++indexes[currentIndex]; if (indexes[currentIndex] == values[currentIndex].Length) { for (int index = 0; index <= currentIndex; ++index) indexes[index] = 0; return MakeCombinationsAddOne(indexes, currentIndex + 1, values, result); } return false; } public static void TestMakeCombinations() { List values = new List(); values.Add(new string[] { "A", "B", "C", "D", "G" }); values.Add(new string[] { "1", "2", "3", "4", "5", "9", "11", "22", "333" }); values.Add(new string[] { "X", "Y", "Z" }); string[][] combinations = Spludlow.Reporting.MakeCombinations(values.ToArray()); List log = new List(); foreach (string[] part in combinations) { string text = ""; foreach (string data in part) { if (text.Length > 0) text += ", "; text += data; } log.Add(text); } Spludlow.Log.Report("TestMakeCombinations", log.ToArray()); } } }