Queryable.Join<TOuter, TInner, TKey, TResult> Method (IQueryable<TOuter>, IEnumerable<TInner>, Expression<Func<TOuter, TKey>>, Expression<Func<TInner, TKey>>, Expression<Func<TOuter, TInner, TResult>>)
Correlates the elements of two sequences based on matching keys. The default equality comparer is used to compare keys.
Assembly: System.Core (in System.Core.dll)
public static IQueryable<TResult> Join<TOuter, TInner, TKey, TResult>( this IQueryable<TOuter> outer, IEnumerable<TInner> inner, Expression<Func<TOuter, TKey>> outerKeySelector, Expression<Func<TInner, TKey>> innerKeySelector, Expression<Func<TOuter, TInner, TResult>> resultSelector )
Type Parameters
- TOuter
The type of the elements of the first sequence.
- TInner
The type of the elements of the second sequence.
- TKey
The type of the keys returned by the key selector functions.
- TResult
The type of the result elements.
Parameters
- outer
- Type: System.Linq.IQueryable<TOuter>
The first sequence to join.
- inner
- Type: System.Collections.Generic.IEnumerable<TInner>
The sequence to join to the first sequence.
- outerKeySelector
- Type: System.Linq.Expressions.Expression<Func<TOuter, TKey>>
A function to extract the join key from each element of the first sequence.
- innerKeySelector
- Type: System.Linq.Expressions.Expression<Func<TInner, TKey>>
A function to extract the join key from each element of the second sequence.
- resultSelector
- Type: System.Linq.Expressions.Expression<Func<TOuter, TInner, TResult>>
A function to create a result element from two matching elements.
Return Value
Type: System.Linq.IQueryable<TResult>An IQueryable<T> that has elements of type TResult obtained by performing an inner join on two sequences.
Usage Note
In Visual Basic and C#, you can call this method as an instance method on any object of type IQueryable<TOuter>. When you use instance method syntax to call this method, omit the first parameter. For more information, see Extension Methods (Visual Basic) or Extension Methods (C# Programming Guide).| Exception | Condition |
|---|---|
| ArgumentNullException |
outer or inner or outerKeySelector or innerKeySelector or resultSelector is null. |
This method has at least one parameter of type Expression<TDelegate> whose type argument is one of the Func<T, TResult> types. For these parameters, you can pass in a lambda expression and it will be compiled to an Expression<TDelegate>.
The Join<TOuter, TInner, TKey, TResult>(IQueryable<TOuter>, IEnumerable<TInner>, Expression<Func<TOuter, TKey>>, Expression<Func<TInner, TKey>>, Expression<Func<TOuter, TInner, TResult>>) method generates a MethodCallExpression that represents calling Join<TOuter, TInner, TKey, TResult>(IQueryable<TOuter>, IEnumerable<TInner>, Expression<Func<TOuter, TKey>>, Expression<Func<TInner, TKey>>, Expression<Func<TOuter, TInner, TResult>>) itself as a constructed generic method. It then passes the MethodCallExpression to the CreateQuery<TElement>(Expression) method of the IQueryProvider represented by the Provider property of the outer parameter.
The query behavior that occurs as a result of executing an expression tree that represents calling Join<TOuter, TInner, TKey, TResult>(IQueryable<TOuter>, IEnumerable<TInner>, Expression<Func<TOuter, TKey>>, Expression<Func<TInner, TKey>>, Expression<Func<TOuter, TInner, TResult>>) depends on the implementation of the type of the outer parameter. The expected behavior is that of an inner join. The outerKeySelector and innerKeySelector functions are used to extract keys from outer and inner, respectively. These keys are compared for equality to match elements from each sequence. A pair of elements is stored for each element in inner that matches an element in outer. Then the resultSelector function is invoked to project a result object from each pair of matching elements.
The following code example demonstrates how to use Join<TOuter, TInner, TKey, TResult>(IQueryable<TOuter>, IEnumerable<TInner>, Expression<Func<TOuter, TKey>>, Expression<Func<TInner, TKey>>, Expression<Func<TOuter, TInner, TResult>>) to perform an inner join of two sequences based on a common key.
class Person
{
public string Name { get; set; }
}
class Pet
{
public string Name { get; set; }
public Person Owner { get; set; }
}
public static void JoinEx1()
{
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 };
// Join the list of Person objects and the list of Pet objects
// to create a list of person-pet pairs where each element is
// an anonymous type that contains the name of pet and the name
// of the person that owns the pet.
var query = people.AsQueryable().Join(pets,
person => person,
pet => pet.Owner,
(person, pet) =>
new { OwnerName = person.Name, Pet = pet.Name });
foreach (var obj in query)
{
Console.WriteLine(
"{0} - {1}",
obj.OwnerName,
obj.Pet);
}
}
/*
This code produces the following output:
Hedlund, Magnus - Daisy
Adams, Terry - Barley
Adams, Terry - Boots
Weiss, Charlotte - Whiskers
*/
Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
I'm pertty new to LINQ myself, I've only started using it for the last 3 or 4 days, but even I can tell that the example provided isn't very good. That's because the Pet object itself has a reference to the Person object. So you could do the same thing without the Join() function by changing the foreach loop to:
foreach (Pet pet in pets)
{
Console.WriteLine("{0} - {1}", pet.Owner.Name, pet.Name);
}
That is much better than using the Join() method: it's both easier to read and would run faster. However, real world applications aren't usually like that. For instance, if you were getting this data from a relational database, the Pet wouldn't have a direct reference to the Person, but both Pet and Person would instead have IDs (or primary keys) that would be used to identify them and there would be a relation from the Pet table to the Person table using those IDs. So here's a more realistic scenario that better demonstrates the beneficial aspects of the Join() function:
class Person
{
public int PersonID { get; set; }
public string Name { get; set; }
}
class Pet
{
public int PetID { get; set; }
public string Name { get; set; }
public int OwnerID { get; set; }
}
public static void JoinEx1()
{
List<Person> people = new List<Person>();
List<Pet> pets = new List<Pet>();
people.Add(new Person { PersonID = 1, Name = "Hedlund, Magnus" });
people.Add(new Person { PersonID = 2, Name = "Adams, Terry" });
people.Add(new Person { PersonID = 3, Name = "Weiss, Charlotte" });
pets.Add(new Pet { PetID = 1, Name = "Barley", OwnerID = 2 });
pets.Add(new Pet { PetID = 2, Name = "Boots", OwnerID = 2 });
pets.Add(new Pet { PetID = 3, Name = "Whiskers", OwnerID = 3 });
pets.Add(new Pet { PetID = 4, Name = "Daisy", OwnerID = 1 });
// Join the list of Person objects and the list of Pet objects
// to create a list of person-pet pairs where each element is
// an anonymous type that contains the name of pet and the name
// of the person that owns the pet.
var query = people.AsQueryable().Join(pets,
person => person.PersonID,
pet => pet.OwnerID,
(person, pet) =>
new { OwnerName = person.Name, PetName = pet.Name });
foreach (var obj in query)
{
Console.WriteLine("{0} - {1}", obj.OwnerName, obj.PetName);
}
}
/*
This code produces the following output:
Hedlund, Magnus - Daisy
Adams, Terry - Barley
Adams, Terry - Boots
Weiss, Charlotte - Whiskers
*/
As you can see, the Pet object doesn't have a direct reference to the Person object, instead it contains the numeric ID of the Person (called OwnerID in Pet and PersonID in Person). Join() takes that numeric ID and uses it to create a new list of objects that contain the associated names we are looking for. There is now no obviously simpler or better way to perform the same task without the Join() function. If we removed the join function, we would have to do something like this:
foreach (Person owner in people)
{
foreach (Pet pet in pets)
{
if (pet.OwnerID == owner.PersonID)
{
Console.WriteLine("{0} - {1}", owner.Name, pet.Name);
}
}
}
Now that isn't really all that bad in this case. We're only going through a total of 12 iterations. But again, in the real world, you're usually dealing with much more than 3 people and 4 pets. Also, if this were being used against something like LINQ to SQL, which if I understand correctly would actually cause the join operation to be executed on the database server rather than on the client, then the performance advantage of the Join() function rather than iterating through some in memory collections of records is HUGE.
Anyways, I'm off to learn more about the coolness of LINQ (to SQL). I hope this proves helpful to someone. ;)
- 9/30/2011
- CptRobby