2013-06-06 5 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 }; 
    } 
} 

목록에 결과 아래 변환해야합니다. 난 당신이 정신적으로 Tstring로 교체 할 수 있습니다, lstShuffle 구현 IEnumerable<string> 때문에 문자열 목록 중 하나

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

에러하지만하지 의미 : 당신은 IEnumerable<IEnumerable<string>> GetPermutations(IEnumerable<string> items)를 호출하고 있습니다.

알렉이 말한대로

, SelectMany(x => x)IEnumerable<T>IEnumerable<IEnumerable<T>>를 평평하게 할 수있는 가장 쉬운 방법이다.

관련 문제