101个linq例子
Take - Simple
public void Linq20() { int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 }; var first3Numbers = numbers.Take(3); Console.WriteLine("First 3 numbers:"); foreach (var n in first3Numbers) { Console.WriteLine(n); } }
First 3 numbers:
5
4
1
Take - Nested
public void Linq21() { List<Customer> customers = GetCustomerList(); var first3WAOrders = ( from c in customers from o in c.Orders where c.Region == "WA" select new { c.CustomerID, o.OrderID, o.OrderDate }) .Take(3); Console.WriteLine("First 3 orders in WA:"); foreach (var order in first3WAOrders) { ObjectDumper.Write(order); } }
First 3 orders in WA:
CustomerID=LAZYK OrderID=10482 OrderDate=3/21/1997
CustomerID=LAZYK OrderID=10545 OrderDate=5/22/1997
CustomerID=TRAIH OrderID=10574 OrderDate=6/19/1997
Skip - Simple
public void Linq22() { int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 }; var allButFirst4Numbers = numbers.Skip(4); Console.WriteLine("All but first 4 numbers:"); foreach (var n in allButFirst4Numbers) { Console.WriteLine(n); } }
All but first 4 numbers:
9
8
6
7
2
Skip - Nested
public void Linq23() { List<Customer> customers = GetCustomerList(); var waOrders = from c in customers from o in c.Orders where c.Region == "WA" select new { c.CustomerID, o.OrderID, o.OrderDate }; var allButFirst2Orders = waOrders.Skip(2); Console.WriteLine("All but first 2 orders in WA:"); foreach (var order in allButFirst2Orders) { ObjectDumper.Write(order); } }
All but first 2 orders in WA:
CustomerID=TRAIH OrderID=10574 OrderDate=6/19/1997
CustomerID=TRAIH OrderID=10577 OrderDate=6/23/1997
CustomerID=TRAIH OrderID=10822 OrderDate=1/8/1998
CustomerID=WHITC OrderID=10269 OrderDate=7/31/1996
CustomerID=WHITC OrderID=10344 OrderDate=11/1/1996
CustomerID=WHITC OrderID=10469 OrderDate=3/10/1997
CustomerID=WHITC OrderID=10483 OrderDate=3/24/1997
CustomerID=WHITC OrderID=10504 OrderDate=4/11/1997
CustomerID=WHITC OrderID=10596 OrderDate=7/11/1997
CustomerID=WHITC OrderID=10693 OrderDate=10/6/1997
CustomerID=WHITC OrderID=10696 OrderDate=10/8/1997
CustomerID=WHITC OrderID=10723 OrderDate=10/30/1997
CustomerID=WHITC OrderID=10740 OrderDate=11/13/1997
CustomerID=WHITC OrderID=10861 OrderDate=1/30/1998
CustomerID=WHITC OrderID=10904 OrderDate=2/24/1998
CustomerID=WHITC OrderID=11032 OrderDate=4/17/1998
CustomerID=WHITC OrderID=11066 OrderDate=5/1/1998
TakeWhile - Simple
public void Linq24() { int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 }; var firstNumbersLessThan6 = numbers.TakeWhile(n => n < 6); Console.WriteLine("First numbers less than 6:"); foreach (var n in firstNumbersLessThan6) { Console.WriteLine(n); } }
Result
First numbers less than 6:
5
4
1
3
TakeWhile - Indexed
public void Linq25() { int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 }; var firstSmallNumbers = numbers.TakeWhile((n, index) => n >= index); Console.WriteLine("First numbers not less than their position:"); foreach (var n in firstSmallNumbers) { Console.WriteLine(n); } }
First numbers not less than their position:
5
4
SkipWhile - Simple
public void Linq26() { int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 }; var allButFirst3Numbers = numbers.SkipWhile(n => n % 3 != 0); Console.WriteLine("All elements starting from first element divisible by 3:"); foreach (var n in allButFirst3Numbers) { Console.WriteLine(n); } }
All elements starting from first element divisible by 3:
3
9
8
6
7
2
0
SkipWhile - Indexed
public void Linq27() { int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 }; var laterNumbers = numbers.SkipWhile((n, index) => n >= index); Console.WriteLine("All elements starting from first element less than its position:"); foreach (var n in laterNumbers) { Console.WriteLine(n); } }
Result
All elements starting from first element less than its position:
1
3
9
8
6
7
2
0
OrderBy - Simple 1
public void Linq28() { string[] words = { "cherry", "apple", "blueberry" }; var sortedWords = from w in words orderby w select w; Console.WriteLine("The sorted list of words:"); foreach (var w in sortedWords) { Console.WriteLine(w); } }
Result
The sorted list of words:
apple
blueberry
cherry
OrderBy - Simple 2
public void Linq29() { string[] words = { "cherry", "apple", "blueberry" }; var sortedWords = from w in words orderby w.Length select w; Console.WriteLine("The sorted list of words (by length):"); foreach (var w in sortedWords) { Console.WriteLine(w); } }
Result
The sorted list of words (by length):
apple
cherry
blueberry
OrderBy - Simple 3
public void Linq30() { List<Product> products = GetProductList(); var sortedProducts = from p in products orderby p.ProductName select p; ObjectDumper.Write(sortedProducts); }
Result
ProductID=17 ProductName=Alice Mutton Category=Meat/Poultry UnitPrice=39.0000 UnitsInStock=0
ProductID=3 ProductName=Aniseed Syrup Category=Condiments UnitPrice=10.0000 UnitsInStock=13
ProductID=40 ProductName=Boston Crab Meat Category=Seafood UnitPrice=18.4000 UnitsInStock=123
ProductID=60 ProductName=Camembert Pierrot Category=Dairy Products UnitPrice=34.0000 UnitsInStock=19
ProductID=18 ProductName=Carnarvon Tigers Category=Seafood UnitPrice=62.5000 UnitsInStock=42
ProductID=1 ProductName=Chai Category=Beverages UnitPrice=18.0000 UnitsInStock=39
ProductID=2 ProductName=Chang Category=Beverages UnitPrice=19.0000 UnitsInStock=17
ProductID=39 ProductName=Chartreuse verte Category=Beverages UnitPrice=18.0000 UnitsInStock=69
ProductID=4 ProductName=Chef Anton's Cajun Seasoning Category=Condiments UnitPrice=22.0000 UnitsInStock=53
ProductID=5 ProductName=Chef Anton's Gumbo Mix Category=Condiments UnitPrice=21.3500 UnitsInStock=0
ProductID=48 ProductName=Chocolade Category=Confections UnitPrice=12.7500 UnitsInStock=15
ProductID=38 ProductName=Côte de Blaye Category=Beverages UnitPrice=263.5000 UnitsInStock=17
ProductID=58 ProductName=Escargots de Bourgogne Category=Seafood UnitPrice=13.2500 UnitsInStock=62
ProductID=52 ProductName=Filo Mix Category=Grains/Cereals UnitPrice=7.0000 UnitsInStock=38
ProductID=71 ProductName=Flotemysost Category=Dairy Products UnitPrice=21.5000 UnitsInStock=26
ProductID=33 ProductName=Geitost Category=Dairy Products UnitPrice=2.5000 UnitsInStock=112
ProductID=15 ProductName=Genen Shouyu Category=Condiments UnitPrice=15.5000 UnitsInStock=39
ProductID=56 ProductName=Gnocchi di nonna Alice Category=Grains/Cereals UnitPrice=38.0000 UnitsInStock=21
ProductID=31 ProductName=Gorgonzola Telino Category=Dairy Products UnitPrice=12.5000 UnitsInStock=0
ProductID=6 ProductName=Grandma's Boysenberry Spread Category=Condiments UnitPrice=25.0000 UnitsInStoc
OrderBy - Comparer
public void Linq31() { string[] words = { "aPPLE", "AbAcUs", "bRaNcH", "BlUeBeRrY", "ClOvEr", "cHeRry" }; var sortedWords = words.OrderBy(a => a, new CaseInsensitiveComparer()); ObjectDumper.Write(sortedWords); } public class CaseInsensitiveComparer : IComparer<string> { public int Compare(string x, string y) { return string.Compare(x, y, StringComparison.OrdinalIgnoreCase); } }
Result
AbAcUs
aPPLE
BlUeBeRrY
bRaNcH
cHeRry
ClOvEr
OrderByDescending - Simple 1
public void Linq32() { double[] doubles = { 1.7, 2.3, 1.9, 4.1, 2.9 }; var sortedDoubles = from d in doubles orderby d descending select d; Console.WriteLine("The doubles from highest to lowest:"); foreach (var d in sortedDoubles) { Console.WriteLine(d); } }
The doubles from highest to lowest:
4.1
2.9
2.3
1.9
1.7
OrderByDescending - Simple 2
public void Linq33() { List<Product> products = GetProductList(); var sortedProducts = from p in products orderby p.UnitsInStock descending select p; ObjectDumper.Write(sortedProducts); }
Result
ProductID=75 ProductName=Rhönbräu Klosterbier Category=Beverages UnitPrice=7.7500 UnitsInStock=125
ProductID=40 ProductName=Boston Crab Meat Category=Seafood UnitPrice=18.4000 UnitsInStock=123
ProductID=6 ProductName=Grandma's Boysenberry Spread Category=Condiments UnitPrice=25.0000 UnitsInStock=120
ProductID=55 ProductName=Pâté chinois Category=Meat/Poultry UnitPrice=24.0000 UnitsInStock=115
ProductID=61 ProductName=Sirop d'érable Category=Condiments UnitPrice=28.5000 UnitsInStock=113
ProductID=33 ProductName=Geitost Category=Dairy Products UnitPrice=2.5000 UnitsInStock=112
ProductID=36 ProductName=Inlagd Sill Category=Seafood UnitPrice=19.0000 UnitsInStock=112
ProductID=34 ProductName=Sasquatch Ale Category=Beverages UnitPrice=14.0000 UnitsInStock=111
ProductID=22 ProductName=Gustaf's Knäckebröd Category=Grains/Cereals UnitPrice=21.0000 UnitsInStock=104
ProductID=73 ProductName=Röd Kaviar Category=Seafood UnitPrice=15.0000 UnitsInStock=101
ProductID=46 ProductName=Spegesild Category=Seafood UnitPrice=12.0000 UnitsInStock=95
ProductID=12 ProductName=Queso Manchego La Pastora Category=Dairy Products UnitPrice=38.0000 UnitsInStock=86
ProductID=41 ProductName=Jack's New England Clam Chowder Category=Seafood UnitPrice=9.6500 UnitsInStock=85
ProductID=59 ProductName=Raclette Courdavault Category=Dairy Products UnitPrice=55.0000 UnitsInStock=79
ProductID=25 ProductName=NuNuCa Nuß-Nougat-Creme Category=Confections UnitPrice=14.0000 UnitsInStock=76
ProductID=65 ProductName=Louisiana Fiery Hot Pepper Sauce Category=Condiments UnitPrice=21.0500 UnitsInStock=76
ProductID=39 ProductName=Chartreuse verte Category=Beverages UnitPrice=18.0000 UnitsInStock=69
ProductID=50 ProductName=Valkoinen suklaa Category=Confections UnitPrice=16.2500 UnitsInStock=65
OrderByDescending - Comparer
public void Linq34() { string[] words = { "aPPLE", "AbAcUs", "bRaNcH", "BlUeBeRrY", "ClOvEr", "cHeRry" }; var sortedWords = words.OrderByDescending(a => a, new CaseInsensitiveComparer()); ObjectDumper.Write(sortedWords); } public class CaseInsensitiveComparer : IComparer<string> { public int Compare(string x, string y) { return string.Compare(x, y, StringComparison.OrdinalIgnoreCase); } }
Result
ClOvEr
cHeRry
bRaNcH
BlUeBeRrY
aPPLE
AbAcUs
ThenBy - Simple
public void Linq35() { string[] digits = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" }; var sortedDigits = from d in digits orderby d.Length, d select d; Console.WriteLine("Sorted digits:"); foreach (var d in sortedDigits) { Console.WriteLine(d); } }
Result
Sorted digits:
one
six
two
five
four
nine
zero
eight
seven
three
ThenBy - Comparer
public void Linq36() { string[] words = { "aPPLE", "AbAcUs", "bRaNcH", "BlUeBeRrY", "ClOvEr", "cHeRry" }; var sortedWords = words.OrderBy(a => a.Length) .ThenBy(a => a, new CaseInsensitiveComparer()); ObjectDumper.Write(sortedWords); } public class CaseInsensitiveComparer : IComparer<string> { public int Compare(string x, string y) { return string.Compare(x, y, StringComparison.OrdinalIgnoreCase); } }
Result
aPPLE
AbAcUs
bRaNcH
cHeRry
ClOvEr
BlUeBeRrY
ThenByDescending - Simple
public void Linq37() { List<Product> products = GetProductList(); var sortedProducts = from p in products orderby p.Category, p.UnitPrice descending select p; ObjectDumper.Write(sortedProducts); }
Result
ProductID=38 ProductName=Côte de Blaye Category=Beverages UnitPrice=263.5000 UnitsInStock=17
ProductID=43 ProductName=Ipoh Coffee Category=Beverages UnitPrice=46.0000 UnitsInStock=17
ProductID=2 ProductName=Chang Category=Beverages UnitPrice=19.0000 UnitsInStock=17
ProductID=1 ProductName=Chai Category=Beverages UnitPrice=18.0000 UnitsInStock=39
ProductID=35 ProductName=Steeleye Stout Category=Beverages UnitPrice=18.0000 UnitsInStock=20
ProductID=39 ProductName=Chartreuse verte Category=Beverages UnitPrice=18.0000 UnitsInStock=69
ProductID=76 ProductName=Lakkalikööri Category=Beverages UnitPrice=18.0000 UnitsInStock=57
ProductID=70 ProductName=Outback Lager Category=Beverages UnitPrice=15.0000 UnitsInStock=15
ProductID=34 ProductName=Sasquatch Ale Category=Beverages UnitPrice=14.0000 UnitsInStock=111
ProductID=67 ProductName=Laughing Lumberjack Lager Category=Beverages UnitPrice=14.0000 UnitsInStock=52
ProductID=75 ProductName=Rhönbräu Klosterbier Category=Beverages UnitPrice=7.7500 UnitsInStock=125
ProductID=24 ProductName=Guaraná Fantástica Category=Beverages UnitPrice=4.5000 UnitsInStock=20
ProductID=63 ProductName=Vegie-spread Category=Condiments UnitPrice=43.9000 UnitsInStock=24
ProductID=8 ProductName=Northwoods Cranberry Sauce Category=Condiments UnitPrice=40.0000 UnitsInStock=6
ProductID=61 ProductName=Sirop d'érable Category=Condiments UnitPrice=28.5000 UnitsInStock=113
ProductID=6 ProductName=Grandma's Boysenberry Spread Category=Condiments UnitPrice=25.0000 UnitsInStock=120
ProductID=4 ProductName=Chef Anton's Cajun Seasoning Category=Condiments UnitPrice=22.0000 UnitsInStock=53
ProductID=5 ProductName=Chef Anton's Gumbo Mix Category=Condiments UnitPrice=21.3500 UnitsInStock=0
ProductID=65 ProductName=Louisiana Fiery Hot Pepper Sauce Category=Condiments UnitPrice=21.0500 UnitsInStock=76
ProductID=44 ProductName=Gula Malacca Category=Condiments UnitPrice=19.4500 UnitsInStock=27
ProductID=66 ProductName=Louisiana Hot Spiced Okra Category=Condiments UnitPrice=17.0000 UnitsInStock=4
ProductID=15 ProductName=Genen Shouyu Category=Condiments UnitPrice=15.5000 UnitsInStock=39
ProductID=77 ProductName=Original Frankfurter grüne Soße Category=Condiments UnitPrice=13.0000 UnitsInStock=32
ThenByDescending - Comparer
public void Linq38() { string[] words = { "aPPLE", "AbAcUs", "bRaNcH", "BlUeBeRrY", "ClOvEr", "cHeRry" }; var sortedWords = words.OrderBy(a => a.Length) .ThenByDescending(a => a, new CaseInsensitiveComparer()); ObjectDumper.Write(sortedWords); } public class CaseInsensitiveComparer : IComparer<string> { public int Compare(string x, string y) { return string.Compare(x, y, StringComparison.OrdinalIgnoreCase); } }
aPPLE
ClOvEr
cHeRry
bRaNcH
AbAcUs
BlUeBeRrY
Reverse
public void Linq39() { string[] digits = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" }; var reversedIDigits = ( from d in digits where d[1] == 'i' select d) .Reverse(); Console.WriteLine("A backwards list of the digits with a second character of 'i':"); foreach (var d in reversedIDigits) { Console.WriteLine(d); } }
Result
A backwards list of the digits with a second character of 'i':
nine
eight
six
five
GroupBy - Simple 1
public void Linq40() { int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 }; var numberGroups = from n in numbers group n by n % 5 into g select new { Remainder = g.Key, Numbers = g }; foreach (var g in numberGroups) { Console.WriteLine("Numbers with a remainder of {0} when divided by 5:", g.Remainder); foreach (var n in g.Numbers) { Console.WriteLine(n); } } }
Numbers with a remainder of 0 when divided by 5:
5
0
Numbers with a remainder of 4 when divided by 5:
4
9
Numbers with a remainder of 1 when divided by 5:
1
6
Numbers with a remainder of 3 when divided by 5:
3
8
Numbers with a remainder of 2 when divided by 5:
7
2
后续的正在整理,请耐心等待一下