업데이트: 2007년 11월
키가 같은지 여부에 따라 두 시퀀스의 요소를 연관시키고 결과를 그룹화합니다. 기본 같음 비교자를 사용하여 키를 비교합니다.
네임스페이스:
System.Linq 어셈블리:
System.Core(System.Core.dll)
<ExtensionAttribute> _
Public Shared Function GroupJoin(Of TOuter, TInner, TKey, TResult) ( _
outer As IEnumerable(Of TOuter), _
inner As IEnumerable(Of TInner), _
outerKeySelector As Func(Of TOuter, TKey), _
innerKeySelector As Func(Of TInner, TKey), _
resultSelector As Func(Of TOuter, IEnumerable(Of TInner), TResult) _
) As IEnumerable(Of TResult)
Dim outer As IEnumerable(Of TOuter)
Dim inner As IEnumerable(Of TInner)
Dim outerKeySelector As Func(Of TOuter, TKey)
Dim innerKeySelector As Func(Of TInner, TKey)
Dim resultSelector As Func(Of TOuter, IEnumerable(Of TInner), TResult)
Dim returnValue As IEnumerable(Of TResult)
returnValue = outer.GroupJoin(inner, _
outerKeySelector, innerKeySelector, _
resultSelector)
public static IEnumerable<TResult> GroupJoin<TOuter, TInner, TKey, TResult>(
this IEnumerable<TOuter> outer,
IEnumerable<TInner> inner,
Func<TOuter, TKey> outerKeySelector,
Func<TInner, TKey> innerKeySelector,
Func<TOuter, IEnumerable<TInner>, TResult> resultSelector
)
[ExtensionAttribute]
public:
generic<typename TOuter, typename TInner, typename TKey, typename TResult>
static IEnumerable<TResult>^ GroupJoin(
IEnumerable<TOuter>^ outer,
IEnumerable<TInner>^ inner,
Func<TOuter, TKey>^ outerKeySelector,
Func<TInner, TKey>^ innerKeySelector,
Func<TOuter, IEnumerable<TInner>^, TResult>^ resultSelector
)
J#에서는 제네릭 API를 사용할 수 있지만 새로 선언할 수는 없습니다.
JScript에서는 제네릭 형식이나 메서드를 지원하지 않습니다.
Type 매개 변수
- TOuter
첫 번째 시퀀스 요소의 형식입니다.
- TInner
두 번째 시퀀스 요소의 형식입니다.
- TKey
키 선택기 함수에서 반환하는 키의 형식입니다.
- TResult
결과 요소의 형식입니다.
사용 정보
Visual Basic 및 C#에서는 이 메서드를 IEnumerable<(Of <(TOuter>)>) 형식의 모든 개체에서 인스턴스 메서드로 호출할 수 있습니다. 인스턴스 메서드 구문을 사용하여 이 메서드를 호출할 경우에는 첫 번째 매개 변수를 생략합니다. 자세한 내용은 확장 메서드(Visual Basic) 또는 확장 메서드(C# 프로그래밍 가이드)를 참조하십시오.
| 예외 | 상황 |
|---|
| ArgumentNullException | outer, inner, outerKeySelector, innerKeySelector 또는 resultSelector가 nullNothingnullptrNull 참조(Visual Basic의 경우 Nothing)인 경우
|
이 메서드는 지연된 실행을 사용하여 구현됩니다. 해당 작업을 수행하는 데 필요한 모든 정보가 저장된 개체가 즉시 반환됩니다. 이 메서드에서 나타내는 쿼리는 해당 GetEnumerator 메서드를 직접 호출하거나, foreach(Visual C#의 경우) 또는 For Each(Visual Basic의 경우)를 사용하여 개체를 열거할 때까지 실행되지 않습니다.
기본 같음 비교자인 Default를 사용하여 키를 해시 및 비교합니다.
GroupJoin은 계층적인 결과를 생성하므로 outer의 요소가 inner의 일치하는 요소 컬렉션과 쌍을 이룹니다. GroupJoin을 통해 각 outer 요소의 전체 일치 집합에 대한 결과를 생성할 수 있습니다.
참고: |
|---|
outer의 특정 요소에 대해 inner에 연관된 요소가 없으면 해당 요소의 일치 시퀀스는 비어 있지만 결과에 그대로 표시됩니다. |
resultSelector 함수는 각 outer 요소 및 outer 요소와 일치하는 모든 inner 요소의 컬렉션에 대해 한 번만 호출됩니다. 이와 달리 Join 메서드에서는 outer의 요소 하나와 inner의 요소 하나가 들어 있는 쌍에 대해 결과 선택기 함수가 호출됩니다.
GroupJoin에서는 outer의 요소 순서가 유지되며, outer의 각 요소에 대해 inner의 일치 요소 순서가 유지됩니다.
기존의 관계형 데이터베이스에는 GroupJoin에 직접 해당하는 용어가 없습니다. 그러나 이 메서드는 내부 조인과 왼쪽 우선 외부 조인의 상위 집합을 구현합니다. 그룹화 조인을 통해 이러한 작업을 모두 작성할 수 있습니다. 조인 작업을 참조하십시오.
쿼리 식 구문에서 join ¢¢ç¦ into(Visual C#의 경우) 또는 Group Join(Visual Basic의 경우) 절은 GroupJoin 호출로 변환됩니다.
다음 코드 예제에서는 GroupJoin<(Of <(TOuter, TInner, TKey, TResult>)>)(IEnumerable<(Of <(TOuter>)>), IEnumerable<(Of <(TInner>)>), Func<(Of <(TOuter, TKey>)>), Func<(Of <(TInner, TKey>)>), Func<(Of <(TOuter, IEnumerable<(Of <(TInner>)>), TResult>)>))을 사용하여 두 시퀀스에 대해 그룹화 조인을 수행하는 방법을 보여 줍니다.
Structure Person
Public Name As String
End Structure
Structure Pet
Public Name As String
Public Owner As Person
End Structure
Sub GroupJoinEx1()
Dim magnus As New Person With {.Name = "Hedlund, Magnus"}
Dim terry As New Person With {.Name = "Adams, Terry"}
Dim charlotte As New Person With {.Name = "Weiss, Charlotte"}
Dim barley As New Pet With {.Name = "Barley", .Owner = terry}
Dim boots As New Pet With {.Name = "Boots", .Owner = terry}
Dim whiskers As New Pet With {.Name = "Whiskers", .Owner = charlotte}
Dim daisy As New Pet With {.Name = "Daisy", .Owner = magnus}
Dim people As New List(Of Person)(New Person() {magnus, terry, charlotte})
Dim pets As New List(Of Pet)(New Pet() {barley, boots, whiskers, daisy})
' Create a collection where each element is an anonymous type
' that contains a Person's name and a collection of names of
' the pets that are owned by them.
Dim query = _
people.GroupJoin(pets, _
Function(person) person, _
Function(pet) pet.Owner, _
Function(person, petCollection) _
New With {.OwnerName = person.Name, _
.Pets = petCollection.Select( _
Function(pet) pet.Name)})
Dim output As New System.Text.StringBuilder
For Each obj In query
' Output the owner's name.
output.AppendLine(obj.OwnerName & ":")
' Output each of the owner's pet's names.
For Each pet As String In obj.Pets
output.AppendLine(" " & pet)
Next
Next
' Display the output.
MsgBox(output.ToString)
End Sub
' This code produces the following output:
'
' Hedlund, Magnus
' Daisy
' Adams, Terry
' Barley
' Boots
' Weiss, Charlotte
' Whiskers
class Person
{
public string Name { get; set; }
}
class Pet
{
public string Name { get; set; }
public Person Owner { get; set; }
}
public static void GroupJoinEx1()
{
Person magnus = new Person { Name = "Hedlund, Magnus" };
Person terry = new Person { Name = "Adams, Terry" };
Person charlotte = new Person { Name = "Weiss, Charlotte" };
Pet barley = new Pet { Name = "Barley", Owner = terry };
Pet boots = new Pet { Name = "Boots", Owner = terry };
Pet whiskers = new Pet { Name = "Whiskers", Owner = charlotte };
Pet daisy = new Pet { Name = "Daisy", Owner = magnus };
List<Person> people = new List<Person> { magnus, terry, charlotte };
List<Pet> pets = new List<Pet> { barley, boots, whiskers, daisy };
// Create a list where each element is an anonymous
// type that contains a person's name and
// a collection of names of the pets they own.
var query =
people.GroupJoin(pets,
person => person,
pet => pet.Owner,
(person, petCollection) =>
new
{
OwnerName = person.Name,
Pets = petCollection.Select(pet => pet.Name)
});
foreach (var obj in query)
{
// Output the owner's name.
Console.WriteLine("{0}:", obj.OwnerName);
// Output each of the owner's pet's names.
foreach (string pet in obj.Pets)
{
Console.WriteLine(" {0}", pet);
}
}
}
/*
This code produces the following output:
Hedlund, Magnus:
Daisy
Adams, Terry:
Barley
Boots
Weiss, Charlotte:
Whiskers
*/
Windows Vista, Windows XP SP2, Windows Server 2003, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC
.NET Framework 및 .NET Compact Framework에서 모든 플랫폼의 전체 버전을 지원하지는 않습니다. 지원되는 버전의 목록을 보려면 .NET Framework 시스템 요구 사항을 참조하십시오.
.NET Framework
3.5에서 지원
.NET Compact Framework
3.5에서 지원
참조
기타 리소스