简体   繁体   中英

SQL Query only returns one item (rather than complete list) - asp.net mvc

I have an SQL query within ASP.NET, but it only seems to return 1 result rather than filling the list with 4 as it should (as the query itself is correct)

Code:

List<string> dropItems, dropItems2;

using (var dbContext = new DatabaseContext())
{
    dropItems = dbContext.Database.SqlQuery<String>(
        String.Format("SELECT locations.Code FROM locations")).ToList();
}
using (var dbContext = new DatabaseContext())
{
    dropItems2 = dbContext.Database.SqlQuery<String>(
        String.Format("SELECT locations.Name FROM locations")).ToList();
}

After the code executes, each list only contains one item rather than 4 which they both should

FYI - The item I'm always receiving rather than the full 4 is just the last one

using .ToList() makes a list of what the query returns (lets call it a query result object). since a query only returns one result objects the list is only 1 long.

An easier way to fill a dropdownList is to use its datasource. A example can be found here: DropdownList DataSource

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