如何将List >转换为string []?

时间:2023-01-22 16:38:27

The method

方法

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;

}

Then in the constructor i'm using it

然后在构造函数中我正在使用它

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

The problem in this case is that se.Run(); should return string[] . And dirs is List<Tuple<string, int>>

这种情况下的问题是se.Run();应该返回string []。而dirs是List >

2 个解决方案

#1


4  

You can do this:

你可以这样做:

var dirs = F(textBox3.Text, 0);

Search_Engine se = new Search_Engine();
se.Run(dirs.Select(item => item.Item1).ToArray());

#2


0  

You can do it easily on your own, like this:

您可以自己轻松完成,如下所示:

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

#1


4  

You can do this:

你可以这样做:

var dirs = F(textBox3.Text, 0);

Search_Engine se = new Search_Engine();
se.Run(dirs.Select(item => item.Item1).ToArray());

#2


0  

You can do it easily on your own, like this:

您可以自己轻松完成,如下所示:

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