101 LINQ Samples: Miscellaneous Operators

LINQ Concat与SequenceEqual示例
本文提供了使用LINQ的Concat方法将两个数组或集合连接成一个序列的示例,以及使用SequenceEqual方法检查两个序列是否完全相同的示例。通过具体的代码演示了如何操作整数数组和字符串数组。

Concat - 1

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

public void Linq94(){    int[] numbersA = { 0245689 };    int[] numbersB = { 13578 };     var allNumbers = numbersA.Concat(numbersB);     Console.WriteLine("All numbers from both arrays:");    foreach (var n in allNumbers)    {        Console.WriteLine(n);    }}

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.

public void Linq95(){    List<Customer> customers = GetCustomerList();    List<Product> products = GetProductList();     var customerNames =        from c in customers        select c.CompanyName;    var productNames =        from p in products        select p.ProductName;     var allNames = customerNames.Concat(productNames);     Console.WriteLine("Customer and product names:");    foreach (var n in allNames)    {        Console.WriteLine(n);    }}

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.

public void Linq96(){    var wordsA = new string[] { "cherry""apple""blueberry" };    var wordsB = new string[] { "cherry""apple""blueberry" };     bool match = wordsA.SequenceEqual(wordsB);     Console.WriteLine("The sequences match: {0}", match);}

Result

The sequences match: True

EqualAll - 2

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

public void Linq97(){    var wordsA = new string[] { "cherry""apple""blueberry" };    var wordsB = new string[] { "apple""blueberry""cherry" };     bool match = wordsA.SequenceEqual(wordsB);     Console.WriteLine("The sequences match: {0}", match);}

Result

The sequences match: False

转载于:https://www.cnblogs.com/zjz008/archive/2011/03/13/1982931.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值