Answer the question
In order to leave comments, you need to log in
How to add List to List?
Good evening.
Help with problem solving. Perhaps it is simple, but I have not thought of it yet :)
There are a couple of such models.
public class UserModel
{
public string FirstName { get; set; }
public string LastName { get; set; }
public List<AccountModel> Accounts { get; set; }
}
public class AccountModel
{
public string AccountType { get; set; }
public string Email { get; set; }
public string Password { get; set; }
}
public class UserManager
{
private List<UserModel> _userList;
public UserManager()
{
_userList = new List<UserModel>();
PopulateList();
}
private void PopulateList()
{
// Этот список создан для проверки.
var lst = new List<AccountModel>();
lst.Add(new AccountModel()
{
AccountType = "Yandex",
Email = "[email protected]",
Password = "12312312",
});
_userList.Add(new UserModel() {
FirstName = "Jack",
LastName = "The Sparrow",
Accounts = new List<AccountModel>().Add(new AccountModel()
{
//А вот здесь сама проблема. Не могу заполнить список.
})
});
}
}
Answer the question
In order to leave comments, you need to log in
Can I create a list within a list?
class Foo{
public List<int> Digits = new List<int>();
}
var bar = new List<Foo>();
bar[0].Digits[0] = 5;
_userList.Add(new UserModel() {
FirstName = "Jack",
LastName = "The Sparrow",
Accounts = new List().Add(new AccountModel()
{
//Here's the problem. I can't populate the list.
} )
});
var user_accounts = new List<AccountModel>();
user_accounts.Add(new AccountModel()
{
AccountType = "Yandex",
Email = "[email protected]",
Password = "12312312",
});
user_accounts.Add(new AccountModel()
{
AccountType = "Yandex",
Email = "[email protected]",
Password = "12312312",
});
user_accounts.Add(new AccountModel()
{
AccountType = "Yandex",
Email = "[email protected]",
Password = "12312312",
});
var new_user = new UserModel() {
FirstName = "Jack",
LastName = "The Sparrow"}
new_user.Accounts = user_accounts;
_userList.Add(new_user);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question