Querying with EDM
来源:互联网 发布:淘宝ps修图 编辑:程序博客网 时间:2024/05/13 09:32
Entity framework supports three types of queries: 1) LINQ to Entities 2) Entity SQL 3) Native SQL
1) LINQ to Entities: Language-Integrated Query (LINQ) is a powerful query language introduced in Visual Studio 2008. You can use LINQ in C# or Visual Basic to query different data sources. LINQ-to-Entities operats on entity framework entities to access the data from the underlaying database. You can use LINQ method syntax or query syntax to querying with EDM. VisitLINQ Tutorials to learn LINQ step-by-step.
LINQ Method syntax:
//Querying with LINQ to Entities using (var context = new SchoolDBEntities()) { var L2EQuery = context.Students.where(s => s.StudentName == "Bill"); var student = L2EQuery.FirstOrDefault<Student>(); }
LINQ Query syntax:
using (var context = new SchoolDBEntities()) { var L2EQuery = from st in context.Students where st.StudentName == "Bill" select st; var student = L2EQuery.FirstOrDefault<Student>(); }
2) Entity SQL: Entity SQL is another way to create a query. It is processed by the Entity Framework’s Object Services directly. It returns ObjectQuery instead of IQueryable.
You need ObjectContext to create a query using Entity SQL.
The following code snippet shows the same query result as L2E query above.
//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>();
You can also use EntityConnection and EntityCommand to execute Entity SQL as shown below:
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); } } }
3) Native SQL You can execute native SQL queries for a relational database as shown below:
using (var ctx = new SchoolDBEntities()) { var studentName = ctx.Students.SqlQuery("Select studentid, studentname, standardId from Student where studentname='Bill'").FirstOrDefault<Student>(); }Learn to execute raw sql query using DbContext inRaw SQL Query section.
- Querying with EDM
- Chapter10 Querying with LINQ
- Querying XML Data with XQuery
- Querying XML Data with XQuery
- Querying XML Data with XQuery
- Querying XML Data with XQuery
- Querying XML Data with XQuery
- Querying XML Data with XQuery
- Querying XML Data with XQuery
- Querying XML Data with XQuery
- Querying XML Data with XQuery
- Querying XML Data with XQuery
- Querying XML Data with XQuery
- Querying XML Data with XQuery
- Querying CRM data with LINQ
- Querying a MySQL database with LoadRunner
- XPath Querying Over DataSets with the DataSetNavigator - Arpan Desai
- 6.Querying WMI and 7.Working with Active Directory
- 如何绕开Google Play地区限制,自由切换手机App所属国家
- 《深入理解JAVA7》note
- WPF 自定义最小化窗口 的一些易错点
- ARM9时钟学习
- 美域高祝您工作愉快!
- Querying with EDM
- 零基础学习Shell编程
- dataTablesTD宽度问题
- 使用strace, ltrace寻找故障原因的线索
- activiti与spring集成配置
- 创建圆角窗体
- android 里面的 Drawable 和 ConstantState
- 美域高祝您工作愉快!~~~
- 多线程程序设计