[英]How to get multiple combination of items from multiple list in c#
我有三個清單
List<string> firstList = new List<string> { "A", "B" };
List<string> secondList = new List<string> { "C", "D", "E" };
List<string> thirdList = new List<string> { "F", "G" };
我想要以上三個列表中的所有組合,例如
ACF
ACG
ADF
ADG
...
我嘗試了SelectMany
和Zip
但是沒有用。
注意:如果我使用lambda表達式獲得所需的輸出,將對您有所幫助。
您可以通過像這樣使用Join
來做到這一點
public class Program
{
static void Main(string[] args)
{
List<string> firstList = new List<string> { "A", "B" };
List<string> secondList = new List<string> { "C", "D", "E" };
List<string> thirdList = new List<string> { "F", "G" };
List<string> result = firstList
.Join(secondList, x => true, y => true, (m, n) => m + n)
.Join(thirdList, a => true, b => true, (a, b) => a + b)
.ToList();
result.ForEach(x => Console.WriteLine(x));
Console.ReadLine();
}
}
輸出:
您需要3個循環:
List<string> combinations = new List<string>();
for(int i=0; i < firstList.Length; i++)
for(int j=0;j < secondList.Length; j++)
for(int k=0;k < thirdList.Length; k++)
combinations.Add(firstList[i]+secondList[j]+thirdList[k]);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.