101 LINQ Samples: Join Operators

Cross Join

This sample shows how to efficiently join elements of two sequences based on equality between key expressions over the two.

  1. public void Linq102()
  2.  
  3. {
  4.  
  5.     string[] categories = new string[]{ 
  6.  
  7.         "Beverages"
  8.  
  9.         "Condiments"
  10.  
  11.         "Vegetables"
  12.  
  13.         "Dairy Products"
  14.  
  15.         "Seafood" };
  16.  
  17.  
  18.  
  19.     List<Product> products = GetProductList();
  20.  
  21.  
  22.  
  23.     var q =
  24.  
  25.         from c in categories
  26.  
  27.         join p in products on c equals p.Category
  28.  
  29.         select new { Category = c, p.ProductName };
  30.  
  31.  
  32.  
  33.     foreach (var v in q)
  34.  
  35.     {
  36.  
  37.         Console.WriteLine(v.ProductName + ": " + v.Category);
  38.  
  39.     }
  40.  
  41. }

Result

Chai: Beverages
Chang: Beverages
Guaraná Fantástica: Beverages
Sasquatch Ale: Beverages
Steeleye Stout: Beverages
Côte de Blaye: Beverages
Chartreuse verte: Beverages
Ipoh Coffee: Beverages
Laughing Lumberjack Lager: Beverages
Outback Lager: Beverages
Rhönbräu Klosterbier: Beverages
Lakkalikööri: Beverages
Aniseed Syrup: Condiments
Chef Anton's Cajun Seasoning: Condiments
Chef Anton's Gumbo Mix: Condiments
Grandma's Boysenberry Spread: Condiments
Northwoods Cranberry Sauce: Condiments
Genen Shouyu: Condiments
Gula Malacca: Condiments
Sirop d'érable: Condiments
Vegie-spread: Condiments
Louisiana Fiery Hot Pepper Sauce: Condiments
Louisiana Hot Spiced Okra: Condiments
Original Frankfurter grüne Soße: Condiments
Queso Cabrales: Dairy Products
Queso Manchego La Pastora: Dairy Products
Gorgonzola Telino: Dairy Products
Mascarpone Fabioli: Dairy Products
Geitost: Dairy Products
Raclette Courdavault: Dairy Products
Camembert Pierrot: Dairy Products
Gudbrandsdalsost: Dairy Products
Flotemysost: Dairy Products
Mozzarella di Giovanni: Dairy Products
Ikura: Seafood
Konbu: Seafood
Carnarvon Tigers: Seafood
Nord-Ost Matjeshering: Seafood
Inlagd Sill: Seafood
Gravad lax: Seafood
Boston Crab Meat: Seafood
Jack's New England Clam Chowder: Seafood
Rogede sild: Seafood
Spegesild: Seafood
Escargots de Bourgogne: Seafood
Röd Kaviar: Seafood

Group Join

Using a group join you can get all the products that match a given category bundled as a sequence.

  1. public void Linq103()
  2.  
  3. {
  4.  
  5.     string[] categories = new string[]{ 
  6.  
  7.         "Beverages"
  8.  
  9.         "Condiments"
  10.  
  11.         "Vegetables"
  12.  
  13.         "Dairy Products"
  14.  
  15.         "Seafood" };
  16.  
  17.  
  18.  
  19.     List<Product> products = GetProductList();
  20.  
  21.  
  22.  
  23.     var q =
  24.  
  25.         from c in categories
  26.  
  27.         join p in products on c equals p.Category into ps
  28.  
  29.         select new { Category = c, Products = ps };
  30.  
  31.  
  32.  
  33.     foreach (var v in q)
  34.  
  35.     {
  36.  
  37.         Console.WriteLine(v.Category + ":");
  38.  
  39.         foreach (var p in v.Products)
  40.  
  41.         {
  42.  
  43.             Console.WriteLine("   " + p.ProductName);
  44.  
  45.         }
  46.  
  47.     }
  48.  
  49. }

Result

Beverages:
Chai
Chang
Guaraná Fantástica
Sasquatch Ale
Steeleye Stout
Côte de Blaye
Chartreuse verte
Ipoh Coffee
Laughing Lumberjack Lager
Outback Lager
Rhönbräu Klosterbier
Lakkalikööri
Condiments:
Aniseed Syrup
Chef Anton's Cajun Seasoning
Chef Anton's Gumbo Mix
Grandma's Boysenberry Spread
Northwoods Cranberry Sauce
Genen Shouyu
Gula Malacca
Sirop d'érable
Vegie-spread
Louisiana Fiery Hot Pepper Sauce
Louisiana Hot Spiced Okra
Original Frankfurter grüne Soße
Vegetables:
Dairy Products:
Queso Cabrales
Queso Manchego La Pastora
Gorgonzola Telino
Mascarpone Fabioli
Geitost
Raclette Courdavault
Camembert Pierrot
Gudbrandsdalsost
Flotemysost
Mozzarella di Giovanni
Seafood:
Ikura
Konbu
Carnarvon Tigers
Nord-Ost Matjeshering
Inlagd Sill
Gravad lax
Boston Crab Meat
Jack's New England Clam Chowder
Rogede sild
Spegesild
Escargots de Bourgogne
Röd Kaviar

Cross Join with Group Join

The group join operator is more general than join, as this slightly more verbose version of the cross join sample shows.

  1. public void Linq104()
  2.  
  3. {
  4.  
  5.     string[] categories = new string[]{ 
  6.  
  7.         "Beverages"
  8.  
  9.         "Condiments"
  10.  
  11.         "Vegetables"
  12.  
  13.         "Dairy Products"
  14.  
  15.         "Seafood" };
  16.  
  17.  
  18.  
  19.     List<Product> products = GetProductList();
  20.  
  21.  
  22.  
  23.     var q =
  24.  
  25.         from c in categories
  26.  
  27.         join p in products on c equals p.Category into ps
  28.  
  29.         from p in ps
  30.  
  31.         select new { Category = c, p.ProductName };
  32.  
  33.  
  34.  
  35.     foreach (var v in q)
  36.  
  37.     {
  38.  
  39.         Console.WriteLine(v.ProductName + ": " + v.Category);
  40.  
  41.     }
  42.  
  43. }

Result

Chai: Beverages
Chang: Beverages
Guaraná Fantástica: Beverages
Sasquatch Ale: Beverages
Steeleye Stout: Beverages
Côte de Blaye: Beverages
Chartreuse verte: Beverages
Ipoh Coffee: Beverages
Laughing Lumberjack Lager: Beverages
Outback Lager: Beverages
Rhönbräu Klosterbier: Beverages
Lakkalikööri: Beverages
Aniseed Syrup: Condiments
Chef Anton's Cajun Seasoning: Condiments
Chef Anton's Gumbo Mix: Condiments
Grandma's Boysenberry Spread: Condiments
Northwoods Cranberry Sauce: Condiments
Genen Shouyu: Condiments
Gula Malacca: Condiments
Sirop d'érable: Condiments
Vegie-spread: Condiments
Louisiana Fiery Hot Pepper Sauce: Condiments
Louisiana Hot Spiced Okra: Condiments
Original Frankfurter grüne Soße: Condiments
Queso Cabrales: Dairy Products
Queso Manchego La Pastora: Dairy Products
Gorgonzola Telino: Dairy Products
Mascarpone Fabioli: Dairy Products
Geitost: Dairy Products
Raclette Courdavault: Dairy Products
Camembert Pierrot: Dairy Products
Gudbrandsdalsost: Dairy Products
Flotemysost: Dairy Products
Mozzarella di Giovanni: Dairy Products
Ikura: Seafood
Konbu: Seafood
Carnarvon Tigers: Seafood
Nord-Ost Matjeshering: Seafood
Inlagd Sill: Seafood
Gravad lax: Seafood
Boston Crab Meat: Seafood
Jack's New England Clam Chowder: Seafood
Rogede sild: Seafood
Spegesild: Seafood
Escargots de Bourgogne: Seafood
Röd Kaviar: Seafood

Left Outer Join

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.

  1. public void Linq105()
  2.  
  3. {
  4.  
  5.     string[] categories = new string[]{ 
  6.  
  7.         "Beverages"
  8.  
  9.         "Condiments"
  10.  
  11.         "Vegetables"
  12.  
  13.         "Dairy Products"
  14.  
  15.         "Seafood" };
  16.  
  17.  
  18.  
  19.     List<Product> products = GetProductList();
  20.  
  21.  
  22.  
  23.     var q =
  24.  
  25.         from c in categories
  26.  
  27.         join p in products on c equals p.Category into ps
  28.  
  29.         from p in ps.DefaultIfEmpty()
  30.  
  31.         select new { Category = c, ProductName = p == null ? "(No products)" : p.ProductName };
  32.  
  33.  
  34.  
  35.     foreach (var v in q)
  36.  
  37.     {
  38.  
  39.         Console.WriteLine(v.ProductName + ": " + v.Category);
  40.  
  41.     }
  42.  
  43. }

Result

Chai: Beverages
Chang: Beverages
Guaraná Fantástica: Beverages
Sasquatch Ale: Beverages
Steeleye Stout: Beverages
Côte de Blaye: Beverages
Chartreuse verte: Beverages
Ipoh Coffee: Beverages
Laughing Lumberjack Lager: Beverages
Outback Lager: Beverages
Rhönbräu Klosterbier: Beverages
Lakkalikööri: Beverages
Aniseed Syrup: Condiments
Chef Anton's Cajun Seasoning: Condiments
Chef Anton's Gumbo Mix: Condiments
Grandma's Boysenberry Spread: Condiments
Northwoods Cranberry Sauce: Condiments
Genen Shouyu: Condiments
Gula Malacca: Condiments
Sirop d'érable: Condiments
Vegie-spread: Condiments
Louisiana Fiery Hot Pepper Sauce: Condiments
Louisiana Hot Spiced Okra: Condiments
Original Frankfurter grüne Soße: Condiments
(No products): Vegetables
Queso Cabrales: Dairy Products
Queso Manchego La Pastora: Dairy Products
Gorgonzola Telino: Dairy Products
Mascarpone Fabioli: Dairy Products
Geitost: Dairy Products
Raclette Courdavault: Dairy Products
Camembert Pierrot: Dairy Products
Gudbrandsdalsost: Dairy Products
Flotemysost: Dairy Products
Mozzarella di Giovanni: Dairy Products
Ikura: Seafood
Konbu: Seafood
Carnarvon Tigers: Seafood
Nord-Ost Matjeshering: Seafood
Inlagd Sill: Seafood
Gravad lax: Seafood
Boston Crab Meat: Seafood
Jack's New England Clam Chowder: Seafood
Rogede sild: Seafood
Spegesild: Seafood
Escargots de Bourgogne: Seafood
Röd Kaviar: Seafood

posted @ 2011-03-13 21:07  i'm zjz  阅读(339)  评论(0编辑  收藏  举报