다음을 통해 공유


시퀀스에서 첫 번째 요소 반환

연산자를 First 사용하여 시퀀스의 첫 번째 요소를 반환합니다. 사용하는 First 쿼리는 즉시 실행됩니다.

비고

LINQ to SQL은 연산자를 Last 지원하지 않습니다.

예제 1

다음 코드는 테이블에서 첫 번째 Shipper 항목을 찾습니다.

Northwind 샘플 데이터베이스에 대해 이 쿼리를 실행하는 경우 결과는 다음과 같습니다.

ID = 1, Company = Speedy Express;

Shipper shipper = db.Shippers.First();
Console.WriteLine("ID = {0}, Company = {1}", shipper.ShipperID,
    shipper.CompanyName);
Dim shipper As Shipper = db.Shippers.First()
Console.WriteLine("ID = {0}, Company = {1}", shipper.ShipperID, _
        shipper.CompanyName)

예제 2

다음 코드는 Customer BONAP가 있는 단일 CustomerID를 찾습니다.

Northwind 샘플 데이터베이스에 대해 이 쿼리를 실행하면 결과는 다음과 같습니다 ID = BONAP, Contact = Laurence Lebihan.

Customer custQuery =
    (from custs in db.Customers
    where custs.CustomerID == "BONAP"
    select custs)
    .First();

Console.WriteLine("ID = {0}, Contact = {1}", custQuery.CustomerID,
    custQuery.ContactName);
Dim custquery As Customer = _
    (From c In db.Customers _
     Where c.CustomerID = "BONAP" _
     Select c) _
    .First()

Console.WriteLine("ID = {0}, Contact = {1}", custquery.CustomerID, _
    custquery.ContactName)

참고하십시오