distinct use in a dropdownlist

Asked

Viewed 77 times

1

I’m trying to get equal names off my dropdownlist, but they’re different id’s. I wanted where I would have to implant the code if it would be in the view, or in the controller somewhere else.

Data in the table: inserir a descrição da imagem aqui

And this is what the dropdownlist looks likeinserir a descrição da imagem aqui

This is my view:

@Html.ValidationMessageFor(a => a.NomeProdutoId)
@Html.LabelFor(a => a.NomeProdutoId, "Escolher Produto:")
@Html.DropDownListFor(a => a.NomeProdutoId, new SelectList(ViewBag.Compra, "Id", "NomeProduto"))

That is the line of code that the ViewBag.Compra is pulling

public IList<Compra> Lista()
        {
            string hql = "select c from Compra c;";
            IQuery query = session.CreateQuery(hql);
            return query.List<Compra>();
        }

2 answers

1

You can’t solve this just in the query?

select 
 Id, 
 NomeProduto 
from Compra c 
group by Id, NomeProduto
order by NomeProduto

Note: You may need to adjust the field of your Id...

1


Use the following code to get the expected result:

ViewBag.Compra = Lista()
                        .Select(x => new {Id = x.NomeId, NomeProduto = x.NomeProduto})
                        .Distinct();
  • On the part of NomeId would be what? For there is no such variable in the code

  • Same name. From what I understood of the table structure is that the Nomeid is the primary key of the Product Nomeid, right?

  • Name is the user name that pulls from another table, I will try to put Id in place, it is the primary key

  • Theoretically, you need to assign an Id to the Product name. It exists?

  • No, then probably every product will have to have its ID in specific, correct?

  • Exactly, instead of including the Product Name, you include the Product Idproduct. Soon, you will have a table with products that can be used more elegantly in the Dropdownlist (Without the need for Distinct).

  • I understand, the problem that if I do this, this table where the product names and their values are, is ordered to other tables taking their products and values.

Show 3 more comments

Browser other questions tagged

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