2
I’m taking information from a table and filling out a DropDownList
, but I have a lot of information repeated and I would like to group the values so that they are not repeated in the DropDownList
.
Example of information I’m capturing.
Cidade1
Cidade2
Cidade1
Cidade1
Cidade3
I would like Dropdownlist to receive only Cidade1
, Cidade2
and Cidade3
without repetition.
In Controller I am using the following instruction, but I did not succeed:
List<Empresa> items = db.Empresa.OrderBy(x => x.Cidade).ThenBy(x => x.Cidade).ToList();
It would be something like a
distinct campo
in consultation?– gato
You see the problem is that in the Dropdownlist is coming repeated values, have you passed me the Distinct statement ? So the synthesis List<Company> items = db.Empresa.Orderby(x => x.City) is wrong. Distinct(x => x.City). Tolist();
– Cyberlacs
If you want something more complete, it exists this library maintained by Jon Skeet :D There you will find an improved version of the method
Distinct()
which is theDistinctBy()
it allows returning objects instead of single values.– gato