简体   繁体   中英

How to convert List<Tuple<string, int>> to string[]?

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

You can do this:

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM