C# C中使用元组类的多列表返回#
我有一个带有2个方法的类C# C中使用元组类的多列表返回#,c#,C#,我有一个带有2个方法的类Helper public static List<string> GetCountryName() { List<string> CountryName = new List<string>(); using (var sr = new StreamReader(@"Country.txt")) { string line; while ((line = sr.ReadLine(
Helper
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;
}
我想用一个方法返回这些列表,并浏览了这些链接
经过检查后,我试着这样做,但无法从第3行正确创建它Tuple CountryName=new Tuple
Tuple
是由两个或多个值组成的包,这些值的类型在创建Tuple
对象时通过通用参数指定。您正在创建单个值的元组
,该值没有任何意义。尝试这样创建它:
Tuple<List<string>, List<string>> CountryName = new Tuple<List<string>, List<string>>();
另一种方法:
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;
}
在第二种情况下,您可以进一步执行linq以从返回的对象中获取单个列表:
ddlCountryName.ItemsSource = Result.Select(x => x.Item1).ToArray();
ddlCountryCode.ItemsSource = Result.Select(x => x.Item2).ToArray();
Tuple
是由两个或多个值组成的包,这些值的类型在创建Tuple
对象时通过通用参数指定。您正在创建单个值的元组
,该值没有任何意义。尝试这样创建它:
Tuple<List<string>, List<string>> CountryName = new Tuple<List<string>, List<string>>();
另一种方法:
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;
}
在第二种情况下,您可以进一步执行linq以从返回的对象中获取单个列表:
ddlCountryName.ItemsSource = Result.Select(x => x.Item1).ToArray();
ddlCountryCode.ItemsSource = Result.Select(x => x.Item2).ToArray();
试试这个(未测试)
public静态元组GetAllData()
{
List CountryName=新列表();
列表国家代码=新列表();
使用(var sr=new StreamReader(@“Country.txt”))
{
弦线;
而((line=sr.ReadLine())!=null)
{
int index=line.LastIndexOf(“”);
CountryName.Add(行.子字符串(0,索引));
CountryCode.Add(行.子字符串(索引+1));
}
}
返回新元组(CountryName、CountryCode);
}
试试这个(未测试)
public静态元组GetAllData()
{
List CountryName=新列表();
列表国家代码=新列表();
使用(var sr=new StreamReader(@“Country.txt”))
{
弦线;
而((line=sr.ReadLine())!=null)
{
int index=line.LastIndexOf(“”);
CountryName.Add(行.子字符串(0,索引));
CountryCode.Add(行.子字符串(索引+1));
}
}
返回新元组(CountryName、CountryCode);
}
这给出了错误,因为没有给出与Tuple()的所需形式参数“item1”对应的参数。
最后一个方法是完美的,结果返回Items1和Items2中的数据。但是我如何根据下面的代码将其绑定到列表中ddlcontryname.ItemsSource=Helper.GetAllData()代码>这给出了错误,因为没有给出与Tuple()
所需的形式参数“item1”相对应的参数。最后一个方法是完美的,结果返回Items1和Items2中的数据。但是我如何根据下面的代码将其绑定到列表中ddlcontryname.ItemsSource=Helper.GetAllData()
将它们组合成一个包含名称和代码的单一类,然后使用LINQSelect
语句将它们拆分,这将更有意义。将它们组合成一个包含名称和代码的单一类,然后拆分它们,这将更有意义使用LINQSelect
语句仅用于显示。
ddlCountryName.ItemsSource = Result.Item1;
ddlCountryCode.ItemsSource = Result.Item2;
ddlCountryName.ItemsSource = Result.Select(x => x.Item1).ToArray();
ddlCountryCode.ItemsSource = Result.Select(x => x.Item2).ToArray();
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);
}