Share via


Sorgu İfadesi Söz Dizimi Örnekleri: İlişkilerde Gezinme

Entity Framework'teki gezinti özellikleri, ilişkilendirmenin sonundaki varlıkları bulmak için kullanılan kısayol özellikleridir. Gezinti özellikleri, kullanıcının bir varlıktan diğerine veya bir varlıktan ilişkilendirme kümesi aracılığıyla ilgili varlıklara gitmesine olanak tanır. Bu konu başlığında, LINQ to Entities sorgularındaki gezinti özelliklerinde ilişkilerde gezinmeye yönelik sorgu ifadesi söz diziminde örnekler verilmiştir.

Bu örneklerde kullanılan AdventureWorks Satış Modeli, AdventureWorks örnek veritabanındaki Contact, Address, Product, SalesOrderHeader ve SalesOrderDetail tablolarından oluşturulur.

Bu konudaki örneklerde aşağıdaki using/Imports deyimler kullanılır:

using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Objects;
using System.Globalization;
using System.Data.EntityClient;
using System.Data.SqlClient;
using System.Data.Common;

Option Explicit On
Option Strict On
Imports System.Data.Objects
Imports System.Globalization

Örnek 1

Aşağıdaki örnek, tüm kişi kimliklerini ve soyadı "Zhou" olan her kişi için ödenecek toplam toplamı almak için yöntemini kullanır Select . Contact.SalesOrderHeader Gezinti özelliği, her kişi için nesne koleksiyonunu SalesOrderHeader almak için kullanılır. Sum yöntemi gezinti özelliğini kullanarak her ilgili kişinin tüm siparişlerinin toplamının toplamını alırContact.SalesOrderHeader.

string lastName = "Zhou";
using (AdventureWorksEntities context = new AdventureWorksEntities())
{
    ObjectSet<Contact> contacts = context.Contacts;

    var ordersQuery = from contact in contacts
                      where contact.LastName == lastName
                      select new
                      {
                          ContactID = contact.ContactID,
                          Total = contact.SalesOrderHeaders.Sum(o => o.TotalDue)
                      };

    foreach (var contact in ordersQuery)
    {
        Console.WriteLine("Contact ID: {0} Orders total: {1}", contact.ContactID, contact.Total);
    }
}
Dim lastName = "Zhou"
Using context As New AdventureWorksEntities
    Dim contacts As ObjectSet(Of Contact) = context.Contacts

    Dim ordersQuery = From contact In contacts _
                      Where contact.LastName = lastName _
                      Select New With _
                              {.ContactID = contact.ContactID, _
                              .Total = contact.SalesOrderHeaders.Sum(Function(o) o.TotalDue)}

    For Each order In ordersQuery
        Console.WriteLine("Contact ID: {0} Orders total: {1}", order.ContactID, order.Total)
    Next
End Using

Örnek 2

Aşağıdaki örnek, soyadı "Zhou" olan kişilerin tüm siparişlerini alır. Contact.SalesOrderHeader Gezinti özelliği, her kişi için nesne koleksiyonunu SalesOrderHeader almak için kullanılır. Kişinin adı ve siparişleri anonim bir türde döndürülür.

string lastName = "Zhou";
using (AdventureWorksEntities context = new AdventureWorksEntities())
{
    ObjectSet<Contact> contacts = context.Contacts;

    var ordersQuery = from contact in contacts
                      where contact.LastName == lastName
                      select new { LastName = contact.LastName, Orders = contact.SalesOrderHeaders };

    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("");
    }
}
Dim lastName = "Zhou"
Using context As New AdventureWorksEntities
    Dim contacts As ObjectSet(Of Contact) = context.Contacts

    Dim ordersQuery = From contact In contacts _
                      Where contact.LastName = lastName _
                      Select New With _
                              {.LastName = contact.LastName, _
                               .Orders = contact.SalesOrderHeaders}

    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

Örnek 3

Aşağıdaki örnekte SalesOrderHeader.Address ve SalesOrderHeader.Contact gezinti özellikleri, her bir sırayla ilişkili ve Contact nesnelerinin Address koleksiyonunu alır. İlgili kişinin soyadı, sokak adresi, satış siparişi numarası ve Seattle şehrine her siparişin ödenmesi gereken toplam adsız bir türde döndürülür.

string city = "Seattle";
using (AdventureWorksEntities context = new AdventureWorksEntities())
{
    ObjectSet<SalesOrderHeader> orders = context.SalesOrderHeaders;

    var ordersQuery = from order in orders
                      where order.Address.City == city
                      select new
                      {
                          ContactLastName = order.Contact.LastName,
                          ContactFirstName = order.Contact.FirstName,
                          StreetAddress = order.Address.AddressLine1,
                          OrderNumber = order.SalesOrderNumber,
                          TotalDue = order.TotalDue
                      };

    foreach (var orderInfo in ordersQuery)
    {
        Console.WriteLine("Name: {0}, {1}", orderInfo.ContactLastName, orderInfo.ContactFirstName);
        Console.WriteLine("Street address: {0}", orderInfo.StreetAddress);
        Console.WriteLine("Order number: {0}", orderInfo.OrderNumber);
        Console.WriteLine("Total Due: {0}", orderInfo.TotalDue);
        Console.WriteLine("");
    }
}
Dim city = "Seattle"
Using context As New AdventureWorksEntities

    Dim orders As ObjectSet(Of SalesOrderHeader) = context.SalesOrderHeaders

    Dim ordersQuery = From order In orders _
                      Where order.Address.City = city _
                      Select New With { _
                                     .ContactLastName = order.Contact.LastName, _
                                     .ContactFirstName = order.Contact.FirstName, _
                                     .StreetAddress = order.Address.AddressLine1, _
                                     .OrderNumber = order.SalesOrderNumber, _
                                     .TotalDue = order.TotalDue}

    For Each orderInfo In ordersQuery
        Console.WriteLine("Name: {0}, {1}", orderInfo.ContactLastName, orderInfo.ContactFirstName)
        Console.WriteLine("Street address: {0}", orderInfo.StreetAddress)
        Console.WriteLine("Order number: {0}", orderInfo.OrderNumber)
        Console.WriteLine("Total Due: {0}", orderInfo.TotalDue)
        Console.WriteLine("")
    Next

End Using

Örnek 4

Aşağıdaki örnek, 1 Aralık 2003'tan sonra yapılan siparişleri bulmak için yöntemini kullanır Where ve ardından her siparişin order.SalesOrderDetail ayrıntılarını almak için gezinti özelliğini kullanır.

using (AdventureWorksEntities context = new AdventureWorksEntities())
{
    IQueryable<SalesOrderHeader> query =
        from order in context.SalesOrderHeaders
        where order.OrderDate >= new DateTime(2003, 12, 1)
        select order;

    Console.WriteLine("Orders that were made after December 1, 2003:");
    foreach (SalesOrderHeader order in query)
    {
        Console.WriteLine("OrderID {0} Order date: {1:d} ",
            order.SalesOrderID, order.OrderDate);
        foreach (SalesOrderDetail orderDetail in order.SalesOrderDetails)
        {
            Console.WriteLine("  Product ID: {0} Unit Price {1}",
                orderDetail.ProductID, orderDetail.UnitPrice);
        }
    }
}
Using context As New AdventureWorksEntities
    Dim orders As ObjectSet(Of SalesOrderHeader) = context.SalesOrderHeaders

    Dim query = _
        From order In orders _
        Where order.OrderDate >= New DateTime(2003, 12, 1) _
        Select order

    Console.WriteLine("Orders that were made after December 1, 2003:")
    For Each order In query
        Console.WriteLine("OrderID {0} Order date: {1:d} ", _
                order.SalesOrderID, order.OrderDate)
        For Each orderDetail In order.SalesOrderDetails
            Console.WriteLine("  Product ID: {0} Unit Price {1}", _
                orderDetail.ProductID, orderDetail.UnitPrice)
        Next
    Next
End Using

Ayrıca bkz.