Ich habe zwei Listen mit mehreren Daten in jeder Liste. Und brauchen unterhalb der Ausgabe, die Kombinationswerte der Liste (n) wären. HierGeneriere spezifische Kombination in C#, die meine erwartete Ausgabe erzeugt
Example1:
List1: {"A","B","C"}
List2: {"D","E"}
Expected Output Combinations:
{"A"}
{"B"}
{"C"}
{"D"}
{"E"}
{"A,D"}
{"A,E"}
{"B,D"}
{"B,E"}
{"C,D"}
{"C,E"}
Example2:
List1: {"A","B"}
List2: {"D"}
Expected Output Combinations:
{"A"}
{"B"}
{"D"}
{"A,D"}
{"B,D"}
ist Code-Schnipsel, die ich versuche:
public static IEnumerable<IEnumerable<T>> Combinations<T>(this IEnumerable<T> source, int n)
{
if (n == 0) yield return Enumerable.Empty<T>();
int count = 1;
foreach (T item in source)
{
foreach (var innerSequence in source.Skip(count).Combinations(n - 1))
{
yield return new T[] { item }.Concat(innerSequence);}count++;
}
}
}
public static IEnumerable<IEnumerable<T>> AllCombinations<T>(this IList<T> source)
{
IEnumerable<IEnumerable<T>> output = Enumerable.Empty<IEnumerable<T>>();
for (int i = 0; i < source.Count; i++)
{
output = output.Concat(source.Combinations(i));
}
return output;
}
...
List<string> lstCodes = new List<string>();
List<string> lstCodeOut = new List<string>();
if (!String.IsNullOrEmpty(txtbox1.Text))
lstCodes.AddRange(txtbox1.Text.Split(',').ToList());
if (!String.IsNullOrEmpty(txtbox2.Text))
lstCodes.AddRange(txtbox2.Text.Split(',').ToList());
if (!String.IsNullOrEmpty(txtbox3.Text))
lstCodes.AddRange(txtbox3.Text.Split(',').ToList());
foreach (var sequence in lstCodes.AllCombinations())
{
lstCodeOut.Add(string.Join(",", sequence));
}
txtBox.Text = string.Join(Environment.NewLine, lstCodeOut);
Sie können mit einer 'for' Schleife oder einer' foreach' beginnen, später versuchen Sie einige LINQ. Was hast du bisher und wo bist du stecken geblieben? Beachten Sie, dass StackOverflow kein kostenloser Code-Schreibdienst ist. –
Lernen Sie Grundlagen, bevor Sie fragen. –
Hier ist Code-Schnipsel, die ich – Ambi