search by ID for each item in the list

Asked

Viewed 205 times

1

I have a class called Attending. in it I have the following attributes:

public class Atendimento
{

    public Guid AtendimentoId { get; set; }
    public Guid? ResponsavelPorAtender { get; set; }
    public Guid? AdmQueAutorizou { get; set; }

    public virtual Usuario Usuario { get; set; }
    public Guid IdUsuario { get; set; }

}

the attribute public Guid? ResponsavelPorAtender and public Guid? AdmQueAutorizou are the users tbm, in the same way as the public Guid IdUsuario when I make a query, which searches all calls, as I do to assign the name of the user, to for example:public Guid? ResponsavelPorAtender as regards the public Guid IdUsuario I just use the include.

public IEnumerable<Atendimento> ObterTodos()
{
  return Db.AtendimentosDb.Include("Usuario");
}

and show in my Index Asssim:

<table 
    <thead style="width:100%">
      <tr>
        <th>
         Aberto Por:
        </th>
        <th>
        Fechado Por:
        </th>

        </tr>
    </thead>
     <tbody>
        @foreach (var item in Model)
          {

           <tr>
             <td>
               @Html.DisplayFor(modelItem => item.Usuario.Nome)
               </td>
               <td>
                 @Html.DisplayFor(modelItem => item.ResponsavelPorAtender )
                </td>
            </tr>
           }
    </tbody>
</table>

Redeemable by meeting is the ID of the user you have anticipated. how do I search for each item on that list?

User class:

public class Usuario
    {

        public Guid UsuarioId { get; set; }
        public string Nome { get; set; }
        public string Cpf { get; set; }
        public string Senha { get; set; }
        public TipoDeUsuario TipoDeUsuario { get; set; }
    }
  • How’s your relationship? Could you add your class Usuario?

  • I edited, and I put the user class

1 answer

3

Well, first you have to map your entities. In the following way:

public class Atendimento
{
    public Guid AtendimentoId { get; set; }

    public Guid? ResponsavelPorAtender { get; set; }

    public Guid? AdmQueAutorizou { get; set; }

    public virtual Usuario Usuario { get; set; }

    public Guid IdUsuario { get; set; }

    public virtual Usuario Usuario{ get; set;} // Aqui foi criado uma entidade de navegação 
}

And Usuario have related to the list of calls:

public class Usuario
{
    public Guid UsuarioId { get; set; }
    public string Nome { get; set; }
    public string Cpf { get; set; }
    public string Senha { get; set; }
    public TipoDeUsuario TipoDeUsuario { get; set; }

    public virtual ICollection<Atendimento> Atendimentos { get; set; } // Aqui foi criado uma entidade de navegação para uma lista de atendimento
}

Thus, the EntityFramework already know that these data are related, and making the query using the Include is converted to an SQL query bringing the records correctly

public IEnumerable<Atendimento> ObterTodos()
{
  return Db.AtendimentosDb
           .Include(Atd => Atd.Usuario)
           .ToList();
}

Browser other questions tagged

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