Search data from related tables

Asked

Viewed 729 times

2

I know that to return the values of a directly related entity in the bank is done so with LINQ:

List<Usuario> lista = banco.Usuario.Include("Tipo").ToList();

But how can I get the data from another table related to the "Type" table? For example, the table "Type" has a relationship with the table "Situation" to which the table "User" does not know.

Summarizing the relationship is like this: "User" with "Type" and "Type" with "Situation". I need the data from "Situation" coming along also.

Dbcontext

public partial class BancoDeHorasEntities : DbContext
{
    public BancoDeHorasEntities()
        : base("name=BancoDeHorasEntities")
    {
    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        throw new UnintentionalCodeFirstException();
    }

    public DbSet<Usuario> Usuario { get; set; }
    public DbSet<Tipo> Tipo { get; set; }
    public DbSet<Situacao> Situacao { get; set; }
    public DbSet<sysdiagrams> sysdiagrams { get; set; }
}

Usuario.Cs

public partial class Usuario
{
    public int Id { get; set; }
    public Nullable<int> IdPessoa { get; set; }
    public string Descricao { get; set; }
    public Nullable<int> IdTipo { get; set; }

    public virtual Tipo Tipo { get; set; }
}

Tipo.Cs

 public partial class Tipo
 {
    public Tipo()
    {
        this.Usuarios = new HashSet<Usuario>();
    }

    public int Id { get; set; }
    public Nullable<int> IdSituacao { get; set; }
    public string Descricao { get; set; }

    public virtual Situacao Situacao { get; set; }
    public virtual ICollection<Usuario> Usuarios { get; set; }
}

Situation.Cs

public partial class Situacao
{
    public Situacao()
    {
        this.Tipos = new HashSet<Tipo>();
    }

    public int Id { get; set; }
    public string Nome { get; set; }
    public string Descricao { get; set; }

    public virtual ICollection<Tipo> Tipo { get; set; }
}

Modeling of the Database

Modelagem do banco de dados

In short the problem is: how to obtain, also, the data from the table "Situation" when searching "User".

  • Why his OnModelCreating fires a UnintentionalCodeFirstException?

  • I don’t really know. The project was created by another and when I started using it it was like this.

1 answer

3


You can add other include to his expression LINQ

List<Usuario> lista = banco.Usuario
                           .Include("Tipo")
                           .Include("NomeTabela")
                           .ToList();

Edit

After seeing its entities:

Using LINQ:

List<Usuario> lista = banco.Usuario.Include(u => u.Tipo.Situacao).ToList;  

Using string:

List<Usuario> lista = banco.Usuario.Include("Tipo.Situacao").ToList;

Other examples

  • However, in the case of your reply, it will only serve for tables that are directly related to "User". And this is not my case.

  • I need the data from the table "Situation" which is related to "Type" which is related to "User".

  • It still wasn’t. It gives a build error. The User entity is of the type DbSet<> and not IQueryable<>, that must be it.

  • I found a solution here. I will post. Thanks @ramaral

  • Post also your Dbcontext.

  • By the way, post the classes User, Type and Situation.For my solution to make no mistake, I think they have to have navigation Property that define these relations.

  • This solution does not involve my problem, because I need data from the table that is in a 2nd level of relationship with the table "User", which in my case is the table "Situation".

  • Did you take any tests?

  • 1

    I will post the image of the bank modeling, I think it will be clearer for you.

  • I think it will be so: List<Usuario> lista = banco.Usuario.Include(u => u.Tipo.Situacao).ToList;

  • As the documentation you are correct regarding the use of Include class DbExtensions. But we come across a Include inherited from DbQuery<TResult> in my case, and your reply a Include class DbExtensions. So do not compile.

  • Yes I noticed it today when I went back to analyzing my answer, however Include dbquery<Tresult> should compile as well. See here

Show 8 more comments

Browser other questions tagged

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