use of distinct in a dropdownlist

1

I want to remove equal names from my dropdownlist, but they are from different id's. I wanted to where I would have to deploy the code if it would be in the view, or in the controller somewhere else.

Data in the table:

Andthat'swhatthedropdownlistlookslike

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"))

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

public IList<Compra> Lista()
        {
            string hql = "select c from Compra c;";
            IQuery query = session.CreateQuery(hql);
            return query.List<Compra>();
        }
    
asked by anonymous 24.11.2017 / 18:30

2 answers

1

Use the following code to get the expected result:

ViewBag.Compra = Lista()
                        .Select(x => new {Id = x.NomeId, NomeProduto = x.NomeProduto})
                        .Distinct();
    
24.11.2017 / 18:36
1

Can not you just solve this 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 ...

    
24.11.2017 / 20:47