Reputation: 3
I am using C# windows form
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*);
}
Upvotes: 0
Views: 85
Reputation: 885
I think will be better to use SelectMany.
listBoxFirstname.Items.AddRange(acc.getAccounts().SelectMany(item=>item[0]))
EDIT:
Sorry, i'm blind, you can do it without Select many - you can just use Select
listBoxFirstname.Items.AddRange(acc.getAccounts().Select(item=>item[0]))
Upvotes: 0
Reputation: 17499
This should do the job:
List<string> firstNames = account.Select(item => item[0]).ToList();
Upvotes: 0
Reputation: 2167
Without a lambda expression:
List<string[]> accounts = acc.getAccounts()
for (int i = 1; i < accounts ; i++)
{
listBoxFirstname.Items.Add(account[i][0]);
listBoxLastname.Items.Add(account[i][1]);
}
Upvotes: 1
Reputation: 581
Use a lambda expression to iterate through the list and select the first name
account.ForEach(s => listBoxFirstname.Items.Add(s[0]));
Upvotes: 7