2013-06-06 136 views
1

我真的不明白这个T的东西呢。我需要将以下结果转换为列表如何将IEnumerable <IEnumerable <T>>转换为列表<string>?

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(); 
+1

没有侮辱意,但你理解你的代码做了什么? – gunr2171

+1

我不明白GetPermutations部分,因为我没有编码。这就是为什么我问:D @ gunr2171 – MonsterMMORPG

回答

9

对于任何IEnumerable<IEnumerable<T>>,我们可以简单地拨打SelectMany

例子:

IEnumerable<IEnumerable<String>> lotsOStrings = new List<List<String>>(); 
IEnumerable<String> flattened = lotsOStrings.SelectMany(s => s); 
2

由于lstShuffle工具IEnumerable<string>,你可以精神上Tstring替代:你打电话IEnumerable<IEnumerable<string>> GetPermutations(IEnumerable<string> items)

正如Alexi所说,SelectMany(x => x)是将IEnumerable<IEnumerable<T>>拼合成IEnumerable<T>的最简单方法。

相关问题