Introduction to the ADO.NET Entity Framework using ASP.NET - What are Entity SQL queries?
(Page 5 of 5 )
I did not introduce Entity SQL queries (which are totally different from normal SQL) in this article. But, I will at some point in the future. For now, to put it simply, Entity SQL queries are very similar to classic SQL queries, except that these queries work on Entities designed using the Entity Model designer.
For now, let us see few simple Entity SQL queries.
To retrieve all "Emp" objects, we can write the following code:
Dim ctxt As New NorthwindEntities
Dim queryString As String = "SELECT VALUE Emp FROM NorthwindEntities.Emp AS Emp"
Dim query As New ObjectQuery(Of Emp)(queryString, ctxt, MergeOption.NoTracking)
Me.GridView1.DataSource = query
Me.GridView1.DataBind()
We can also add filter conditions as follows:
Dim ctxt As New NorthwindEntities
Dim queryString As String = "SELECT VALUE Emp FROM NorthwindEntities.Emp AS Emp WHERE Emp.Dept.Deptno = 20"
Dim query As New ObjectQuery(Of Emp)(queryString, ctxt, MergeOption.NoTracking)
Me.GridView1.DataSource = query
Me.GridView1.DataBind()
Similarly, a particular value in an object can be retrieved using Entity SQL as follows:
Dim queryString As String = "SELECT Emp.Ename FROM NorthwindEntities.Emp AS Emp WHERE Emp.Empno = 1006"
Dim query As New ObjectQuery(Of String)(queryString, ctxt, MergeOption.NoTracking)
Me.lblMsg.Text = query.SelectValue(Of String)("it.ename").FirstOrDefault
In my upcoming articles, we will see more and more examples of the ADO.NET Entity Framework together with other up-to-date Microsoft technologies. I hope you enjoyed the article and any suggestions, bugs, errors, enhancements etc. are highly appreciated at http://jagchat.spaces.live.com
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |