星空网 > 软件开发 > ASP.net

使用EDMX查询(EF基础系列10)

EF支持三种类型的查询:

1.LINQ to Entities

2.Entity SQL

3.Native SQL 

 

1.LINQ to Entities

LINQ Method syntax:

using (var context = new SchoolDBEntities1())      {        //使用Linq to Entities        var myQuery = context.Students.Where(s => s.StudentName == "张三");        var student = myQuery.FirstOrDefault<Student>();                      }

 

LINQ Query syntax:

 using (var context = new SchoolDBEntities1())      {        var myQuery = from s in context.Students               where s.StudentName == "张三"               select s;        var student = myQuery.FirstOrDefault<Student>();      }

 

2.Entity SQL

entity SQL 是另外的一个查询语言,它直接被EF的Object Service处理,返回值是ObjectQuery而不是IQueryable;

//Querying with Object Services and Entity SQL  string sqlString = "SELECT VALUE st FROM SchoolDBEntities.Students " +            "AS st WHERE st.StudentName == 'Bill'";    var objctx = (ctx as IObjectContextAdapter).ObjectContext;          ObjectQuery<Student> student = objctx.CreateQuery<Student>(sqlString);          Student newStudent = student.First<Student>();

 

你同样可以使用EntityConnection和entityCommand来执行Entity SQL:

 using (var con = new EntityConnection("name=SchoolDBEntities"))  {    con.Open();    EntityCommand cmd = con.CreateCommand();    cmd.CommandText = "SELECT VALUE st FROM SchoolDBEntities.Students as st where st.StudentName='Bill'";    Dictionary<int, string> dict = new Dictionary<int, string>();    using (EntityDataReader rdr = cmd.ExecuteReader(CommandBehavior.SequentialAccess | CommandBehavior.CloseConnection))    {        while (rdr.Read())        {          int a = rdr.GetInt32(0);          var b = rdr.GetString(1);          dict.Add(a, b);        }    }          }

EntityDataReader不返回ObjectQuery,代替的是返回数据的行数和列数,你可以点击这个 MSDN blog链接来学习Entity SQL;

3.Native SQL

你可以执行原生态的SQL:

using (var ctx = new SchoolDBEntities())  {    var studentName = ctx.Students.SqlQuery("Select studentid, studentname, standardId from Student where studentname='Bill'").FirstOrDefault<Student>();  }  

 




原标题:使用EDMX查询(EF基础系列10)

关键词:

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

黑五网一收官大捷!亚马逊印度出口商业绩狂飙:https://www.kjdsnews.com/a/1689219.html
订单量持续增长,Ozon推出新货运平台:https://www.kjdsnews.com/a/1689220.html
eBay“广告专区”上线,卖家一线广告情报站,助力业务高效增长!:https://www.kjdsnews.com/a/1689221.html
2024-让我们忠于“品牌”,让我们直面“转化”:https://www.kjdsnews.com/a/1689222.html
eBay、Amazon和Walmart如何刊登汽配和设置ACES呢?:https://www.kjdsnews.com/a/1689223.html
《2023年美国青少年社交媒体使用情况报告》:https://www.kjdsnews.com/a/1689224.html
探讨内地人开设香港账户的可行性 :https://www.kjdsnews.com/a/1836442.html
在古巴做游轮 古巴旅游项目:https://www.vstour.cn/a/363194.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流