2016-10-11 6 views
1

Я пытаюсь включить свойство собственности ребенка, но возвращаются в нуле, даже я с помощью включения для свойстваВключить вложенный вложенный ребенок с помощью LINQ

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

Даже используя Include(n => n.NFe.participante.Empresa) свойство возвращает нуль

Тогда я грехопадения

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..... 
    } 

Результаты:

enter image description here

+3

EF известно игнорировать включает в себя, когда запрос использует проекцию (select), подобную вашей. –

+0

@IvanStoev вы знаете, как я могу исправить эту проблему? –

ответ

2

EF игнорирует выражения Include, если тип элемента результата окончательного запроса не является сущностью, используемой как root для включений.

Что вы можете сделать, это включить требуемые свойства в проекции запроса и полагаться на EF навигацию адресной привязкой связать их с соответствующими объектами ссылок на них:

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, 
    }); 
Смежные вопросы