Select in two tables

Asked

Viewed 292 times

0

Ban.cs
public partial class Ban
    {
        public int IdBan { get; set; }
        public System.DateTime DataBan { get; set; }
        public System.DateTime DataDesban { get; set; }
        public int Usuario_IdUsuario { get; set; }
        public string Motivo { get; set; }

        public virtual Usuario Usuario { get; set; }
    }


Usuario.cs
public partial class Usuario
    {
        public Usuario()
        {
            this.Avaliacao = new HashSet<Avaliacao>();
            this.Ban = new HashSet<Ban>();
            this.Comentario = new HashSet<Comentario>();
            this.Denuncia = new HashSet<Denuncia>();
            this.Login = new HashSet<Login>();
            this.Mensagens = new HashSet<Mensagens>();
            this.Mensagens1 = new HashSet<Mensagens>();
            this.Pedido = new HashSet<Pedido>();
            this.Publicacao = new HashSet<Publicacao>();
            this.UsuarioPermissoes = new HashSet<UsuarioPermissoes>();
        }

        public int IdUsuario { get; set; }
        public string NomeCompleto { get; set; }
        public string Email { get; set; }
        public string Senha { get; set; }
        public string Sexo { get; set; }
        public string Telefone { get; set; }
        public string About { get; set; }
        public string Nickname { get; set; }
        public string Email2 { get; set; }
        public string Endereco { get; set; }
        public string Bairro { get; set; }
        public string Estado { get; set; }
        public string Cep { get; set; }
        public string Celular { get; set; }
        public string FotoPerfil { get; set; }
        public string StatusPerfil { get; set; }
        public Nullable<int> NBans { get; set; }
        public Nullable<bool> Banido { get; set; }
        public System.DateTime DtCadastro { get; set; }
        public System.DateTime DtNasc { get; set; }

        public virtual ICollection<Avaliacao> Avaliacao { get; set; }
        public virtual ICollection<Ban> Ban { get; set; }
        public virtual ICollection<Comentario> Comentario { get; set; }
        public virtual ICollection<Denuncia> Denuncia { get; set; }
        public virtual ICollection<Login> Login { get; set; }
        public virtual ICollection<Mensagens> Mensagens { get; set; }
        public virtual ICollection<Mensagens> Mensagens1 { get; set; }
        public virtual ICollection<Pedido> Pedido { get; set; }
        public virtual ICollection<Publicacao> Publicacao { get; set; }
        public virtual ICollection<UsuarioPermissoes> UsuarioPermissoes { get; set; }
    }

I have an action that returns a list of bans from the site. However, I do not want to return only the data of this table "Ban", I would like to display also the name of the User who was banned, to facilitate the identification of users who were banned. How do I exquisite the user name along with the data from the table "Ban"?

  • You can put in your question how are the models of Ban and User?

  • @Romaniomorrisonmendez In relationship issue? Or you want to see the code of the two classes?

  • The code of the two Models.

  • @Ciganomorrisonmendez I updated. Classes were created automatically by EF. There is some problem?

  • No, not at all.

1 answer

1


It’s no secret:

var ban = contexto.Bans.Include(b => b.Usuario).FirstOrDefault();
var nomeUsuario = ban.Usuario.Nome;
  • Really! Thank you very much.

Browser other questions tagged

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