101 LINQ Samples: Miscellaneous Operators

Concat - 1

This sample uses Concat to create one sequence that contains each array's values, one after the other.

  1. public void Linq94()
  2. {
  3.     int[] numbersA = { 0245689 };
  4.     int[] numbersB = { 13578 };
  5.  
  6.     var allNumbers = numbersA.Concat(numbersB);
  7.  
  8.     Console.WriteLine("All numbers from both arrays:");
  9.     foreach (var n in allNumbers)
  10.     {
  11.         Console.WriteLine(n);
  12.     }
  13. }

Result

All numbers from both arrays: 











8

Concat - 2

This sample uses Concat to create one sequence that contains the names of all customers and products, including any duplicates.

  1. public void Linq95()
  2. {
  3.     List<Customer> customers = GetCustomerList();
  4.     List<Product> products = GetProductList();
  5.  
  6.     var customerNames =
  7.         from c in customers
  8.         select c.CompanyName;
  9.     var productNames =
  10.         from p in products
  11.         select p.ProductName;
  12.  
  13.     var allNames = customerNames.Concat(productNames);
  14.  
  15.     Console.WriteLine("Customer and product names:");
  16.     foreach (var n in allNames)
  17.     {
  18.         Console.WriteLine(n);
  19.     }
  20. }

Result

Customer and product names:
Alfreds Futterkiste
Ana Trujillo Emparedados y helados
Antonio Moreno Taquería
Around the Horn
Berglunds snabbköp
Blauer See Delikatessen
Blondel père et fils
Bólido Comidas preparadas
Bon app'
Bottom-Dollar Markets
B's Beverages
Cactus Comidas para llevar
Centro comercial Moctezuma
Chop-suey Chinese
Comércio Mineiro
Consolidated Holdings
Drachenblut Delikatessen
Du monde entier
Eastern Connection
Ernst Handel
Familia Arquibaldo
FISSA Fabrica Inter. Salchichas S.A.
Folies gourmandes
Folk och fä HB
Frankenversand
France restauration
Franchi S.p.A.
Furia Bacalhau e Frutos do Mar
Galería del gastrónomo
Godos Cocina Típica
Gourmet Lanchonetes
Great Lakes Food Market
GROSELLA-Restaurante
Hanari Carnes
HILARIÓN-Abastos
Hungry Coyote Import Store
Hungry Owl All-Night Grocers
Island Trading
Königlich Essen
La corne d'abondance
La maison d'Asie
Laughing Bacchus Wine Cellars
Lazy K Kountry Store
Lehmanns Marktstand
Let's Stop N Shop
LILA-Supermercado
LINO-Delicateses
Lonesome Pine Restaurant
Magazzini Alimentari Riuniti
Maison Dewey Mère Paillarde
Morgenstern Gesundkost
North/South
Océano Atlántico Ltda.
Old World Delicatessen
Ottilies Käseladen
Paris spécialités
Pericles Comidas clásicas
Piccolo und mehr
Princesa Isabel Vinhos
Que Delícia
Queen Cozinha
QUICK-Stop
Rancho grande
Rattlesnake Canyon Grocery
Reggiani Caseifici
Ricardo Adocicados
Richter Supermarkt
Romero y tomillo Santé Gourmet
Save-a-lot Markets
Seven Seas Imports
Simons bistro
Spécialités du monde
Split Rail Beer & Ale
Suprêmes délices
The Big Cheese
The Cracker Box
Toms Spezialitäten
Tortuga Restaurante
Tradição Hipermercados
Trail's Head Gourmet Provisioners
Vaffeljernet
Victuailles en stock
Vins et alcools Chevalier
Die Wandernde Kuh
Wartian Herkku
Wellington Importadora
White Clover Markets
Wilman Kala
Wolski Zajazd
Chai
Chang
Aniseed Syrup
Chef Anton's Cajun Seasoning
Chef Anton's Gumbo Mix
Grandma's Boysenberry Spread
Uncle Bob's Organic Dried Pears
Northwoods Cranberry Sauce
Mishi Kobe Niku
Ikura
Queso Cabrales
Queso Manchego La Pastora
Konbu
Tofu
Genen Shouyu
Pavlova
Alice Mutton
Carnarvon Tigers
Teatime Chocolate Biscuits
Sir Rodney's Marmalade 
Sir Rodney's Scones 
Gustaf's Knäckebröd 
Tunnbröd 
Guaraná Fantástica 
NuNuCa Nuß-Nougat-Creme 
Gumbär Gummibärchen 
Schoggi Schokolade 
Rössle Sauerkraut 
Thüringer Rostbratwurst 
Nord-Ost Matjeshering 
Gorgonzola Telino 
Mascarpone Fabioli 
Geitost 
Sasquatch Ale 
Steeleye Stout 
Inlagd Sill 
Gravad lax 
Côte de Blaye 
Chartreuse verte 
Boston Crab Meat 
Jack's New England Clam Chowder 
Singaporean Hokkien Fried Mee 
Ipoh Coffee 
Gula Malacca 
Rogede sild 
Spegesild 
Zaanse koeken 
Chocolade 
Maxilaku 
Valkoinen suklaa 
Manjimup Dried Apples 
Filo Mix 
Perth Pasties 
Tourtière 
Pâté chinois 
Gnocchi di nonna Alice 
Ravioli Angelo 
Escargots de Bourgogne 
Raclette Courdavault 
Camembert Pierrot 
Sirop d'érable 
Tarte au sucre 
Vegie-spread 
Wimmers gute Semmelknödel 
Louisiana Fiery Hot Pepper Sauce 
Louisiana Hot Spiced Okra 
Laughing Lumberjack Lager 
Scottish Longbreads 
Gudbrandsdalsost 
Outback Lager 
Flotemysost 
Mozzarella di Giovanni 
Röd Kaviar 
Longlife Tofu 
Rhönbräu Klosterbier 
Lakkalikööri 
Original Frankfurter grüne Soße

EqualAll - 1

This sample uses EqualAll to see if two sequences match on all elements in the same order.

  1. public void Linq96()
  2. {
  3.     var wordsA = new string[] { "cherry""apple""blueberry" };
  4.     var wordsB = new string[] { "cherry""apple""blueberry" };
  5.  
  6.     bool match = wordsA.SequenceEqual(wordsB);
  7.  
  8.     Console.WriteLine("The sequences match: {0}", match);
  9. }

Result

The sequences match: True

EqualAll - 2

This sample uses EqualAll to see if two sequences match on all elements in the same order.

  1. public void Linq97()
  2. {
  3.     var wordsA = new string[] { "cherry""apple""blueberry" };
  4.     var wordsB = new string[] { "apple""blueberry""cherry" };
  5.  
  6.     bool match = wordsA.SequenceEqual(wordsB);
  7.  
  8.     Console.WriteLine("The sequences match: {0}", match);
  9. }

Result

The sequences match: False

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