筛选导航属性EF核心

时间:2019-03-16 17:37:41

标签: c# linq entity-framework-core

在我的存储库中,我尝试过滤具有有效变体的产品 但是我不知道怎么办,因为是第一次。 我应该退还所有启用或未启用变体的产品

Varianti

我要过滤的字段位于.Where(v => v.Active == 1)中。

insert tablename password="yourpasswordvalue",user="yourusername" 怎么办?

3 个答案:

答案 0 :(得分:1)

只需将您的过滤器添加到where方法

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();
}

如果您不需要Varianti的值,则可以删除包含部分

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 :(得分:0)

添加到Where()子句并使用Any()

return await _ctx.Articoli
        .Include(i => i.Varianti)
        .Include(i => i.FotoArticoli)
        .Where(a => a.Divisione == divisione
                  && a.Category!= "001"
                  && a => a.Varianti.Any(v => v.Active == 1))

        .ToListAsync();

答案 2 :(得分:0)

To get all products with active variants need to make a projection to new article object and there filter variants

This is the solution

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)               
                     .Select(x => new Articolo
                    {
                        Codice = x.Codice ,
                        Prezzo = x.Prezzo ,
                        Varianti = x.Varianti.Where(v => v.attivo== 0).ToList(),
                        FotoArticoli = x.FotoArticoli
                    })           
                .ToListAsync();
}