Click here to Skip to main content
15,891,529 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
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:

C#
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
Posted
Updated 20-Oct-14 1:11am
v5
Comments
Nathan Minier 20-Oct-14 7:20am    
Quick question: If you're using EF (which DatabaseContext infers), why aren't you using EF for data access(ie dbContext.Set<locations>().Select(x => new { x.Code, x.Name }).toList() ?
ForumCrazy 20-Oct-14 7:22am    
Because I'm not super Knowledgeable in Linq. Will the code you have suggested solve the issue I am having?
Nathan Minier 20-Oct-14 7:47am    
I'd suggest looking into using LINQ, it's very easy and intuitive once you get a grasp on it, and it will (often) do a better job shaping queries than you will with SQLCommand-type objects.

There is a very robust primer at http://www.codeproject.com/Articles/33769/Basics-of-LINQ-Lamda-Expressions
Kornfeld Eliyahu Peter 20-Oct-14 7:25am    
Even I do not think that this is your problem - why the String.Format part?
Did you double checked the locations table (view?) in your database?

You should change your code like this:
C#
List<string> dropItems, dropItems2;

using (var dbContext = new DatabaseContext())
{
    dropItems = dbContext.Locations.Select( l => l.Code).ToList();
    dropItems2 = dbContext.Locations.Select( l => l.Name).ToList();
}
 
Share this answer
 
Comments
Nathan Minier 20-Oct-14 7:44am    
This will poll the database twice. You'd be better served with an anonymous object or a ViewModel, just as standard practice.
Raul Iloc 20-Oct-14 8:05am    
Yes you are right, the code could be optimized, but I did not wanted to change the internal logic used by the originator of the question.
Your Code Working Fine With me..
I can't find out why, you are not getting the result..

Try different code..I think this will solve your problem.
C#
List<string> dropItems, dropItems2;
 
using (var dbContext = new DatabaseContext())
{
var data = from o in dbContext.locations select new { o.Code };
foreach (var str in data)
{
 if(str!=null)
 {
  dropItems .Add(str.ToString());
 }
}
//dropItems = dbContext.Database.SqlQuery<string>(
 //String.Format("SELECT locations.Code FROM locations")).ToList();
}
using (var dbContext = new DatabaseContext())
{
var data1 = from o in dbContext.locations select new { o.Name };
foreach (var str1 in data1)
{
 if(str1!=null)
 {
  dropItems2 .Add(str1.ToString());
 }
}

    //dropItems2 = dbContext.Database.SqlQuery<string>(
        //String.Format("SELECT locations.Name FROM locations")).ToList();
}

</string></string></string>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900