Include in Entity Framework from an Enumerable

Asked

Viewed 105 times

2

I have the following entities:

public class Rota
{
    public Rota()
    {
        CidadesRotas = new List<CidadesRota>();
    }
    public int RotaId { get; set; }
    public string Descricao { get; set; }
    public string Observacao { get; set; }
    public bool Ativo { get; set; }
    public virtual ICollection<CidadesRota> CidadesRotas { get; set; }

}

public class Cidade
{
    public Cidade()
    {
        CidadesRotas = new List<CidadesRota>();
    }
    public int CidadeId { get; set; }
    public string Nome { get; set; }
    public virtual Estado Estado { get; set; }
    public virtual ICollection<CidadesRota> CidadesRotas { get; set; }
}




public class CidadesRota
{
    public int CidadeId { get; set; }
    public int RotaId { get; set; }
    public bool Ativo { get; set; }
    public Cidade Cidade { get; set; }
    public Rota Rota { get; set; }
}

When I perform a route search for an ID, I need each item on my Cityshow list to be filled in with their respective cities. I know you can use Entity’s Include, but I don’t know how to use it from a list item.

In the following example, return me a route with a city list:

var query = _context.Rota
            .Include(r => r.CidadesRotas)
            .Where(r => r.RotaId == RotaId);

Doubt: It is possible to bring each item from the list of cities?

1 answer

2


Yes! Possible! Just use Theninclude:

var query = _context.Rota
        .Include(r => r.CidadesRotas)
            .ThenInclude(r => r.Cidade)
        .Where(r => r.RotaId == RotaId);

It may be that your Intellisense accuses some problem, but it is an Intellisense bug, which can be followed by here: https://github.com/aspnet/EntityFrameworkCore/issues/6560

Can try to build that will give success. :)

  • thank you, I’ll test...

Browser other questions tagged

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