List<string> list1 = new List<string>(){ "Ugly", "Pretty"};
List<string> list2 = new List<string>(){ "Dogs", "Carts", "Pigs"};
List<string> list3 = new List<string>(){ "Rock", "Suck"};
var result = from s1 in list1
from s2 in list2
from s3 in list3
select new[] { s1, s2, s3 };
foreach (var item in result)
{
Console.WriteLine(String.Join(",", item));
}
如果你正在寻找一个更通用的解决方案,不仅为3所列出您可以尝试埃里克利珀的解决方案
foreach (var item in new[] { list1, list2, list3 }.CartesianProduct())
{
Console.WriteLine(String.Join(",", item));
}
public static partial class MyExtensions
{
// Eric Lippert’s Blog
// Computing a Cartesian Product with LINQ
// http://blogs.msdn.com/b/ericlippert/archive/2010/06/28/computing-a-cartesian-product-with-linq.aspx
public static IEnumerable<IEnumerable<T>> CartesianProduct<T>(this IEnumerable<IEnumerable<T>> sequences)
{
// base case:
IEnumerable<IEnumerable<T>> result = new[] { Enumerable.Empty<T>() };
foreach (var sequence in sequences)
{
var s = sequence; // don't close over the loop variable
// recursive case: use SelectMany to build the new product out of the old one
result =
from seq in result
from item in s
select seq.Concat(new[] { item });
}
return result;
}
}
我建议在select语句中使用别的东西,因为目前你有多余的连接,因此内存使用。 – abatishchev
@abatishchev其实不会有问题。当连接2或3个字符串时,编译器会生成对'String.Concat(str1,str2,str3)'的调用,它使用'FastAllocateString'来创建新的字符串。这比在String.Format调用中创建新的StringBuilder效率更高。但在这种特殊情况下,OP希望有空格分隔的字符串,所以这就是为什么应该使用格式的原因。 –
@lazyberezovsky - 我认为同样的事情,并测试它:连接它与'a +“”+ b +“”+ c'大约比'String.Format'快30%。 – JulianR