How to use LINQ multiple joins

Web developers use LINQ join clause when they want 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 developers are performing.

Any number of join operations can be appended to each other to perform a multiple join. Each join clause in C# correlates a specified data source with the results of the previous join.

The following example creates three collections: a list of Student objects, a list of Album objects, and a list of Movie objects.

The first join clause in C# matches students and albums based on a Student object matching Book.Owner. It returns a sequence of anonymous types that contain the Student object and Album.Title.

The second join clause in C# correlates the anonymous types returned by the first join with Movie objects in the supplied list of movies, based on a composite key that consists of the Owner property of type Student, and the first letter of the album’s title. It returns a sequence of anonymous types that contain the Album.Title and Movie.Title properties from each matching pair. Because this is an inner join, only those objects from the first data source that have a match in the second data source are returned.

 

class Student

{

public string FirtsName {get;set;}

public string LastName {get;set;}

}

 

class Media

{

public string Title {get;set;}

public Student Owner {get;set;}

}

 

class Album : Media

{

}

 

class Movie : Media

{

}

 

public static void MultipleJoinExample()

{

Student christian = new Student { FirstName = “Christian”, LastName = “Laine” };

Student brian = new Student { FirstName = “Brian”, LastName = “Dennis” };

Student peter = new Student { FirstName = “Peter”, LastName = “Horner” };

Student mark = new Student { FirstName = “Mark”, LastName = “Wright” };

Student andrew = new Student { FirstName = “Andrew”, LastName = “Selly” };

 

 

Album achtung = new Album { Title = “Achtung Baby “, Owner = terry };

Album life = new Album { Title = “My Life”, Owner = terry };

Album thriller = new Album { Title = “Thriller”, Owner = charlotte };

Album timeout = new Album { Title = “Time Out of Mind “, Owner = rui };

Album stankonia = new Album { Title = “Stankonia “, Owner = magnus };

 

Movie bonie = new Movie { Title = “Bonnie and Clyde”, Owner = phyllis };

Movie nemo = new Movie { Title = “Finding Nemo “, Owner = magnus };

Movie godfather = new Movie { Title = “The Godfather”, Owner = terry };

Movie good = new Movie { Title = ” The Good, The Bad and The Ugly”, Owner = charlotte };

Movie lord = new Movie { Title = “The Lord of the Rings”, Owner = rui };

Movie star = new Movie { Title = “Star Wars”, Owner = arlene };

 

// Create three lists.

List<Student> students = new List<Student> { christian, brian, peter, mark, andrew  };

List<Album> albums = new List<Album> { achtung, life, thriller, timeout, stankonia };

List<Movie> movies  = new List<Movie> { bonie, nemo, godfather, good, lord, star };

 

// The first join matches Student and Album.Owner from the list of students and

// albums, based on a common Student. The second join matches movies whose titles start

// with the same letter as the albums that have the same owner.

var query = from student in students

join album in albums on student equals student.Owner

join movie in movies on

new { Owner = student, Letter = album.Title.Substring(0, 1) }

equals new { movie.Owner, Letter = movie.Name.Substring(0, 1) }

select new { AlbumTitle = album.Title, MovieTitle = movie.Title };

 

foreach (var obj in query)

{

Console.WriteLine(

“The album title \”{0}\” starts with the same letter as the movie title \”{1}\” and both have the same owner .”,

obj.AlbumTitle, obj.MovieTitle);

}

}

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.