c# - 如何将 IEnumerable<IEnumerable<T>> 转换为 List<string>?

标签 c# linq ienumerable

我真的还不明白这个T的东西。我需要将以下结果转换为列表

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);

关于c# - 如何将 IEnumerable<IEnumerable<T>> 转换为 List<string>?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16952626/

相关文章:

C# var 有强类型吗?

c# - 正确使用 .NET 并发集合

c# - 使用 LINQ 比较两个列表的更好方法?

c# - Linq XML 不会选择指定的 xml 元素

c# - SortedSet - 存储类对象时的自定义顺序

c# - 如何在 foreach 中隐式转换自定义 IEnumerable?

c# - Visual Studio 2022 Azure 云服务没有 Web 角色

c# - C# 中的简单游戏,仅包含 native 库

c# - 有条件地加入 LINQ?

.net - C++ IEnumerable <int> = class-> method();在WinForms中