Filter navigation property EF core

c# entity-framework-core linq

Question

In my repository I try to filter the products with the variants active but I do not know how because is first time. I should return all products with or without variants active

public async Task<IEnumerable<Articolo>> GetArticoliByDivisioneAsync(string divisione)
{
    return await _ctx.Articoli
                     .Where(a => a.Divisione == divisione
                                 && a.Category!= "001")
                     .Include(i => i.Varianti)
                     .Include(i => i.FotoArticoli)               
                     .ToListAsync();
}

The field I want to filter on is in Varianti.

How can do .Where(v => v.Active == 1) ?

1
1
3/18/2019 8:18:28 AM

Popular Answer

Just add your filter to where Method

public async Task<IEnumerable<Articolo>> GetArticoliByDivisioneAsync(string divisione)
{
    return await _ctx.Articoli
        .Where(a => a.Divisione == divisione
                  && a.Category!= "001"
                  && a.Varianti.Any(v=>v.Active == 1))
        .Include(i => i.Varianti)
        .Include(i => i.FotoArticoli)               
        .ToListAsync();
}

if you don't need the value of Varianti you can remove include part

public async Task<IEnumerable<Articolo>> GetArticoliByDivisioneAsync(string divisione)
{
    return await _ctx.Articoli
        .Where(a => a.Divisione == divisione
                  && a.Category!= "001"
                  && a.Varianti.Any(v=>v.Active == 1))
        .Include(i => i.FotoArticoli)               
        .ToListAsync();
}
1
3/16/2019 6:59:33 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow