How to convert IEnumerable<IEnumerable<T>> to List<string>?

MonsterMMORPG picture MonsterMMORPG · Jun 6, 2013 · Viewed 9.1k times · Source

I really don't understand this T thing yet. I need to convert below result to 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 };
    }
}

this line below fails because i am not able to convert properly. i mean not error but not string list either

lstShuffle = GetPermutations(lstShuffle).Select(pr => pr.ToString()).ToList();

Answer

MgSam picture MgSam · Jun 6, 2013

For any IEnumerable<IEnumerable<T>> we can simply call SelectMany.

Example:

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