Filtering through empty children

Asked

Viewed 51 times

1

I have two entities (TipoDeRequerimento and TipoRequerimentoSituaçãoAcademica) in my query. Today I can make a filter to bring the Types of requirements that have a certain academic situation pre-registered. Now I need this filter to return also the requirements types that do not have Academic Situation informed, that is, for a certain requirement that I registered and did not inform Academic Situation.

Follow the code of the consultation:

IList lista = null;
using (var bll = new TipoRequerimentoRepository(empresa))
{
    var pLista = bll.Query(p => p.Descricao.Contains(filtro) || filtro == "")
        .Where(p => categoriaID == 0 || p.CategoriaID == categoriaID)
        .Where(p => situacaoAcademicaID == 0 || p.TipoRequerimentoSituacaoAcademica.Any(x => x.AlunoSituacaoAcademica.AlunoSituacaoAcademicaID == situacaoAcademicaID))
        .Where(p => p.Ativo == true)
        .Select(p => new
        {
            id = p.ID,
            name = p.Descricao,
            prazoDias = p.Prazo,
            valor = p.ValorUnitario,
            Gratuito = p.Gratuito
        });
    lista = pQtdeRegistros > 0 ? pLista.Take(pQtdeRegistros).AsNoTracking().Distinct().ToList() : pLista.AsNoTracking().ToList();
}
return lista;

In the filter I tried to put a code that filtered the TipoRequerimentoStuacaoAcademica == NULL but I did not succeed. If someone can help me, I really appreciate.

2 answers

3

It seems to me that TipoRequerimentoStuacaoAcademica is a collection of TipoDeRequerimento.

Try to use !p.TipoRequerimentoStuacaoAcademica.Any() instead of p.TipoRequerimentoStuacaoAcademica == null.

  • I also tried it returns empty with nothing on the list

  • It worked with a help of a parentheses getting that way .Where(p => (!p.TipoRequerimentoSituacaoAcademica.Any() || p.TipoRequerimentoSituacaoAcademica.Any(x => x.AlunoSituacaoAcademica.AlunoSituacaoAcademicaID == situacaoAcademicaID)))

1


If, for example, we define that situacaoAcademicaID = -1 returns the requirements that do not have informed Academic Status, you can implement so:

var pLista = bll.Query(p => p.Descricao.Contains(filtro) || filtro == "")
    .Where(p => categoriaID == 0 || p.CategoriaID == categoriaID)
    .Where(p => situacaoAcademicaID == 0
                || situacaoAcademicaID == -1 && p.TipoRequerimentoSituacaoAcademica.Count == 0 
                || p.TipoRequerimentoSituacaoAcademica.Any(x => x.AlunoSituacaoAcademica.AlunoSituacaoAcademicaID == situacaoAcademicaID))
    .Where(p => p.Ativo == true)
    .Select(p => new
  • Unfortunately it didn’t work returns the empty list too.

Browser other questions tagged

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