I am using C# windows form
我正在使用C#窗体
I have a List of arrays from a function in a class and I called the function into the form the function returned a List of arrays, how do i get the value of the arrays?
我有一个来自类中函数的数组列表,我将函数调用为函数返回数组列表的形式,如何获取数组的值?
Here is my List of array code
这是我的数组代码列表
public List<string[]> getAccounts()
{
List<string[]> account = new List<string[]>();
while (*condition*)
{
string[] users = new string[2];
users[0] = user["firstname"].ToString();
users[1] = user["lastname"].ToString();
account.Add(users);
}
return account;
}
and when i call the function I want to show all the firstname into a listbox as well as the last name into another listbox
当我调用该函数时,我想将所有名字显示在列表框中,并将姓氏显示在另一个列表框中
for (int i = 1; i <= acc.getAccounts().Count; i++)
{
listBoxFirstname.Items.Add(*all the first name from the list*);
}
4 个解决方案
#1
7
Use a lambda expression to iterate through the list and select the first name
使用lambda表达式遍历列表并选择第一个名称
account.ForEach(s => listBoxFirstname.Items.Add(s[0]));
#2
1
Without a lambda expression:
没有lambda表达式:
List<string[]> accounts = acc.getAccounts()
for (int i = 1; i < accounts ; i++)
{
listBoxFirstname.Items.Add(account[i][0]);
listBoxLastname.Items.Add(account[i][1]);
}
#3
0
This should do the job:
这应该做的工作:
List<string> firstNames = account.Select(item => item[0]).ToList();
#4
0
I think will be better to use SelectMany.
我认为使用SelectMany会更好。
listBoxFirstname.Items.AddRange(acc.getAccounts().SelectMany(item=>item[0]))
的AddRange
的SelectMany
EDIT:
编辑:
Sorry, i'm blind, you can do it without Select many - you can just use Select
对不起,我是瞎子,你可以不用选择很多 - 你可以使用Select
listBoxFirstname.Items.AddRange(acc.getAccounts().Select(item=>item[0]))
#1
7
Use a lambda expression to iterate through the list and select the first name
使用lambda表达式遍历列表并选择第一个名称
account.ForEach(s => listBoxFirstname.Items.Add(s[0]));
#2
1
Without a lambda expression:
没有lambda表达式:
List<string[]> accounts = acc.getAccounts()
for (int i = 1; i < accounts ; i++)
{
listBoxFirstname.Items.Add(account[i][0]);
listBoxLastname.Items.Add(account[i][1]);
}
#3
0
This should do the job:
这应该做的工作:
List<string> firstNames = account.Select(item => item[0]).ToList();
#4
0
I think will be better to use SelectMany.
我认为使用SelectMany会更好。
listBoxFirstname.Items.AddRange(acc.getAccounts().SelectMany(item=>item[0]))
的AddRange
的SelectMany
EDIT:
编辑:
Sorry, i'm blind, you can do it without Select many - you can just use Select
对不起,我是瞎子,你可以不用选择很多 - 你可以使用Select
listBoxFirstname.Items.AddRange(acc.getAccounts().Select(item=>item[0]))