Load records from different tables as Anonymous type Entity framework

Asked

Viewed 152 times

1

How can I load data from multiple tables in the Entity Framework? By RAW Query I get normal, but by the Entity Framework I’m not getting to the point.

What I need is to select the last entry that contains a specific product (I will pass the ID) ordered by the Decreasing Dataregistration (identify that is the last one) and together select the PRODUCT object I passed the ID?

Obs: I removed the Annotations and mapping to decrease the post size!

I access the entrance through my:

contexto.Entrada.Lista().Where(...).OrderByDescending(m => m.DataCadastro).FirstOrDefault();

Classes:

class Entrada {
    public int Id { get; set; }
    public DateTime DataCadastro { get; set; }      
    public virtual ICollection<ItemEntrada> Items;
}    
class Produto {
    public int Id { get; set; }     
    public string Nome { get; set; }
}    
class ItemEntrada {
    public int Id { get; set; }     
    public int EntradaId { get; set; }      
    public int ProdutoId { get; set; }      
    public virtual Produto Produto { get; set; };       
    public virtual Entrada Entrada { get; set; };
}
  • Put Apolo to your SQL that you put in your RAW?

  • I did with Join but I cannot use RAW SQL need to use Entity framework in the project.

  • Apolo, I want to see your Raw SQL that then I shoot and send by the Entity understood!

  • Maria I appreciate the help, but it contains company information and process, I can not pass! The data above are invented! In the case of the query it would return the input and the!

  • Ok @Apolo, I made an answer let’s see if it’s right... !!! please test and tell me! If I see the SQL I will any Lambda Expression would be quieter ... but, all right we will respect the company!

1 answer

1


As reported by the OP of the question, I did a simulation by fictitious data...

int Id = 1; // id do produto;
var resultado = contexto.ItemEntrada
        .Include("Produto")
        .Include("Entrada")
        .Where(x=>x.Produto.Id == Id)
        .OrderByDescending(x=> x.Entrada.DataCadastro)
        .Take(1)
        .Select( x => new {
            EntradaId = x.EntradaId,
            DataCadastro = x.Entrada.Datacadastro,  
            ProdutoId = x.Produto.Id, 
            ProdutoNome = x.Produto.Nome
        });
  • The Input does not have Reference to the Product. The input contains several items that these contain 1 product each! Vlw for help!

  • I’m fixing @Apolo! now that I’ve seen!

  • @Apollo take a look! now why do you have to pick up hair ItemEntrada

  • You are FODA Maria! Vlw really worked. I don’t need the Include("Product") and Include("Input") unless I want a right Eager Loading?

  • Right @Apolo, it’s like I don’t know about your configuration of Entity I’ve already put on face and forced it as a pattern...! but, good that it was solved...! Check out a tip a good practice is to disable this load so that your application gains speed and you force (by include) the load when you need it, from time to time we do not need ... kkkk but, good that it was solved.

  • 1

    Okay, thanks for the tip!

Show 1 more comment

Browser other questions tagged

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