制定聯結和交叉乘積查詢

下列範例顯示如何合併多張資料表中的結果。

範例 1

下列範例在 Visual Basic 的 From 子句 (在 C# 中是 from 子句) 中使用外部索引鍵巡覽,來選取倫敦客戶的所有訂單。

var infoQuery =
    from cust in db.Customers
    from ord in cust.Orders
    where cust.City == "London"
    select ord;
Dim infoQuery = _
From cust In db.Customers, ord In cust.Orders _
Where cust.City = "London" _
Select ord

範例 2

下列範例在 Visual Basic 的 Where 子句 (在 C# 中是 where 子句) 中使用外部索引鍵巡覽,來篩選 Supplier 在美國的缺貨 Products

var infoQuery =
    from prod in db.Products
    where prod.Supplier.Country == "USA" && prod.UnitsInStock == 0
    select prod;
Dim infoQuery = _
    From prod In db.Products _
    Where prod.Supplier.Country = "USA" AndAlso _
        CShort(prod.UnitsInStock) = 0 _
    Select prod

範例 3

下列範例在 Visual Basic 的 From 子句 (在 C# 中是 from 子句) 中使用外部索引鍵巡覽,來篩選西雅圖的員工並列出其所在地區。

var infoQuery =
    from emp in db.Employees
    from empterr in emp.EmployeeTerritories
    where emp.City == "Seattle"
    select new
    {
        emp.FirstName,
        emp.LastName,
        empterr.Territory.TerritoryDescription
    };

範例 4

下列範例在 Visual Basic 的 Select 子句 (在 C# 中是 select 子句) 中使用外部索引鍵巡覽,來篩選員工配對 (其中一名員工向另一名員工報告),以及兩名員工都來自同一個 City 的地點。

var infoQuery =
    from emp1 in db.Employees
    from emp2 in emp1.Employees
    where emp1.City == emp2.City
    select new
    {
        FirstName1 = emp1.FirstName,
        LastName1 = emp1.LastName,
        FirstName2 = emp2.FirstName,
        LastName2 = emp2.LastName,
        emp1.City
    };

Dim infoQuery = _
    From e1 In db.Employees, e2 In e1.Employees _
    Where e1.City = e2.City _
    Select FirstName1 = e1.FirstName, _
        LastName1 = e1.LastName, FirstName2 = e2.FirstName, _
        LastName2 = e2.LastName, e1.City

範例 5

下列 Visual Basic 範例會尋找所有客戶和訂單、確定訂單與客戶相符,而且確保該清單中的每位客戶都提供有連絡人名稱。

Dim q1 = From c In db.Customers, o In db.Orders _
         Where c.CustomerID = o.CustomerID _
         Select c.CompanyName, o.ShipRegion

' Note that because the O/R designer generates class
' hierarchies for database relationships for you,
' the following code has the same effect as the above
' and is shorter:

Dim q2 = From c In db.Customers, o In c.Orders _
         Select c.CompanyName, o.ShipRegion

For Each nextItem In q2
    Console.WriteLine("{0}   {1}", nextItem.CompanyName, _
        nextItem.ShipRegion)
Next

範例 6

下列範例明確聯結 (Join) 兩張資料表,並規劃來自這兩張資料表的結果。

var q =
    from c in db.Customers
    join o in db.Orders on c.CustomerID equals o.CustomerID
        into orders
    select new { c.ContactName, OrderCount = orders.Count() };
Dim q = From c In db.Customers _
        Group Join o In db.Orders On c.CustomerID Equals o.CustomerID _
            Into orders = Group _
        Select c.ContactName, OrderCount = orders.Count()

範例 7

下列範例明確聯結三張資料表,並規劃來自每張資料表的結果。

var q =
    from c in db.Customers
    join o in db.Orders on c.CustomerID equals o.CustomerID
        into ords
    join e in db.Employees on c.City equals e.City into emps
    select new
    {
        c.ContactName,
        ords = ords.Count(),
        emps = emps.Count()
    };
Dim q = From c In db.Customers _
        Group Join o In db.Orders On c.CustomerID Equals o.CustomerID _
            Into ords = Group _
        Group Join e In db.Employees On c.City Equals e.City _
            Into emps = Group _
        Select c.ContactName, ords = ords.Count(), emps = emps.Count()

範例 8

下列範例顯示如何使用 LEFT OUTER JOIN 來達成 DefaultIfEmpty()。 如果 DefaultIfEmpty() 沒有 Order,則 Employee 方法會傳回 null。

var q =
    from e in db.Employees
    join o in db.Orders on e equals o.Employee into ords
        from o in ords.DefaultIfEmpty()
        select new { e.FirstName, e.LastName, Order = o };
Dim q = From e In db.Employees() _
        Group Join o In db.Orders On e Equals o.Employee Into ords _
            = Group _
        From o In ords.DefaultIfEmpty() _
        Select e.FirstName, e.LastName, Order = o

範例 9

下列範例會規劃透過聯結產生的 let 運算式。

var q =
    from c in db.Customers
    join o in db.Orders on c.CustomerID equals o.CustomerID
        into ords
    let z = c.City + c.Country
        from o in ords
        select new { c.ContactName, o.OrderID, z };
Dim q = From c In db.Customers _
        Group Join o In db.Orders On c.CustomerID Equals o.CustomerID _
            Into ords = Group _
        Let z = c.City + c.Country _
        From o In ords _
        Select c.ContactName, o.OrderID, z

範例 10

下列範例顯示具有複合索引鍵的 join

var q =
    from o in db.Orders
    from p in db.Products
    join d in db.OrderDetails
        on new { o.OrderID, p.ProductID } equals new
    {
        d.OrderID,
        d.ProductID
    } into details
        from d in details
        select new { o.OrderID, p.ProductID, d.UnitPrice };
Dim q = From o In db.Orders _
        From p In db.Products _
        Group Join d In db.OrderDetails On New With {o.OrderID, _
            p.ProductID} _
            Equals New With {d.OrderID, d.ProductID} Into details _
                = Group _
        From d In details _
        Select o.OrderID, p.ProductID, d.UnitPrice

範例 11

下列範例顯示如何建構 join,其中另一端可以為 null,而另一端則不可以。

var q =
    from o in db.Orders
    join e in db.Employees
        on o.EmployeeID equals (int?)e.EmployeeID into emps
        from e in emps
        select new { o.OrderID, e.FirstName };
Dim q = From o In db.Orders _
        Group Join e In db.Employees On o.EmployeeID _
            Equals e.EmployeeID Into emps = Group _
        From e In emps _
        Select o.OrderID, e.FirstName

另請參閱