[RESOLVED]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
F.Y.I – The item I’m always receiving rather than the full 4 is just the last one

Hi WithoutanId,

Thanks for your post.

You can try remove String.Format,like this:

var blogNames = context.Database.SqlQuery<string>( "SELECT locations.Code FROM locations").ToList(); 

and I suggest that you can  try use Linq to implement.

More information,you can refer to the following link:

http://msdn.microsoft.com/en-us/data/jj592907.aspx

Hope this can be helpful.

Best Regards,

Eileen

Leave a Reply