0

I want to include some entities that I have in my context, but I don't where to use the include method in my linq query.... In my method I use the include in _context.TurnoDocente but I didn't get any result at all...

Info method:

public async Task<IActionResult> Index(string anoLetivo, int ano, int semestre)
{
        var turno_docente = new List<TurnoDocente>();     

        Main main = new Main();
        main.TurnoDocente = turno_docente;

        //user
        var user = await GetCurrentUserAsync();
        //docente
        var IDdocente = _context.Docente.SingleOrDefault(x => x.UserId == user.Id);

        if (ano == 0 || semestre == 0)
        {
            var nomedoc = (from nome in _context.Docente
                           join turnodoc in _context.TurnoDocente.Include(c=>c.Docente)
                                                                 .Include(c=>c.Turno)
                                                                 .Include(c=>c.Turno.MetaDisciplina) on nome.DocenteId equals turnodoc.DocenteId
                           join turno in _context.Turno on turnodoc.TurnoId equals turno.TurnoId
                           join metadisc in _context.MetaDisciplina on turno.MetaDisciplinaId equals metadisc.MetaDisciplinaId
                           join nalunos in _context.NAlunos on metadisc.MetaDisciplinaId equals nalunos.MetaDisciplinaId
                           join anoletivo in _context.AnoLetivo on nalunos.AnoLetivoId equals anoletivo.AnoLetivoId
                           where anoletivo.Ano == anoLetivo && nome.DepartamentoId == IDdocente.DepartamentoId
                           select new
                           {
                               nome = nome.DocenteId,
                               nomemeta = turno.TurnoId
                           }).ToList().Distinct();

            foreach (var itex in nomedoc)
            {
                turno_docente.Add(new TurnoDocente
                {
                  DocenteId = itex.nome,
                  TurnoId = itex.nomemeta
                });                 
            }
        }
    return View(main);
}

View

@model ModelsLibrary.Main

@{
ViewData["Title"] = "Index";
}

<table class="table">
   <thead>
       <tr>
          <th>
            <label>Disciplina</label>
          </th>
           <th>
            <label>Docente</label>
        </th>
    </tr>
</thead>
<tbody>
    @foreach (var ite in Model.TurnoDocente)
    {
            <tr>
                <td>
                    @Html.DisplayFor(modelItem => ite.Turno.MetaDisciplina.Nome)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => ite.Docente.Nome)
                </td>
            </tr>
    }
  </tbody>

I need to get the information instead of have the ID's but I can't understand where I need to use the include method to get all the information from the other entities.

1 Answers1

0
public async Task<IActionResult> Index(string anoLetivo, int ano, int semestre)
    {
        if (!(ano == 0 || semestre == 0))
        {
            return NotFound(); //handle parameter
        }

        //user
        var user = await GetCurrentUserAsync();
        //docente
        var IDdocente = _context.Docente.SingleOrDefault(x => x.UserId == user.Id);

        Main main = new Main();

        main.TurnoDocente = (from nome in _context.Docente
                       join turnodoc in _context.TurnoDocente.Include(c => c.Docente)
                           .Include(c => c.Turno)
                           .Include(c => c.Turno.MetaDisciplina) on nome.DocenteId equals turnodoc.DocenteId
                       join turno in _context.Turno on turnodoc.TurnoId equals turno.TurnoId
                       join metadisc in _context.MetaDisciplina on turno.MetaDisciplinaId equals metadisc.MetaDisciplinaId
                       join nalunos in _context.NAlunos on metadisc.MetaDisciplinaId equals nalunos.MetaDisciplinaId
                       join anoletivo in _context.AnoLetivo on nalunos.AnoLetivoId equals anoletivo.AnoLetivoId
                       where anoletivo.Ano == anoLetivo && nome.DepartamentoId == IDdocente.DepartamentoId
                       select new TurnoDocente //<== add this
                       {
                           DocenteId = nome.DocenteId,
                           TurnoId = turno.TurnoId,
                           Turno = ...., //init your model here
                           Docente = ....,//init your model here
                       }).ToList().Distinct();

        return View(main); //return view and give it model
    }
Dongdong
  • 2,208
  • 19
  • 28