如何转换List <tuple <string,int =“”>&gt;字符串[]?

时间:2016-07-01 07:05:57

标签: c# .net arrays

方法

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

std::vector<std::string> strVect; auto pushToVector = [strVect] () { strVect.push_back(std::string("Hi")); }; pushToVector ();

2 个答案:

答案 0 :(得分:4)

你可以这样做:

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

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

答案 1 :(得分:0)

您可以自己轻松完成,例如:

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