This example program uses two classes for storing data. Join query syntax is basically the same as in SQL. Occasionally it is easier to develop logic in this style.
using System;
using System.Linq;
class Customer
{
public int ID { get; set; }
public string Name { get; set; }
}
class Order
{
public int ID { get; set; }
public string Product { get; set; }
}
class Program
{
static void Main()
{
// Step 1: example customers and orders.
var customers = new Customer[]
{
new Customer{ID = 5, Name =
"Sam"},
new Customer{ID = 6, Name =
"Dave"},
new Customer{ID = 7, Name =
"Julia"},
new Customer{ID = 8, Name =
"Sue"}
};
var orders = new Order[]
{
new Order{ID = 5, Product =
"Book"},
new Order{ID = 6, Product =
"Game"},
new Order{ID = 7, Product =
"Computer"},
new Order{ID = 8, Product =
"Shirt"}
};
// Step 2: join on the ID properties.
var query = from c in customers
join o in orders on c.ID equals o.ID
select new { c.Name, o.Product };
// Step 3: display joined groups.
foreach (var group in query)
{
Console.WriteLine(
"{0} bought {1}", group.Name, group.Product);
}
}
}
Sam bought Book
Dave bought Game
Julia bought Computer
Sue bought Shirt