如何转换List>string[]

本文关键字:string int 何转换 Tuple 转换 List | 更新日期: 2023-09-27 17:53:52

方法
static List<Tuple<string, int>> F(string root, int rootLevel)
{
    List<Tuple<string, int>> result = new List<Tuple<string, int>>();
    foreach (var item in Directory.EnumerateDirectories(root))
    {
        try
        {
            result.Add(new Tuple<string, int>(item, rootLevel + 1));
            result.AddRange(F(item, rootLevel + 1));
        }
        catch (UnauthorizedAccessException ex)
        {
            string tried = "";
        }
    }
    return result;
}

然后在构造函数中使用它

var dirs = F(textBox3.Text, 0);
var deep = (from d in dirs
            orderby d.Item2 descending
            select d).FirstOrDefault().Item2;
Search_Engine se = new Search_Engine();
se.Run();

本例中的问题是se.Run();应该返回string[]dirsList<Tuple<string, int>>

如何转换List<Tuple<string, int>>string[]

你可以这样做:

var dirs = F(textBox3.Text, 0);
Search_Engine se = new Search_Engine();
se.Run(dirs.Select(item => item.Item1).ToArray());

您可以自己轻松地完成,像这样:

var length = dirs.Count();
string[] array = new string[length];
for(int i = 0; i < length; i++)
{
  array[i] = dirs[i].Item1;
}
//array is now what you want