P
P
Peter2019-04-12 20:18:16
C++ / C#
Peter, 2019-04-12 20:18:16

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; }
    }

Next, I create a simple manager class that would populate my list of users.
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()
                {
                           //А вот здесь сама проблема. Не могу заполнить список.
                })
            });
        }
    }

The problem is the following. Can I create a list within a list? What is the task. There is an abstract list of users.
Each user has a certain amount of account data. There can be many accounts and one user.
And is such a thing even possible? So that a list element could have its own list.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander, 2019-04-12
@Morpheus_God

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.
} )
});

Do not do it this way. It's unreadable.
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 question

Ask a Question

731 491 924 answers to any question