Pro.Entity.framework 4.0 Note chapter4

SQL Server logically processes a query in the following order (by number):

(8) SELECT (9) TOP
(1) FROM
(3) JOIN
(2) ON
(4) WHERE
(5) GROUP BY
(6) WITH
(7) HAVING
(1) ORDER BY


Linq  例一

var people = from p in context.People
where p.LastName == "King"
|| p.LastName == "Jones"
orderby p.LastName, p.FirstName
select new { p.FirstName, p.LastName };

例二

var people = from p in context.People
orderby p.FirstName
where p.LastName == "King"
|| p.LastName == "Jones"
group p by p.LastName;


A LINQ query can end in either a SELECT or GROUP.

例三

var people = from p in context.People
join
emp in context.Employeeson p.BusinessEntityIDequals emp.BusinessEntityID
orderby p.LastName, p.FirstName descending
select new { p.FirstName, p.LastName, emp.HireDate };


Entity SQL


using (var context = new dataBaseEntities())
{
var str = "SELECT VALUE p FROM dataBaseEntities.People AS p WHERE p.LastName
= 'King' Order by p.FirstName";
var people = context.CreateQuery<Person>(str);
foreach (var person in people)
{
listBox1.Items.Add(string.Format("{0} {1}", person.FirstName, person.LastName));
}
}


Besides using LINQ to Entities and Entity SQL, there is one more way you can query the EDM, and that is
through the EntityClient. The EntityClient provider can be used by the Entity Framework to access data
described in an EDM.

using (EntityConnection conn = new EntityConnection("name = dataBaseEntities"))
{
conn.Open();
var query = "SELECT p.FirstName, p.LastName FROM dataBaseEntities.People
AS p WHERE p.LastName = 'King' Order by p.FirstName";
EntityCommand cmd = conn.CreateCommand();
cmd.CommandText = query;
using (EntityDataReader rdr

= cmd.ExecuteReader(System.Data.CommandBehavior.SequentialAccess))
{
while (rdr.Read())
{
firstname = rdr.GetString(0);
lastname = rdr.GetString(1);
listBox1.Items.Add(string.Format("{0} {1}", firstname, lastname));
}
}
conn.Close();
}


Immediate vs. Deferred Query Execution


Deferred Execution

The query is executed when the
query variable (in this case, the variable query) is iterated over in the foreach statement. This form of
query execution is called deferred execution

var query = from p in context.People
select new { p.LastName, p.FirstName, p.MiddleName, p.BusinessEntityID };
var secondquery = query.Where(p => p.BusinessEntityID == 8);
foreach (var per in query)
{
listBox1.Items.Add(string.Format("{0} {1}", per.FirstName, per.LastName));
}


Immediate

• ToList: Create a List(T) from an IEnumerable(T).
• ToDictionary: Creates a Dictionary from an IEnumerable(T).
• ToLookup: Creates a generic Lookup from an IEnumerable(T).
• ToArray: Creates an array from an IEnumerable(T).
• Average: Computes the average of a sequence of numeric values.
• First: Selects first value in a sequence of values.
• Count: Counts number of values in a sequence of values.
• Max: Returns the maximum value in a sequence of values.


var query = (from p in context.People
select new { p.LastName, p.FirstName, p.MiddleName, p.BusinessEntityID
}).ToList();


var query = (from p in context.People
where p.LastName.StartsWith("K")
select new { p.LastName, p.FirstName, p.MiddleName, p.BusinessEntityID
}).Count();

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章