List names in a query and send to a Viewmodel

Asked

Viewed 62 times

1

I wanted to list the names belonging to a group and send to a Viewmodel and be collected by a View(details). Problem:

'Iqueryable' does not contain a Definition for 'Catechetizing' and in the Extension method 'Catechetizing' Accepting a first argument of type 'Iqueryable' could be found (are you Missing a using Directive or an Assembly Reference?)

Controller:

 public ActionResult Detalhes(int? id)
        {
            var grupo = db.Grupo.Where(g => g.GrupoID ==id).FirstOrDefault();

            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            //query para recolher as nomes dos catequizandos pertencentes ao grupo selecionado
            var queryNomeCatequizandos = (from g in db.Grupo
                                          join i in db.Inscricao on g.GrupoID equals i.GrupoID
                                          join c in db.Catequizando on i.CatequizandoID equals c.CatequizandoID
                                          join p in db.Pessoa on c.CatequizandoID equals p.PessoaID
                                          where g.GrupoID == i.GrupoID && i.CatequizandoID == c.CatequizandoID && c.CatequizandoID == p.PessoaID && g.GrupoID == id
                                          select new GrupoViewModel
                                          {
                                              NomeCatequizando = p.Nome,                                                                                  
                                          });



            if (queryNomeCatequizandos == null)
            {
                return HttpNotFound();
            }

            GrupoViewModel model = new GrupoViewModel()
            {
                GrupoID = grupo.GrupoID,
                NomeCatequizando = queryNomeCatequizandos.NomeCatequizando,
            };


            return View(grupo);
        }

Viewmodel:

public class GrupoViewModel
    {
        public int GrupoID { get; set; }
        public String AnoPastoral { get; set; }
        public String HoraInicio { get; set; }
        public String DiaSessao { get; set; }
        public String AnoCatequese { get; set; }
        public String LetraGrupo { get; set; }
        public String Sala { get; set; }
        public String Observacoes { get; set; }
        public String NomeCatequizando { get; set; }

    }

You have a problem: Inscription model

public int? Grupoid { get; set; }

Controller error:

GrupoID = i.GrupoID,

Error:

Cannot implicitly Convert type 'int? ' to 'int'. An Explicit Conversion exists (are you Missing a cast?)

1 answer

4


Your approach is not correct. The right one would be:

var queryNomeCatequizandos = (from g in db.Grupo
                              join i in db.Inscricao on g.GrupoID equals i.GrupoID
                              join c in db.Catequizando on i.CatequizandoID equals c.CatequizandoID
                              join p in db.Pessoa on c.CatequizandoID equals p.PessoaID
                              where g.GrupoID == i.GrupoID && i.CatequizandoID == c.CatequizandoID && c.CatequizandoID == p.PessoaID && g.GrupoID == id
                              select new GrupoViewModel
                              {
                                  GrupoID = i.GrupoID,
                                  NomeCatequizando = p.Nome
                              });

The return would be:

return View(queryNomeCatequizandos.ToList());

This here you can remove:

        GrupoViewModel model = new GrupoViewModel()
        {
            GrupoID = grupo.GrupoID,
            NomeCatequizando = queryNomeCatequizandos.NomeCatequizando,
        };

To View would have something like:

@model IEnumerable<MeuProjeto.ViewModels.GrupoViewModel>

And the listing:

@foreach (var grupo in Model) { ... }

For the null group problem, do the following:

var queryNomeCatequizandos = (from g in db.Grupo
                              join i in db.Inscricao on g.GrupoID equals i.GrupoID
                              join c in db.Catequizando on i.CatequizandoID equals c.CatequizandoID
                              join p in db.Pessoa on c.CatequizandoID equals p.PessoaID
                              where g.GrupoID == i.GrupoID && i.CatequizandoID == c.CatequizandoID && c.CatequizandoID == p.PessoaID && g.GrupoID == id
                              select new GrupoViewModel
                              {
                                  GrupoID = i.GrupoID ?? 0,
                                  NomeCatequizando = p.Nome
                              });
  • There is a problem the group is not required.

  • You should have put that in the question. If it’s not mandatory, how will you select by the sentence you put?

  • I am Portuguese and didn’t notice the context in the word "sentence", but an Inscription may or may not be related to a group. that is, Foreignkey may be null.

  • "Sentencing" in Brazilian Portuguese is the query Linq. Anyway, I put an improvement on the answer.

  • Another problem arose in my View, I wanted to send more information to the View, other than the list of Names, such as "Lettering", "room", etc, which is not of the Ienumerable type. How can I solve this problem?

  • Filling more things in Viewmodel and grouping into View. Open up another question, please.

Show 1 more comment

Browser other questions tagged

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