Click here to Skip to main content
15,169,285 members
Please Sign up or sign in to vote.
4.11/5 (2 votes)
I am using the following query. It returns 5 row count in a list but all the elements in the list is null. what I am doing wrong.Please help
C#
public class TempClass
{
    public int SID { get; set; }
    public string SNAME { get; set; }
    public string SAGE { get; set; }
}

_dbContext.Database.SqlQuery<TempClass>("select ID, NAME, AGE from eis_hierarchy").ToList()
Posted
Comments
Tirthankar Dutta 31-Oct-13 16:15pm
   
Can you please help me?

Hi,

I recommend you to use Linq, when you are using Entity Framework the data base contents will be available as Class Entity.

Where Linq help you a lot for optimized form of querying & Processing along with clean code.

You also get many helps in Linq when you struck up somewhere, so try Linq

The below sample for your scenario will works.


C#
    public class TempClass
    {
        public int SID { get; set; }
        public string SNAME { get; set; }
        public string SAGE { get; set; }
    }
    
    Public  class Program
    {
     static void Main(string[] args)
       {
            List<tempclass>  lstStudents = new List<tempclass>();
            
            lstStudents.Add(new TempClass{SID= 101, SNAME = "Rajesh",SAGE ="25" });
            lstStudents.Add(new TempClass{SID= 102, SNAME = "Anand",SAGE ="20" });
            lstStudents.Add(new TempClass{SID= 103, SNAME = "Venky",SAGE ="21" });
            lstStudents.Add(new TempClass{SID= 104, SNAME = "Prabhu",SAGE ="23" });

            var data = from stud in lstStudents
                       where stud.SID > 101
                       select stud;

          foreach (TempClass stud in data)
            {
   Console.WriteLine("SID : " + stud.SID + "\tSNAME : " + stud.SNAME + "\tSAGE : " + stud.SAGE);
            }
    
        }

</tempclass></tempclass>



Thanks!
   
v2
Check this sample application:

Entity Framework for Beginners[^]
   
v2

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