Поделиться через


Join Operations

A join of two data sources is the association of objects in one data source with objects that share a common attribute in another data source.

Joining is an important operation in queries that target data sources whose relationships to each other cannot be followed directly. In object-oriented programming, this could mean a correlation between objects that is not modeled, such as the backwards direction of a one-way relationship. An example of a one-way relationship is a Customer class that has a property of type City, but the City class does not have a property that is a collection of Customer objects. If you have a list of City objects and you want to find all the customers in each city, you could use a join operation to find them.

The join methods provided in the LINQ framework are Join and GroupJoin. These methods perform equijoins, or joins that match two data sources based on equality of their keys. (For comparison, Transact-SQL supports join operators other than 'equals', for example the 'less than' operator.) In relational database terms, Join implements an inner join, a type of join in which only those objects that have a match in the other data set are returned. The GroupJoin method has no direct equivalent in relational database terms, but it implements a superset of inner joins and left outer joins. A left outer join is a join that returns each element of the first (left) data source, even if it has no correlated elements in the other data source.

The following illustration shows a conceptual view of two sets and the elements within those sets that are included in either an inner join or a left outer join.

Two overlapping circles showing inner/outer.

Methods

Method Name

Description

C# Query Expression Syntax

Visual Basic Query Expression Syntax

More Information

Join

Joins two sequences based on key selector functions and extracts pairs of values.

join … in … on … equals …

From x In …, y In … Where x.a = y.a

-or-

Join … [As …]In … On …

Enumerable.Join

Queryable.Join

GroupJoin

Joins two sequences based on key selector functions and groups the resulting matches for each element.

join … in … on … equals … into …

Group Join … In … On …

Enumerable.GroupJoin

Queryable.GroupJoin

See Also

Tasks

Formulate Joins and Cross-Product Queries

How to: Join by Using Composite Keys (C# Programming Guide)

How to: Join Content from Dissimilar Files (LINQ)

How to: Order the Results of a Join Clause (C# Programming Guide)

How to: Perform Custom Join Operations (C# Programming Guide)

How to: Perform Grouped Joins (C# Programming Guide)

How to: Perform Inner Joins (C# Programming Guide)

How to: Perform Left Outer Joins (C# Programming Guide)

How to: Populate Object Collections from Multiple Sources (LINQ)

Reference

System.Linq

Anonymous Types (C# Programming Guide)

join clause (C# Reference)

Join Clause (Visual Basic)

Concepts

Standard Query Operators Overview

Anonymous Types (Visual Basic)