How to use LINQ inner join clause based on a composite key

Web developer uses LINQ join clause when he/she wants to associate elements from different source sequences that have no direct relationship in the object model. For example, an electronic devices distributor might have a list of suppliers of a certain product, and a list of buyers. A join clause can be used, for example, to create a list of the suppliers and buyers of that product who are all in the same specified region

A join clause takes two source sequences as input. The elements in each sequence must either be or contain a property that can be compared to a corresponding property in the other sequence. The join clause compares the specified keys for equality by using the special equals keyword. All joins performed by the join clause are equijoins. The shape of the output of a join clause depends on the specific type of join Web developer is performing.

When Web developer wants to compare elements based on multiple properties he/she has to use LIQN inner join clause based on a composite key. Web developer can do this, by specifying the key selector function for each collection to return an anonymous type that consists of properties he/she wants to compare. If he/she labels the properties, they have to have the same label in each key’s anonymous type.

The following example uses a list of Student objects and a list of TennisPlayer object to determine which students are also tennis players. Both of these types have a FirstName and a LastName property of type string. The function that create the join keys from each list’s elements return an anonymous type that consists of the FirstName and LastName properties of each element. The join operator compares these composite keys for equality and returns pairs of objects from each list where both the first and the last name match.

 

class Student

{

public string FirstName { get; set; }

public string LastName { get; set; }

public int StudentID { get; set; }

}

 

class TennisPlayer

 

{

 

public string FirstName { get; set; }

 

public string LastName { get; set; }

 

public int PlayerID { get; set; }

 

}

 

// <summary>

// Performs a join operation using a composite key.

// </summary>

public static void CompositeKeyJoinExample()

{

// Create a list of students.

List<Student> students = new List<Student> {

new Student { FirstName = “Gregory”, LastName = “Croft”, StudentID = 444123 },

new Student { FirstName = “Bill”, LastName = “Yack”, StudentID = 194568 },

new Student { FirstName = “Jeremy”, LastName = “Little”, StudentID = 902011 },

new Student { FirstName = “Phil”, LastName = “Winstanley”, StudentID = 347812 } };

 

// Create a list of tennis players .

List<TennisPlayer> tennisplayers = new List<TennisPlayer> {

new Student { FirstName = ” Scott “, LastName = “Gate”, PlayerID = 5543 },

new Student { FirstName = “Jeremy”, LastName = “Little”, PlayerID = 7091 },

new Student FirstName = “Phil”, LastName = “Winstanley”, PlayerID = 8867 } };

 

// Join the two data sources based on a composite key consisting of first and last name,

// to determine which students are also tennis players.

IEnumerable<string> query = from student in students

join tennisplayer in tennisplayers

on new { student.FirstName, student.LastName }

equals new { tennisplayer.FirstName, tennisplayer.LastName }

select student.FirstName + ” ” + student.LastName;

 

Console.WriteLine(“The following people are both students and tennis players:”);

foreach (string name in query)

Console.WriteLine(name);

}

// This code produces the following output:

//

// The following people are both employees and students:

// Jeremy Little

// Phil Winstanley

If you want to host ASP.NET 4 application then you will need ASP.NET 4.0 hosting provider which supports .NET 4.0 Framework.