クエリ式の構文例 : 要素演算子 (LINQ to Entities)

このトピックでは、クエリ式の構文で、First メソッドを使用して、AdventureWorks Sales Model を照会する例を取り上げます。 The AdventureWorks Sales Model used in these examples is built from the Contact, Address, Product, SalesOrderHeader, and SalesOrderDetail tables in the AdventureWorks sample database.

このトピックの例には、次の using/Imports ステートメントが使用されています。

Option Explicit On
Option Strict On
Imports System.Data.Objects
Imports System.Globalization
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;

First

次の例では、First メソッドを使用して、名が "Brooke" である最初の連絡先を取得します。

Dim firstName = "Brooke"
Using context As New AdventureWorksEntities
    Dim contacts As ObjectSet(Of Contact) = context.Contacts

    Dim query As Contact = ( _
        From cont In contacts _
        Where cont.FirstName = firstName _
        Select cont).First()

    Console.WriteLine("ContactID: " & query.ContactID)
    Console.WriteLine("FirstName: " & query.FirstName)
    Console.WriteLine("LastName: " & query.LastName)
End Using
string firstName = "Brooke";
using (AdventureWorksEntities context = new AdventureWorksEntities())
{
    ObjectSet<Contact> contacts = context.Contacts;

    Contact query = (
        from contact in contacts
        where contact.FirstName == firstName
        select contact)
        .First();

    Console.WriteLine("ContactID: " + query.ContactID);
    Console.WriteLine("FirstName: " + query.FirstName);
    Console.WriteLine("LastName: " + query.LastName);
}

参照

概念

LINQ to Entities でのクエリ