在实体框架中执行Raw sql查询 [英] Execute Raw sql query in entity framework

查看:72
本文介绍了在实体框架中执行Raw sql查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下查询。它在列表中返回5行计数,但列表中的所有元素都为null。我做错了什么。请帮助

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

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()

推荐答案





我建议您使用Linq,当您使用Entity Framework时,数据库内容将作为Class Entity提供。



Linq为优化的查询形式提供帮助。处理干净的代码。



当你到达某个地方时,你也会在Linq得到很多帮助,所以试试Linq



以下示例适用于您的方案。





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.


    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!


检查此示例应用程序:



初学者实体框架 [ ^ ]
Check this sample application:

Entity Framework for Beginners[^]


这篇关于在实体框架中执行Raw sql查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆