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

Web developers use 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 developers are performing.

When Web developers use inner join they have to expects a result set based on two collection in which each element of the first collection appears one time for every matching element in the second collection. If an element in the first collection has no matching elements, it does not appear in the result set.

The following example creates two collections those contain objects of two user-defined types, Person and Book. The query uses the join clause to match Person objects with Book objects whose Author is that Person.

 

class Person

{

public string FirstName { get; set; }

public string LastName { get; set; }

}

 

class Book

{

public string Title { get; set; }

public Person Author { get; set; }

}

 

// <summary>

// Simple inner join.

// </summary>

 

public static void InnerJoinExample()

{

Person edward = new Person { FirstName = “Edward”, LastName = “De Bono” };

Person robert = new Person { FirstName = “Robert”, LastName = “Jordan” };

Person joanne = new Person { FirstName = “Joanne”, LastName = ” Rowling” };

Person terry = new Person { FirstName = “Terry”, LastName = “Pratchett” };

 

 

Book sixhats = new Book { Title = “6 Thinking Hats”, Author = edward };

Book greathunt = new Book { Title = “The great hunt”, Author = robert };

Book hpotter = new Book { Title = ” Harry Potter and the deathly hallows”, Author = joanne };

Book cpeople= new Book { Title = “The Carpet People”, Author = terry};

 

 

// Create two lists.

List<Person> people = new List<Person> {edward,robert,joanne,terry };

List<Book> books = new List<Book> { sixhats, greathunt, hpotter, cpeople };

 

// Create a collection of person-book pairs. Each element in the collection

// is an anonymous type containing both the person’s name and their book’s title

var query = from person in people

join book in books on person equals book.Author

select new { AuthorName  = person.FirstName, BookTitle = book.Title };

 

foreach (var authorAndBook in query)

{

Console.WriteLine(“\”{0}\” is written by {1}”, authorAndBook.BookTitle, authorAndBook.AuthorName);

}

}

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.