How to concatenate a list of an object in a property of another object?

0

I have the following template:

public class Usuario
{
    public int idUsuario { get; set; }
    public string Nome { get; set; }
    public string Email { get; set; }
    public string Senha { get; set; }
    public int Permissao { get; set; }
    public List<Evento> Eventos { get; set; }
}

In the database it is many-to-many, ie User - thirdtable - Events.

In my User Application layer I have persistence, and I have a specific ListAll method that does the select of users and calls the TransformaReaderEmListSource () method.

    private List<Usuario> TransformaReaderEmListadeObjeto(MySqlDataReader reader)
    {
        var usuarios = new List<Usuario>();
        while (reader.Read())
        {
            var temObjeto = new Usuario()
            {
                idUsuario = int.Parse(reader["idUsuario"].ToString()),
                Email = reader["email"].ToString(),
                Senha = reader["senha"].ToString(),
                Permissao = Convert.ToInt32(reader["permissao"].ToString())
            };
            usuarios.Add(temObjeto);
        }

        reader.Close();
        return usuarios;

}

Okay, so far, no problem. I want to list the events related to the user, that is, I need to also list the user events in the third table. So I need the User template to know the events it belongs to. Okay.

That is, by logic, correct me if I am wrong, I also need a ListAllEvents () method. However, if I create another Reader Transformer in List, I will be listing only events that belong to a user of Id: x;

So I need to concatenate this list I created from users so that each user has their list of events. fodeu

Can anyone give me the stones path to solve this problem? Or, clarify me.

    
asked by anonymous 08.10.2016 / 19:18

1 answer

0

Good evening,

Next, I do not know what class mapping is like, I did a basic example of how you persist a generic list in an object.

public class Usuario
{
    public int idUsuario { get; set; }
    public string Nome { get; set; }
    public string Email { get; set; }
    public string Senha { get; set; }
    public int Permissao { get; set; }
    public List<Evento> Eventos { get; set; }
}

public class Evento {

    public int id {get; set;}
    public string descricao {get; set;}
}

public class Main {

    // Lista de eventos.
    List<Evento> lista = new List<Evento>();
    // Cria evento.
    Evento evento1 = new Evento();
    evento1.id = 1;
    evento2.descricao = "Festão do peão";

    EVento evento2 = new Evento();
    evento2.id = 2;
    evento2.descricao = "Rock Brasil";

    // Adicionar eventos na lista.
    lista.Add(evento1);
    lista.Add(evento2);

    // Persistir lista de eventos na tabela de usuário.
    Usuario usuario = new Usuario();
    usuario.id = 1;
    usuario.Nome = "João";
    usuario.Email = "[email protected]";
    usuario.Senha = "123";
    usuario.Permissao = 1;
    // Persistindo lista de eventos no objeto de usuário.
    usuario.Eventos = lista;
}
    
12.10.2016 / 02:51