射影の作成

以下の例では、C# の select ステートメントおよび Visual Basic の Select ステートメントを他の機能と組み合わせて、クエリの射影を作成する方法を示します。

例 1

次の例では、Visual Basic の Select 句 (C# では select 句) を使用して、Customers の連絡先の名前のシーケンスを返します。

var nameQuery =
    from cust in db.Customers
    select cust.ContactName;
Dim nameQuery = From cust In db.Customers _
                Select cust.ContactName

例 2

次の例では、Visual Basic の Select 句 (C# では select 句) と "匿名型" を使用して、Customers の連絡先名と電話番号のシーケンスを返します。

var infoQuery =
    from cust in db.Customers
    select new { cust.ContactName, cust.Phone };
Dim infoQuery = From cust In db.Customers _
                Select cust.ContactName, cust.Phone

例 3

次の例では、Visual Basic の Select 句 (C# では select 句) と "匿名型" を使用して、従業員の名前と電話番号のシーケンスを返します。 結果のシーケンスでは、FirstName フィールドと LastName フィールドは 1 つのフィールド (Name) に結合され、HomePhone フィールドは Phone という名前に変更されます。

var info2Query =
    from emp in db.Employees
    select new
    {
        Name = emp.FirstName + " " + emp.LastName,
        Phone = emp.HomePhone
    };
Dim info2Query = From emp In db.Employees _
                 Select Name = emp.FirstName & " " & emp.LastName, _
             Phone = emp.HomePhone

例 4

次の例では、Visual Basic の Select 句 (C# では select 句) と "匿名型" を使用して、すべての ProductID および HalfPrice という名前の計算値のシーケンスを返します。 この値は、UnitPrice を 2 で割った値に設定されます。

var specialQuery =
    from prod in db.Products
    select new { prod.ProductID, HalfPrice = prod.UnitPrice / 2 };
Dim specialQuery = From prod In db.Products _
                   Select prod.ProductID, HalfPrice = CDec(prod.UnitPrice) / 2

例 5

次の例では、Visual Basic の Select 句 (C# では select 句) と "条件付きステートメント" を使用して、製品名と製品の購入可能性のシーケンスを返します。

var prodQuery =
    from prod in db.Products
    select new
    {
        prod.ProductName,
        Availability =
            prod.UnitsInStock - prod.UnitsOnOrder < 0
        ? "Out Of Stock" : "In Stock"
    };
Dim prodQuery = From prod In db.Products _
                Select prod.ProductName, Availability = _
                    If(prod.UnitsInStock - prod.UnitsOnOrder < 0, _
                    "Out Of Stock", "In Stock")

例 6

次の例では、Visual Basic の Select 句 (C# では select 句) と "既知の型" (Name) を使用して、従業員の名前のシーケンスを返します。

public class Name
{
    public string FirstName = "";
    public string LastName = "";
}

 void empMethod()
 {
 Northwnd db = new Northwnd(@"c:\northwnd.mdf");
 var empQuery =
     from emp in db.Employees
     select new Name
     {
         FirstName = emp.FirstName,
         LastName = emp.LastName
     };
}
Public Class Name
    Public FirstName As String
    Public LastName As String
End Class

Dim db As New Northwnd("c:\northwnd.mdf")
Dim empQuery = From emp In db.Employees _
               Select New Name With {.FirstName = emp.FirstName, .LastName = _
                   emp.LastName}

例 7

次の例では、Visual Basic の SelectWhere (C# では selectwhere) を使用して、London の顧客の連絡先の名前の、"フィルター処理されたシーケンス" を返します。

var contactQuery =
    from cust in db.Customers
    where cust.City == "London"
    select cust.ContactName;
Dim contactQuery = _
    From cust In db.Customers _
    Where cust.City = "London" _
    Select cust.ContactName

例 8

次の例では、Visual Basic の Select 句 (C# では select 句) と "匿名型" を使用して、顧客に関するデータの "成型されたサブセット" を返します。

var custQuery =
    from cust in db.Customers
    select new
    {
        cust.CustomerID,
        CompanyInfo = new { cust.CompanyName, cust.City, cust.Country },
        ContactInfo = new { cust.ContactName, cust.ContactTitle }
    };
Dim custQuery = From cust In db.Customers _
                Select cust.CustomerID, CompanyInfo = New With {cust.CompanyName, _
                    cust.City, cust.Country}, ContactInfo = _
                    New With {cust.ContactName, cust.ContactTitle}

例 9

次の例では、入れ子になったクエリを使用して以下の結果を返します。

  • すべての注文および対応する OrderID のシーケンス。

  • 注文内で割引のある項目から成るサブシーケンス。

  • 出荷コストが含まれない場合に節約される費用。

var ordQuery =
    from ord in db.Orders
    select new
    {
        ord.OrderID,
        DiscountedProducts =
            from od in ord.OrderDetails
            where od.Discount > 0.0
            select od,
        FreeShippingDiscount = ord.Freight
    };
Dim ordQuery = From ord In db.Orders _
               Select ord.OrderID, DiscountedProducts = _
               (From od In ord.OrderDetails _
                Where od.Discount > 0.0 _
                Select od), _
           FreeShippingDiscount = ord.Freight

関連項目