방법: 탐색 속성을 사용하여 관계 탐색(Entity Framework)
이 항목에서는 탐색 속성을 통해 관계를 탐색하는 방법을 보여 줍니다. 자세한 내용은 탐색 속성(EDM)을 참조하십시오. 예제에서는 성이 "Zhou"인 연락처의 모든 주문을 가져옵니다. Contact.SalesOrderHeader 탐색 속성을 사용하여 각 연락처의 SalesOrderHeader 개체 컬렉션을 가져옵니다. 다음의 엔터티 프레임워크 쿼리 기술을 각각 사용하여 동일한 예제를 보여 줍니다.
LINQ to Entities
ObjectQuery<T>를 사용한 Entity SQL
ObjectQuery<T>의 쿼리 작성기 메서드
이 항목의 예제는 Adventure Works Sales 모델을 기반으로 합니다. 이 예제의 코드를 실행하려면 프로젝트에 AdventureWorks Sales 모델을 추가하고 엔터티 프레임워크를 사용하도록 프로젝트를 구성해야 합니다. 이렇게 하려면 방법: Entity Framework 프로젝트 수동 구성 및 방법: 엔터티 데이터 모델 수동 정의(Entity Framework)의 절차를 수행합니다. 엔터티 데이터 모델 마법사를 사용하여 AdventureWorks Sales 모델을 정의할 수도 있습니다. 자세한 내용은 방법: 엔터티 데이터 모델 마법사 사용(Entity Framework)을 참조하십시오.
예제
다음은 LINQ to Entities 예제입니다.
Using AWEntities As New AdventureWorksEntities
Dim contacts As ObjectQuery(Of Contact) = AWEntities.Contact
Dim ordersQuery = From contact In contacts _
Where contact.LastName = "Zhou" _
Select New With _
{.LastName = contact.LastName, _
.Orders = contact.SalesOrderHeader}
For Each order In ordersQuery
Console.WriteLine("Name: {0}", order.LastName)
For Each orderInfo In order.Orders
Console.WriteLine("Order ID: {0}, Order date: {1}, Total Due: {2}", _
orderInfo.SalesOrderID, orderInfo.OrderDate, orderInfo.TotalDue)
Next
Console.WriteLine("")
Next
End Using
using (AdventureWorksEntities AWEntities = new AdventureWorksEntities())
{
ObjectQuery<Contact> contacts = AWEntities.Contact;
var ordersQuery = from contact in contacts
where contact.LastName == "Zhou"
select new { LastName = contact.LastName, Orders = contact.SalesOrderHeader };
foreach (var order in ordersQuery)
{
Console.WriteLine("Name: {0}", order.LastName);
foreach (SalesOrderHeader orderInfo in order.Orders)
{
Console.WriteLine("Order ID: {0}, Order date: {1}, Total Due: {2}",
orderInfo.SalesOrderID, orderInfo.OrderDate, orderInfo.TotalDue);
}
Console.WriteLine("");
}
}
다음은 Entity SQL 예제입니다.
Using advWorksContext As New AdventureWorksEntities
Dim esqlQuery As String = "SELECT c.FirstName, c.SalesOrderHeader " & _
" FROM AdventureWorksEntities.Contact AS c where c.LastName = 'Zhou'"
Try
Dim objQuery As New ObjectQuery(Of DbDataRecord)(esqlQuery, advWorksContext)
For Each rec As DbDataRecord In objQuery
' Display contact's first name.
Console.WriteLine("First Name {0}: ", rec.Item(0))
Dim list As List(Of SalesOrderHeader) = DirectCast(rec.Item(1), List(Of SalesOrderHeader))
' Display SalesOrderHeader information
' associated with the contact.
For Each soh As SalesOrderHeader In list
Console.WriteLine(" Order ID: {0}, Order date: {1}, Total Due: {2}", _
soh.SalesOrderID, soh.OrderDate, soh.TotalDue)
Next
Next
Catch ex As EntityException
Console.WriteLine(ex.ToString())
Catch ex As InvalidOperationException
Console.WriteLine(ex.ToString())
End Try
End Using
using (AdventureWorksEntities advWorksContext =
new AdventureWorksEntities())
{
string esqlQuery = @"SELECT c.FirstName, c.SalesOrderHeader
FROM AdventureWorksEntities.Contact AS c where c.LastName = 'Zhou'";
try
{
foreach (DbDataRecord rec in
new ObjectQuery<DbDataRecord>(esqlQuery, advWorksContext))
{
// Display contact's first name.
Console.WriteLine("First Name {0}: ", rec[0]);
List<SalesOrderHeader> list = rec[1] as List<SalesOrderHeader>;
// Display SalesOrderHeader information
// associated with the contact.
foreach (SalesOrderHeader soh in list)
{
Console.WriteLine(" Order ID: {0}, Order date: {1}, Total Due: {2}",
soh.SalesOrderID, soh.OrderDate, soh.TotalDue);
}
}
}
catch (EntityException ex)
{
Console.WriteLine(ex.ToString());
}
catch (InvalidOperationException ex)
{
Console.WriteLine(ex.ToString());
}
}
다음은 쿼리 작성기 방법을 사용한 예제입니다.
Using advWorksContext As New AdventureWorksEntities()
Try
' Define a query that returns a nested
' DbDataRecord for the projection.
Dim query As ObjectQuery(Of DbDataRecord) = _
advWorksContext.Contact.Select("it.FirstName, " _
+ "it.LastName, it.SalesOrderHeader") _
.Where("it.LastName = 'Zhou'")
For Each rec As DbDataRecord In query.Execute(MergeOption.AppendOnly)
' Display contact's first name.
Console.WriteLine("First Name {0}: ", rec(0))
' Display SalesOrderHeader information
' associated with the contact.
For Each soh As SalesOrderHeader In CType(rec(2), List(Of SalesOrderHeader))
Console.WriteLine(" Order ID: {0}, " + _
"Order date: {1}, Total Due: {2}", _
soh.SalesOrderID, soh.OrderDate, soh.TotalDue)
Next
Next
Catch ex As EntitySqlException
Console.WriteLine(ex.ToString())
End Try
End Using
using (AdventureWorksEntities advWorksContext =
new AdventureWorksEntities())
{
try
{
// Define a query that returns a nested
// DbDataRecord for the projection.
ObjectQuery<DbDataRecord> query =
advWorksContext.Contact.Select("it.FirstName, "
+ "it.LastName, it.SalesOrderHeader")
.Where("it.LastName = 'Zhou'");
foreach (DbDataRecord rec in
query.Execute(MergeOption.AppendOnly))
{
// Display contact's first name.
Console.WriteLine("First Name {0}: ", rec[0]);
List<SalesOrderHeader> list = rec[2]
as List<SalesOrderHeader>;
// Display SalesOrderHeader information
// associated with the contact.
foreach (SalesOrderHeader soh in list)
{
Console.WriteLine(" Order ID: {0}, " +
"Order date: {1}, Total Due: {2}",
soh.SalesOrderID, soh.OrderDate, soh.TotalDue);
}
}
}
catch (EntitySqlException ex)
{
Console.WriteLine(ex.ToString());
}
}
참고 항목
작업
방법: 쿼리 경로를 사용하여 결과 모양 결정(Entity Framework)