我真的不明白这件事.我需要将以下结果转换为List
private void generateKeywords_Click(object sender, RoutedEventArgs e)
{
string srText = new TextRange(
txthtmlsource.Document.ContentStart,
txthtmlsource.Document.ContentEnd).Text;
List<string> lstShuffle = srText.Split(' ')
.Select(p => p.ToString().Trim().Replace("\r\n", ""))
.ToList<string>();
lstShuffle = GetPermutations(lstShuffle)
.Select(pr => pr.ToString())
.ToList();
}
public static IEnumerable<IEnumerable<T>> GetPermutations<T>(
IEnumerable<T> items)
{
if (items.Count() > 1)
{
return items
.SelectMany(
item => GetPermutations(items.Where(i => !i.Equals(item))),
(item, permutation) => new[] { item }.Concat(permutation));
}
else
{
return new[] { items };
}
}
下面这一行失败,因为我无法正常转换.我的意思是不是错误,但也不是字符串列表
lstShuffle = GetPermutations(lstShuffle).Select(pr => pr.ToString()).ToList();
解决方法:
对于任何IEnumerable< IEnumerable< T>>我们可以简单地调用SelectMany.
例:
IEnumerable<IEnumerable<String>> lotsOStrings = new List<List<String>>();
IEnumerable<String> flattened = lotsOStrings.SelectMany(s => s);