如何:执行参数化查询(实体框架)
本主题说明如何使用 ObjectQuery 执行具有参数的 Entity SQL 查询。 本示例将两个参数传递到 ObjectQuery,执行查询,并循环访问 Contact
项的集合。 将使用以下每种实体框架 查询技术演示同一示例:
LINQ to Entities
Entity SQL 以及 ObjectQuery<T>
ObjectQuery<T> 的查询生成器方法
本主题中的示例基于 Adventure Works 销售模型。若要运行本主题中的代码,则必须已经将 Adventure Works 销售模型添加到了您的项目中,并且已经将项目配置为使用实体框架。有关更多信息,请参见如何:使用实体数据模型向导(实体框架)或如何:手动配置实体框架项目和如何:手动定义实体数据模型(实体框架)。
示例
以下是 LINQ to Entities 示例。
Using context As New AdventureWorksEntities()
Dim FirstName = "Frances"
Dim LastName = "Adams"
Dim contactQuery = From contact In context.Contacts _
Where contact.LastName = LastName AndAlso contact.FirstName = FirstName _
Select contact
' Iterate through the results of the parameterized query.
For Each result In contactQuery
Console.WriteLine("{0} {1}", result.FirstName, result.LastName)
Next
End Using
using (AdventureWorksEntities context =
new AdventureWorksEntities())
{
string FirstName = "Frances";
string LastName = "Adams";
var contactQuery = from contact in context.Contacts
where contact.LastName == LastName && contact.FirstName == FirstName
select contact;
// Iterate through the results of the parameterized query.
foreach (var result in contactQuery)
{
Console.WriteLine("{0} {1} ", result.FirstName, result.LastName);
}
}
以下是 Entity SQL 示例。
Using context As New AdventureWorksEntities()
' Create a query that takes two parameters.
Dim queryString As String = "SELECT VALUE Contact FROM AdventureWorksEntities.Contacts " & _
" AS Contact WHERE Contact.LastName = @ln AND Contact.FirstName = @fn"
Dim contactQuery As New ObjectQuery(Of Contact)(queryString, context)
' Add parameters to the collection.
contactQuery.Parameters.Add(New ObjectParameter("ln", "Adams"))
contactQuery.Parameters.Add(New ObjectParameter("fn", "Frances"))
' Iterate through the collection of Contact items.
For Each result As Contact In contactQuery
Console.WriteLine("Last Name: {0}; First Name: {1}", result.LastName, result.FirstName)
Next
End Using
using (AdventureWorksEntities context =
new AdventureWorksEntities())
{
// Create a query that takes two parameters.
string queryString =
@"SELECT VALUE Contact FROM AdventureWorksEntities.Contacts
AS Contact WHERE Contact.LastName = @ln AND
Contact.FirstName = @fn";
ObjectQuery<Contact> contactQuery =
new ObjectQuery<Contact>(queryString, context);
// Add parameters to the collection.
contactQuery.Parameters.Add(new ObjectParameter("ln", "Adams"));
contactQuery.Parameters.Add(new ObjectParameter("fn", "Frances"));
// Iterate through the collection of Contact items.
foreach (Contact result in contactQuery)
Console.WriteLine("Last Name: {0}; First Name: {1}",
result.LastName, result.FirstName);
}
这是查询生成器方法示例。
Dim firstName As String = "Frances"
Dim lastName As String = "Adams"
Using context As New AdventureWorksEntities()
' Get the contacts with the specified name.
Dim contactQuery As ObjectQuery(Of Contact) = context.Contacts.Where("it.LastName = @ln AND it.FirstName = @fn", _
New ObjectParameter("ln", lastName), New ObjectParameter("fn", firstName))
' Iterate through the collection of Contact items.
For Each result As Contact In contactQuery
Console.WriteLine("Last Name: {0}; First Name: {1}", result.LastName, result.FirstName)
Next
End Using
string firstName = @"Frances";
string lastName = @"Adams";
using (AdventureWorksEntities context =
new AdventureWorksEntities())
{
// Get the contacts with the specified name.
ObjectQuery<Contact> contactQuery = context.Contacts
.Where("it.LastName = @ln AND it.FirstName = @fn",
new ObjectParameter("ln", lastName),
new ObjectParameter("fn", firstName));
// Iterate through the collection of Contact items.
foreach (Contact result in contactQuery)
Console.WriteLine("Last Name: {0}; First Name: {1}",
result.LastName, result.FirstName);
}
另请参见
任务
如何:执行返回实体类型对象的查询(实体框架)
如何:执行返回匿名类型集合的查询(实体框架)
如何:执行返回基元类型集合的查询(实体框架)