本文介绍了清单<名单,LT结合INT>>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这种类型的目录列表>包含此
I've a List of this type List> that contains this
List<int> A = new List<int> {1, 2, 3, 4, 5};
List<int> B = new List<int> {0, 1};
List<int> C = new List<int> {6};
List<int> X = new List<int> {....,....};
我想有这样的所有组合
I want to have all combinations like this
1-0-6
1-1-6
2-0-6
2-1-6
3-0-6
等。
根据你的是这不可能性使用LINQ来解决?
According to you is This possibile to resolve using Linq?
推荐答案
这是相当类似this答案我给了另一个问题:
It's quite similar to this answer I gave to another question:
var combinations = from a in A
from b in B
from c in C
orderby a, b, c
select new List<int> { a, b, c };
var x = combinations.ToList();
有关可变数量的投入,现在添加了泛型:
For a variable number of inputs, now with added generics:
var x = AllCombinationsOf(A, B, C);
public static List<List<T>> AllCombinationsOf<T>(params List<T>[] sets)
{
// need array bounds checking etc for production
var combinations = new List<List<T>>();
// prime the data
foreach (var value in sets[0])
combinations.Add(new List<T> { value });
foreach (var set in sets.Skip(1))
combinations = AddExtraSet(combinations, set);
return combinations;
}
private static List<List<T>> AddExtraSet<T>
(List<List<T>> combinations, List<T> set)
{
var newCombinations = from value in set
from combination in combinations
select new List<T>(combination) { value };
return newCombinations.ToList();
}
这篇关于清单&LT;名单,LT结合INT&GT;&GT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!