简体   繁体   中英

Distinct items of query on a list “entity framework Razor”

<!DOCTYPE html>

<html>



@{
PRO1.Modelo.DBMundo Datos = new PRO1.Modelo.DBMundo();
List<PRO1.Modelo.Country> countrylist = Datos.Country.Distinct().ToList();
List<PRO1.Modelo.City> citylist = Datos.City.Distinct().ToList();
}


<head>
<meta name="viewport" content="width=device-width" />
<title>Paises</title>
</head>
<body>
<p>Seleciona las opciones</p>
<p>Codigo de pais</p>
<form name="formula1" method="post" action="">


    <select name="selcodpais">
        <optgroup label="prueba">
            @{

                foreach (PRO1.Modelo.Country registro in countrylist)
                {
                    <option value="a">@registro.Continent</option>
                }

            }
        </optgroup>

    </select>
    <input type="submit" value="Buscar" />



     </form>



     </body>
     </html>

So what im trying is to display all the items form the table on the select form, but i keep geting theese duplicates even using .Distinct(), i only want to be displayed one time per item, maybe using group by but i still dont know how to do it since im a newbie if u also have some clue or information about it i will really apreciate it Thanks by the way

Need to tell it what makes it distinct -- otherwise it goes by object reference. Assuming a country has a Name property...

List<PRO1.Modelo.Country> countrylist = Datos.Country.Distinct(c => c.Name).ToList();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM