我想转换IEnumerable<Contact>为List<Contact>.我怎样才能做到这一点?
绝对的心灵空白.那是其中的一天.但我一直在寻找一种解决方案,以获得一定长度的项目列表的独特组合.例如,给定一个列表[a,b,c]和长度为2,它将返回[a,b] [a,c] [b,c]但不返回[b,a] [c,a] [c ,b]的
为此,我发现了许多代码,但似乎没有一个代码.以下代码似乎最合适,我一直在尝试根据我的需要改变它:
// Returns an enumeration of enumerators, one for each permutation
// of the input.
public static IEnumerable<IEnumerable<T>> Permute<T>(IEnumerable<T> list, int count)
{
if (count == 0)
{
yield return new T[0];
}
else
{
int startingElementIndex = 0;
foreach (T startingElement in list)
{
IEnumerable<T> remainingItems = AllExcept(list, startingElementIndex);
foreach (IEnumerable<T> permutationOfRemainder in Permute(remainingItems, count - 1))
{
yield return Concat<T>(
new T[] { startingElement },
permutationOfRemainder);
}
startingElementIndex += 1;
}
} …Run Code Online (Sandbox Code Playgroud)