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

Asked

Viewed 341 times

0

I have the following model:

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 - terceiratabela - Events.

In my Applicationuser layer I have persistence, and I have a specific Listed() method that selects users and calls the Transformeremlistadeobject 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 user related events, IE, I need to list also the user events in the third table. So I need the User model to know the events it belongs to. Ok.

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

So, I need to concatenate this list I created of users so that each user has their own event list. fucked

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

  • I do not know if I understood your idea well, but I could not call a method within this method TransformaReaderEmListadeObjeto to list the events the user is involved in? If you list the users for the events and the evnetos for the user you will loop. When listing users you should list the events related to the user and when listing events, list the users related to the event, but you can’t make it always be loaded both to avoid the loop.

1 answer

0

Good night,

Next, I don’t know what the class mapping looks like, I did a basic example of how you persist a generic list on 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;
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.