使用C#中的Tuple类返回多个列表

时间:2016-05-11 05:29:27

标签: c#

我有一个课程Helper,有2个方法

public static List<string> GetCountryName()
{
    List<string> CountryName = new List<string>();
    using (var sr = new StreamReader(@"Country.txt"))
    {
        string line;
        while ((line = sr.ReadLine()) != null)
        {
            int index = line.LastIndexOf(" ");
            CountryName.Add(line.Substring(0, index));
        }
    }
    return CountryName;
}

public static List<string> GetCountryCode()
{
    List<string> CountryCode = new List<string>();
    using (var sr = new StreamReader(@"Country.txt"))
    {
        string line;
        while ((line = sr.ReadLine()) != null)
        {
            int index = line.LastIndexOf(" ");
            CountryCode.Add(line.Substring(index + 1));
        }
    }
    return CountryCode;
}

我将这些返回值与我的WPF ComboBox绑定如下

ddlCountryName.ItemsSource = Helper.GetCountryName();
ddlCountryCode.ItemsSource = Helper.GetCountryCode();

我想在一个方法中返回这些List并通过这些链接

Tuple Class

https://stackoverflow.com/a/10278769

完成后,我尝试这样做,但无法从第3行开始正确Tuple<List<string>> CountryName = new Tuple<List<string>>

public static Tuple<List<string>,List<string>> GetAllData()
{
    Tuple<List<string>> CountryName = new Tuple<List<string>>
    List<string> CountryCode = new List<string>();
    using (var sr = new StreamReader(@"Country.txt"))
    {
        string line;
        while ((line = sr.ReadLine()) != null)
        {
            int index = line.LastIndexOf(" ");
            CountryName.Add(line.Substring(0, index));
            CountryCode.Add(line.Substring(index + 1));
        }
    }
    return CountryName;
    return CountryCode;
}

请帮助我明智地返回列表项,并按照下面的代码

绑定ItemsSource
ddlCountryName.ItemsSource = Helper.GetCountryName();

2 个答案:

答案 0 :(得分:1)

Tuple是一个包含两个或更多值的包,这些值的类型是在创建Tuple对象时通过通用参数指定的。您正在创建单个值的Tuple,这没有任何意义。尝试创建它:

Tuple<List<string>, List<string>> CountryName = new Tuple<List<string>, List<string>>();

另请注意,函数不能包含多个return语句。您应该将两个列表添加到Tuple对象中,然后一次性返回。

你的最终功能将是这样的(保留你现有的两个函数并创建一个调用它们的新函数):

public static Tuple<List<string>,List<string>> GetAllData()
{
  return new Tuple<List<string>, List<string>>(GetCountryName(), GetCountryCode());
}

另一种方法:

public static List<Tuple<string, string>> GetAllData()
{
  List<Tuple<string, string>> Result = new List<Tuple<string, string>>();

  using (var sr = new StreamReader(@"Country.txt"))
  {
    string line;
    while ((line = sr.ReadLine()) != null)
    {
        int index = line.LastIndexOf(" ");
        var Name = line.Substring(0, index);
        var Code = line.Substring(index + 1);
        Result.Add(new Tuple<string, string>(Name, Code));
    }
  }

  return Result;
}

在第一种情况下,您直接绑定到元组成员:

ddlCountryName.ItemsSource = Result.Item1;
ddlCountryCode.ItemsSource = Result.Item2;

在第二种情况下,您可以进一步使用linq从返回的对象中获取单个列表:

ddlCountryName.ItemsSource = Result.Select(x => x.Item1).ToArray();
ddlCountryCode.ItemsSource = Result.Select(x => x.Item2).ToArray();

答案 1 :(得分:1)

试试这个(未经测试)

public static Tuple<List<string>, List<string>> GetAllData()
{
    List<string> CountryName = new List<string>();
    List<string> CountryCode = new List<string>();
    using (var sr = new StreamReader(@"Country.txt"))
    {
        string line;
        while ((line = sr.ReadLine()) != null)
        {
            int index = line.LastIndexOf(" ");
            CountryName.Add(line.Substring(0, index));
            CountryCode.Add(line.Substring(index + 1));
        }
    }
    return new Tuple<List<string>, List<string>>(CountryName,CountryCode);
}