using System;
using System.Collections.Generic;
using System.Linq; namespace Linq101
{
internal class Join
{
/// <summary>
/// This sample shows how to efficiently join elements of two sequences based on equality between key expressions over the two.
/// </summary>
public void Linq102()
{
string[] categories = { "Beverages", "Condiments", "Vegetables", "Dairy Products", "Seafood" };
List<Data.Product> products = Data.GetProductList(); var q = from c in categories
join p in products on c equals p.Category
select new { Category = c, p.ProductName }; ObjectDumper.Write(q);
} /// <summary>
/// Using a group join you can get all the products that match a given category bundled as a sequence.
/// </summary>
public void Linq103()
{
string[] categories = { "Beverages", "Condiments", "Vegetables", "Dairy Products", "Seafood" };
List<Data.Product> products = Data.GetProductList(); var q = from c in categories
join p in products on c equals p.Category into ps
select new { Category = c, Products = ps }; foreach (var v in q)
{
Console.WriteLine(v.Category + ":");
foreach (var p in v.Products)
{
Console.WriteLine(" " + p.ProductName);
}
}
} /// <summary>
/// The group join operator is more general than join, as this slightly more verbose version of the cross join sample shows.
/// </summary>
public void Linq104()
{
string[] categories = { "Beverages", "Condiments", "Vegetables", "Dairy Products", "Seafood" };
List<Data.Product> products = Data.GetProductList(); var q = from c in categories
join p in products on c equals p.Category into ps
from p in ps
select new { Category = c, p.ProductName }; foreach (var v in q)
{
Console.WriteLine(v.ProductName + ": " + v.Category);
}
} /// <summary>
/// A so-called outer join can be expressed with a group join. A left outer joinis like a cross join,
/// except that all the left hand side elements get included at least once, even if they don't match any right hand side elements.
/// Note how Vegetablesshows up in the output even though it has no matching products.
/// </summary>
public void Linq105()
{
string[] categories = { "Beverages", "Condiments", "Vegetables", "Dairy Products", "Seafood" };
List<Data.Product> products = Data.GetProductList(); var q = from c in categories
join p in products on c equals p.Category into ps
from p in ps.DefaultIfEmpty()
select new { Category = c, ProductName = p == null ? "(No Products)" : p.ProductName }; foreach (var v in q)
{
Console.WriteLine(v.ProductName + ": " + v.Category);
}
}
}
}