다음을 통해 공유


방법: 쿼리에서 복합 키 처리

일부 연산자는 하나의 인수만 사용할 수 있습니다. 인수에 데이터베이스의 열이 두 개 이상 포함되어야 하는 경우 조합을 나타내는 익명 형식을 만들어야 합니다.

예제 1

다음 예제에서는 하나의 key 인수만 사용할 수 있는 연산자를 GroupBy 호출하는 쿼리를 보여줍니다.

        var query =
from cust in db.Customers
group cust.ContactName by new { City = cust.City, Region = cust.Region };

        foreach (var grp in query)
        {
            Console.WriteLine($"\nLocation Key: {grp.Key}");
            foreach (var listing in grp)
            {
                Console.WriteLine($"\t{listing}");
            }
        }
Dim query = _
From cust In db.Customers _
Group cust.ContactName By Key = New With {cust.City, cust.Region} _
Into Group

For Each grp In query
    Console.WriteLine("Location Key: {0}", grp.Key)
    For Each listing In grp.Group
        Console.WriteLine(vbTab & "0}", listing)
    Next
Next

예제 2

다음 예제에서와 같이 조인과 관련된 같은 상황이 존재합니다.

        var query =
from ord in db.Orders
from prod in db.Products
join det in db.OrderDetails
    on new { ord.OrderID, prod.ProductID } equals new { det.OrderID, det.ProductID }
    into details
from det in details
select new { ord.OrderID, prod.ProductID, det.UnitPrice };
Dim query = From ord In db.Orders, prod In db.Products _
            Join det In db.OrderDetails _
            On New With {ord.OrderID, prod.ProductID} Equals _
            New With {det.OrderID, det.ProductID} _
            Select ord.OrderID, prod.ProductID, det.UnitPrice

참고하십시오