2

I'm trying to include the properties of a child property, but is returned as null, even I using inclusion for the property

 var notas =
                dtx.NFeItem.Where(n => n.Empresa.EmpresaId == empresa.EmpresaId)
                    .Where(n => n.NFe.ide_dEmi.Value.Year >= anoInicial)
                    .Where(n => n.NFe.ide_dEmi.Value.Month >= mesInicial)
                    .Where(n => n.NFe.ide_dEmi.Value.Year <= anoFinal)
                    .Where(n => n.NFe.ide_dEmi.Value.Month <= mesFinal)
                    .Where(n => n.NFe.ide_tpNF == "1")
                    .Include(n => n.NFe)
                    .Include(n => n.NFe.participante.Empresa)
                    .GroupBy(g => new { g.CST_ICMS, g.CFOP, g.aliqICMS, g.pRedBC_ICMS, g.NFe.ide_dEmi.Value.Month, g.NFe.ide_dEmi.Value.Year, g.NFe.participante })
                    .Select(i => new { i.Key.CFOP, CST = i.Key.CST_ICMS, pICMS = i.Key.aliqICMS, pRedBC = i.Key.pRedBC_ICMS, mes = i.Key.Month, ano = i.Key.Year, NFePart = i.Key.participante });

Even using Include(n => n.NFe.participante.Empresa) the property returns null

Then I do the fall

 var periodos = notas.DistinctBy(i => new { i.ano, i.mes }).Select(i => new { i.ano, i.mes }).ToList();


            foreach (var periodo in periodos)
            {

                var notasPeriodo = notas.Where(i => i.ano == periodo.ano && i.mes == periodo.mes).ToList();
                var participantes = notasPeriodo.DistinctBy(p => p.NFePart.CNPJ).Select(i => i.NFePart).ToList();


    //etc.....
    }

Results:

enter image description here

2
  • 3
    EF is known to ignore includes when the query is using projection (select) like yours. Commented Oct 11, 2016 at 13:15
  • @IvanStoev you know how can I fix this problem? Commented Oct 11, 2016 at 13:21

1 Answer 1

3

EF ignores the Include expressions if the final query result element type is not the entity used as root for the includes.

What you can do is to include the desired properties in the query projection and rely on EF navigation property fixup to bind them to the respective objects referencing them:

var notas = dtx.NFeItem
    .Where(n => n.Empresa.EmpresaId == empresa.EmpresaId)
    .Where(n => n.NFe.ide_dEmi.Value.Year >= anoInicial)
    .Where(n => n.NFe.ide_dEmi.Value.Month >= mesInicial)
    .Where(n => n.NFe.ide_dEmi.Value.Year <= anoFinal)
    .Where(n => n.NFe.ide_dEmi.Value.Month <= mesFinal)
    .Where(n => n.NFe.ide_tpNF == "1")
    .GroupBy(g => new { g.CST_ICMS, g.CFOP, g.aliqICMS, g.pRedBC_ICMS, g.NFe.ide_dEmi.Value.Month, g.NFe.ide_dEmi.Value.Year, g.NFe.participante })
    .Select(i => new
    { 
        i.Key.CFOP,
        CST = i.Key.CST_ICMS,
        pICMS = i.Key.aliqICMS,
        pRedBC = i.Key.pRedBC_ICMS,
        mes = i.Key.Month,
        ano = i.Key.Year,
        NFePart = i.Key.participante,
        // include properties:
        i.Key.participante.Empresa,
    });
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.