101 LINQ Samples: Ordering Operators

OrderBy - Simple 1

This sample uses orderby to sort a list of words alphabetically.


     
       
  1. public void Linq28()
  2. {
  3.     string[] words = { "cherry""apple""blueberry" };
  4.  
  5.     var sortedWords =
  6.         from w in words
  7.         orderby w
  8.         select w;
  9.  
  10.     Console.WriteLine("The sorted list of words:");
  11.     foreach (var w in sortedWords)
  12.     {
  13.         Console.WriteLine(w);
  14.     }
  15. }

Result

The sorted list of words:
apple
blueberry
cherry

OrderBy - Simple 2

This sample uses orderby to sort a list of words by length.


     
       
  1. public void Linq29()
  2. {
  3.     string[] words = { "cherry""apple""blueberry" };
  4.  
  5.     var sortedWords =
  6.         from w in words
  7.         orderby w.Length
  8.         select w;
  9.  
  10.     Console.WriteLine("The sorted list of words (by length):");
  11.     foreach (var w in sortedWords)
  12.     {
  13.         Console.WriteLine(w);
  14.     }
  15. }

Result

The sorted list of words (by length):
apple
cherry
blueberry

OrderBy - Simple 3

This sample uses orderby to sort a list of products by name.


     
       
  1. public void Linq30()
  2. {
  3.     List<Product> products = GetProductList();
  4.  
  5.     var sortedProducts =
  6.         from p in products
  7.         orderby p.ProductName
  8.         select p;
  9.  
  10.     ObjectDumper.Write(sortedProducts);
  11. }

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 UnitsInStock=120
ProductID=37 ProductName=Gravad lax Category=Seafood UnitPrice=26.0000 UnitsInStock=11
ProductID=24 ProductName=Guaraná Fantástica Category=Beverages UnitPrice=4.5000 UnitsInStock=20
ProductID=69 ProductName=Gudbrandsdalsost Category=Dairy Products UnitPrice=36.0000 UnitsInStock=26
ProductID=44 ProductName=Gula Malacca Category=Condiments UnitPrice=19.4500 UnitsInStock=27
ProductID=26 ProductName=Gumbär Gummibärchen Category=Confections UnitPrice=31.2300 UnitsInStock=15
ProductID=22 ProductName=Gustaf's Knäckebröd Category=Grains/Cereals UnitPrice=21.0000 UnitsInStock=104
ProductID=10 ProductName=Ikura Category=Seafood UnitPrice=31.0000 UnitsInStock=31
ProductID=36 ProductName=Inlagd Sill Category=Seafood UnitPrice=19.0000 UnitsInStock=112
ProductID=43 ProductName=Ipoh Coffee Category=Beverages UnitPrice=46.0000 UnitsInStock=17
ProductID=41 ProductName=Jack's New England Clam Chowder Category=Seafood UnitPrice=9.6500 UnitsInStock=85
ProductID=13 ProductName=Konbu Category=Seafood UnitPrice=6.0000 UnitsInStock=24
ProductID=76 ProductName=Lakkalikööri Category=Beverages UnitPrice=18.0000 UnitsInStock=57
ProductID=67 ProductName=Laughing Lumberjack Lager Category=Beverages UnitPrice=14.0000 UnitsInStock=52
ProductID=74 ProductName=Longlife Tofu Category=Produce UnitPrice=10.0000 UnitsInStock=4
ProductID=65 ProductName=Louisiana Fiery Hot Pepper Sauce Category=Condiments UnitPrice=21.0500 UnitsInStock=76
ProductID=66 ProductName=Louisiana Hot Spiced Okra Category=Condiments UnitPrice=17.0000 UnitsInStock=4
ProductID=51 ProductName=Manjimup Dried Apples Category=Produce UnitPrice=53.0000 UnitsInStock=20
ProductID=32 ProductName=Mascarpone Fabioli Category=Dairy Products UnitPrice=32.0000 UnitsInStock=9
ProductID=49 ProductName=Maxilaku Category=Confections UnitPrice=20.0000 UnitsInStock=10
ProductID=9 ProductName=Mishi Kobe Niku Category=Meat/Poultry UnitPrice=97.0000 UnitsInStock=29
ProductID=72 ProductName=Mozzarella di Giovanni Category=Dairy Products UnitPrice=34.8000 UnitsInStock=14
ProductID=30 ProductName=Nord-Ost Matjeshering Category=Seafood UnitPrice=25.8900 UnitsInStock=10
ProductID=8 ProductName=Northwoods Cranberry Sauce Category=Condiments UnitPrice=40.0000 UnitsInStock=6
ProductID=25 ProductName=NuNuCa Nuß-Nougat-Creme Category=Confections UnitPrice=14.0000 UnitsInStock=76
ProductID=77 ProductName=Original Frankfurter grüne Soße Category=Condiments UnitPrice=13.0000 UnitsInStock=32
ProductID=70 ProductName=Outback Lager Category=Beverages UnitPrice=15.0000 UnitsInStock=15
ProductID=55 ProductName=Pâté chinois Category=Meat/Poultry UnitPrice=24.0000 UnitsInStock=115
ProductID=16 ProductName=Pavlova Category=Confections UnitPrice=17.4500 UnitsInStock=29
ProductID=53 ProductName=Perth Pasties Category=Meat/Poultry UnitPrice=32.8000 UnitsInStock=0
ProductID=11 ProductName=Queso Cabrales Category=Dairy Products UnitPrice=21.0000 UnitsInStock=22
ProductID=12 ProductName=Queso Manchego La Pastora Category=Dairy Products UnitPrice=38.0000 UnitsInStock=86
ProductID=59 ProductName=Raclette Courdavault Category=Dairy Products UnitPrice=55.0000 UnitsInStock=79
ProductID=57 ProductName=Ravioli Angelo Category=Grains/Cereals UnitPrice=19.5000 UnitsInStock=36
ProductID=75 ProductName=Rhönbräu Klosterbier Category=Beverages UnitPrice=7.7500 UnitsInStock=125
ProductID=73 ProductName=Röd Kaviar Category=Seafood UnitPrice=15.0000 UnitsInStock=101
ProductID=45 ProductName=Rogede sild Category=Seafood UnitPrice=9.5000 UnitsInStock=5
ProductID=28 ProductName=Rössle Sauerkraut Category=Produce UnitPrice=45.6000 UnitsInStock=26
ProductID=34 ProductName=Sasquatch Ale Category=Beverages UnitPrice=14.0000 UnitsInStock=111
ProductID=27 ProductName=Schoggi Schokolade Category=Confections UnitPrice=43.9000 UnitsInStock=49
ProductID=68 ProductName=Scottish Longbreads Category=Confections UnitPrice=12.5000 UnitsInStock=6
ProductID=42 ProductName=Singaporean Hokkien Fried Mee Category=Grains/Cereals UnitPrice=14.0000 UnitsInStock=26
ProductID=20 ProductName=Sir Rodney's Marmalade Category=Confections UnitPrice=81.0000 UnitsInStock=40
ProductID=21 ProductName=Sir Rodney's Scones Category=Confections UnitPrice=10.0000 UnitsInStock=3
ProductID=61 ProductName=Sirop d'érable Category=Condiments UnitPrice=28.5000 UnitsInStock=113
ProductID=46 ProductName=Spegesild Category=Seafood UnitPrice=12.0000 UnitsInStock=95
ProductID=35 ProductName=Steeleye Stout Category=Beverages UnitPrice=18.0000 UnitsInStock=20
ProductID=62 ProductName=Tarte au sucre Category=Confections UnitPrice=49.3000 UnitsInStock=17
ProductID=19 ProductName=Teatime Chocolate Biscuits Category=Confections UnitPrice=9.2000 UnitsInStock=25
ProductID=29 ProductName=Thüringer Rostbratwurst Category=Meat/Poultry UnitPrice=123.7900 UnitsInStock=0
ProductID=14 ProductName=Tofu Category=Produce UnitPrice=23.2500 UnitsInStock=35
ProductID=54 ProductName=Tourtière Category=Meat/Poultry UnitPrice=7.4500 UnitsInStock=21
ProductID=23 ProductName=Tunnbröd Category=Grains/Cereals UnitPrice=9.0000 UnitsInStock=61
ProductID=7 ProductName=Uncle Bob's Organic Dried Pears Category=Produce UnitPrice=30.0000 UnitsInStock=15
ProductID=50 ProductName=Valkoinen suklaa Category=Confections UnitPrice=16.2500 UnitsInStock=65
ProductID=63 ProductName=Vegie-spread Category=Condiments UnitPrice=43.9000 UnitsInStock=24
ProductID=64 ProductName=Wimmers gute Semmelknödel Category=Grains/Cereals UnitPrice=33.2500 UnitsInStock=22
ProductID=47 ProductName=Zaanse koeken Category=Confections UnitPrice=9.5000 UnitsInStock=36

OrderBy - Comparer

This sample uses an OrderBy clause with a custom comparer to do a case-insensitive sort of the words in an array.


     
       
  1. public void Linq31()
  2. {
  3.     string[] words = { "aPPLE""AbAcUs""bRaNcH""BlUeBeRrY""ClOvEr""cHeRry" };
  4.  
  5.     var sortedWords = words.OrderBy(a => a, new CaseInsensitiveComparer());
  6.  
  7.     ObjectDumper.Write(sortedWords);
  8. }
  9.  
  10. public class CaseInsensitiveComparer : IComparer<string>
  11. {
  12.     public int Compare(string x, string y)
  13.     {
  14.         return string.Compare(x, y, StringComparison.OrdinalIgnoreCase);
  15.     }
  16. }

Result

AbAcUs
aPPLE
BlUeBeRrY
bRaNcH
cHeRry
ClOvEr

OrderByDescending - Simple 1

This sample uses orderby and descending to sort a list of doubles from highest to lowest.


     
       
  1. public void Linq32()
  2. {
  3.     double[] doubles = { 1.72.31.94.12.9 };
  4.  
  5.     var sortedDoubles =
  6.         from d in doubles
  7.         orderby d descending
  8.         select d;
  9.  
  10.     Console.WriteLine("The doubles from highest to lowest:");
  11.     foreach (var d in sortedDoubles)
  12.     {
  13.         Console.WriteLine(d);
  14.     }
  15. }

Result

The doubles from highest to lowest:
4.1
2.9
2.3
1.9
1.7

OrderByDescending - Simple 2

This sample uses orderby to sort a list of products by units in stock from highest to lowest.


     
       
  1. public void Linq33()
  2. {
  3.     List<Product> products = GetProductList();
  4.  
  5.     var sortedProducts =
  6.         from p in products
  7.         orderby p.UnitsInStock descending
  8.         select p;
  9.  
  10.     ObjectDumper.Write(sortedProducts);
  11. }

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
ProductID=58 ProductName=Escargots de Bourgogne Category=Seafood UnitPrice=13.2500 UnitsInStock=62
ProductID=23 ProductName=Tunnbröd Category=Grains/Cereals UnitPrice=9.0000 UnitsInStock=61
ProductID=76 ProductName=Lakkalikööri Category=Beverages UnitPrice=18.0000 UnitsInStock=57
ProductID=4 ProductName=Chef Anton's Cajun Seasoning Category=Condiments UnitPrice=22.0000 UnitsInStock=53
ProductID=67 ProductName=Laughing Lumberjack Lager Category=Beverages UnitPrice=14.0000 UnitsInStock=52
ProductID=27 ProductName=Schoggi Schokolade Category=Confections UnitPrice=43.9000 UnitsInStock=49
ProductID=18 ProductName=Carnarvon Tigers Category=Seafood UnitPrice=62.5000 UnitsInStock=42
ProductID=20 ProductName=Sir Rodney's Marmalade Category=Confections UnitPrice=81.0000 UnitsInStock=40
ProductID=1 ProductName=Chai Category=Beverages UnitPrice=18.0000 UnitsInStock=39
ProductID=15 ProductName=Genen Shouyu Category=Condiments UnitPrice=15.5000 UnitsInStock=39
ProductID=52 ProductName=Filo Mix Category=Grains/Cereals UnitPrice=7.0000 UnitsInStock=38
ProductID=47 ProductName=Zaanse koeken Category=Confections UnitPrice=9.5000 UnitsInStock=36
ProductID=57 ProductName=Ravioli Angelo Category=Grains/Cereals UnitPrice=19.5000 UnitsInStock=36
ProductID=14 ProductName=Tofu Category=Produce UnitPrice=23.2500 UnitsInStock=35
ProductID=77 ProductName=Original Frankfurter grüne Soße Category=Condiments UnitPrice=13.0000 UnitsInStock=32
ProductID=10 ProductName=Ikura Category=Seafood UnitPrice=31.0000 UnitsInStock=31
ProductID=9 ProductName=Mishi Kobe Niku Category=Meat/Poultry UnitPrice=97.0000 UnitsInStock=29
ProductID=16 ProductName=Pavlova Category=Confections UnitPrice=17.4500 UnitsInStock=29
ProductID=44 ProductName=Gula Malacca Category=Condiments UnitPrice=19.4500 UnitsInStock=27
ProductID=28 ProductName=Rössle Sauerkraut Category=Produce UnitPrice=45.6000 UnitsInStock=26
ProductID=42 ProductName=Singaporean Hokkien Fried Mee Category=Grains/Cereals UnitPrice=14.0000 UnitsInStock=26
ProductID=69 ProductName=Gudbrandsdalsost Category=Dairy Products UnitPrice=36.0000 UnitsInStock=26
ProductID=71 ProductName=Flotemysost Category=Dairy Products UnitPrice=21.5000 UnitsInStock=26
ProductID=19 ProductName=Teatime Chocolate Biscuits Category=Confections UnitPrice=9.2000 UnitsInStock=25
ProductID=13 ProductName=Konbu Category=Seafood UnitPrice=6.0000 UnitsInStock=24
ProductID=63 ProductName=Vegie-spread Category=Condiments UnitPrice=43.9000 UnitsInStock=24
ProductID=11 ProductName=Queso Cabrales Category=Dairy Products UnitPrice=21.0000 UnitsInStock=22
ProductID=64 ProductName=Wimmers gute Semmelknödel Category=Grains/Cereals UnitPrice=33.2500 UnitsInStock=22
ProductID=54 ProductName=Tourtière Category=Meat/Poultry UnitPrice=7.4500 UnitsInStock=21
ProductID=56 ProductName=Gnocchi di nonna Alice Category=Grains/Cereals UnitPrice=38.0000 UnitsInStock=21
ProductID=24 ProductName=Guaraná Fantástica Category=Beverages UnitPrice=4.5000 UnitsInStock=20
ProductID=35 ProductName=Steeleye Stout Category=Beverages UnitPrice=18.0000 UnitsInStock=20
ProductID=51 ProductName=Manjimup Dried Apples Category=Produce UnitPrice=53.0000 UnitsInStock=20
ProductID=60 ProductName=Camembert Pierrot Category=Dairy Products UnitPrice=34.0000 UnitsInStock=19
ProductID=2 ProductName=Chang Category=Beverages UnitPrice=19.0000 UnitsInStock=17
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=62 ProductName=Tarte au sucre Category=Confections UnitPrice=49.3000 UnitsInStock=17
ProductID=7 ProductName=Uncle Bob's Organic Dried Pears Category=Produce UnitPrice=30.0000 UnitsInStock=15
ProductID=26 ProductName=Gumbär Gummibärchen Category=Confections UnitPrice=31.2300 UnitsInStock=15
ProductID=48 ProductName=Chocolade Category=Confections UnitPrice=12.7500 UnitsInStock=15
ProductID=70 ProductName=Outback Lager Category=Beverages UnitPrice=15.0000 UnitsInStock=15
ProductID=72 ProductName=Mozzarella di Giovanni Category=Dairy Products UnitPrice=34.8000 UnitsInStock=14
ProductID=3 ProductName=Aniseed Syrup Category=Condiments UnitPrice=10.0000 UnitsInStock=13
ProductID=37 ProductName=Gravad lax Category=Seafood UnitPrice=26.0000 UnitsInStock=11
ProductID=30 ProductName=Nord-Ost Matjeshering Category=Seafood UnitPrice=25.8900 UnitsInStock=10
ProductID=49 ProductName=Maxilaku Category=Confections UnitPrice=20.0000 UnitsInStock=10
ProductID=32 ProductName=Mascarpone Fabioli Category=Dairy Products UnitPrice=32.0000 UnitsInStock=9
ProductID=8 ProductName=Northwoods Cranberry Sauce Category=Condiments UnitPrice=40.0000 UnitsInStock=6
ProductID=68 ProductName=Scottish Longbreads Category=Confections UnitPrice=12.5000 UnitsInStock=6
ProductID=45 ProductName=Rogede sild Category=Seafood UnitPrice=9.5000 UnitsInStock=5
ProductID=66 ProductName=Louisiana Hot Spiced Okra Category=Condiments UnitPrice=17.0000 UnitsInStock=4
ProductID=74 ProductName=Longlife Tofu Category=Produce UnitPrice=10.0000 UnitsInStock=4
ProductID=21 ProductName=Sir Rodney's Scones Category=Confections UnitPrice=10.0000 UnitsInStock=3
ProductID=5 ProductName=Chef Anton's Gumbo Mix Category=Condiments UnitPrice=21.3500 UnitsInStock=0
ProductID=17 ProductName=Alice Mutton Category=Meat/Poultry UnitPrice=39.0000 UnitsInStock=0
ProductID=29 ProductName=Thüringer Rostbratwurst Category=Meat/Poultry UnitPrice=123.7900 UnitsInStock=0
ProductID=31 ProductName=Gorgonzola Telino Category=Dairy Products UnitPrice=12.5000 UnitsInStock=0
ProductID=53 ProductName=Perth Pasties Category=Meat/Poultry UnitPrice=32.8000 UnitsInStock=0

OrderByDescending - Comparer

This sample uses an OrderBy clause with a custom comparer to do a case-insensitive descending sort of the words in an array.


     
       
  1. public void Linq34()
  2. {
  3.     string[] words = { "aPPLE""AbAcUs""bRaNcH""BlUeBeRrY""ClOvEr""cHeRry" };
  4.  
  5.     var sortedWords = words.OrderByDescending(a => a, new CaseInsensitiveComparer());
  6.  
  7.     ObjectDumper.Write(sortedWords);
  8. }
  9.  
  10. public class CaseInsensitiveComparer : IComparer<string>
  11. {
  12.     public int Compare(string x, string y)
  13.     {
  14.         return string.Compare(x, y, StringComparison.OrdinalIgnoreCase);
  15.     }
  16. }

Result

ClOvEr
cHeRry
bRaNcH
BlUeBeRrY
aPPLE
AbAcUs

ThenBy - Simple

This sample uses a compound orderby to sort a list of digits, first by length of their name, and then alphabetically by the name itself.


     
       
  1. public void Linq35()
  2. {
  3.     string[] digits = { "zero""one""two""three""four""five""six""seven""eight""nine" };
  4.  
  5.     var sortedDigits =
  6.         from d in digits
  7.         orderby d.Length, d
  8.         select d;
  9.  
  10.     Console.WriteLine("Sorted digits:");
  11.     foreach (var d in sortedDigits)
  12.     {
  13.         Console.WriteLine(d);
  14.     }
  15. }

Result

Sorted digits:
one
six
two
five
four
nine
zero
eight
seven
three

ThenBy - Comparer

This sample uses an OrderBy and a ThenBy clause with a custom comparer to sort first by word length and then by a case-insensitive sort of the words in an array.


     
       
  1. public void Linq36()
  2. {
  3.     string[] words = { "aPPLE""AbAcUs""bRaNcH""BlUeBeRrY""ClOvEr""cHeRry" };
  4.  
  5.     var sortedWords =
  6.         words.OrderBy(a => a.Length)
  7.              .ThenBy(a => a, new CaseInsensitiveComparer());
  8.  
  9.     ObjectDumper.Write(sortedWords);
  10. }
  11.  
  12. public class CaseInsensitiveComparer : IComparer<string>
  13. {
  14.     public int Compare(string x, string y)
  15.     {
  16.         return string.Compare(x, y, StringComparison.OrdinalIgnoreCase);
  17.     }
  18. }

Result

aPPLE
AbAcUs
bRaNcH
cHeRry
ClOvEr
BlUeBeRrY

ThenByDescending - Simple

This sample uses a compound orderby to sort a list of products, first by category, and then by unit price, from highest to lowest.


     
       
  1. public void Linq37()
  2. {
  3.     List<Product> products = GetProductList();
  4.  
  5.     var sortedProducts =
  6.         from p in products
  7.         orderby p.Category, p.UnitPrice descending
  8.         select p;
  9.  
  10.     ObjectDumper.Write(sortedProducts);
  11. }

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
ProductID=3 ProductName=Aniseed Syrup Category=Condiments UnitPrice=10.0000 UnitsInStock=13
ProductID=20 ProductName=Sir Rodney's Marmalade Category=Confections UnitPrice=81.0000 UnitsInStock=40
ProductID=62 ProductName=Tarte au sucre Category=Confections UnitPrice=49.3000 UnitsInStock=17
ProductID=27 ProductName=Schoggi Schokolade Category=Confections UnitPrice=43.9000 UnitsInStock=49
ProductID=26 ProductName=Gumbär Gummibärchen Category=Confections UnitPrice=31.2300 UnitsInStock=15
ProductID=49 ProductName=Maxilaku Category=Confections UnitPrice=20.0000 UnitsInStock=10
ProductID=16 ProductName=Pavlova Category=Confections UnitPrice=17.4500 UnitsInStock=29
ProductID=50 ProductName=Valkoinen suklaa Category=Confections UnitPrice=16.2500 UnitsInStock=65
ProductID=25 ProductName=NuNuCa Nuß-Nougat-Creme Category=Confections UnitPrice=14.0000 UnitsInStock=76
ProductID=48 ProductName=Chocolade Category=Confections UnitPrice=12.7500 UnitsInStock=15
ProductID=68 ProductName=Scottish Longbreads Category=Confections UnitPrice=12.5000 UnitsInStock=6
ProductID=21 ProductName=Sir Rodney's Scones Category=Confections UnitPrice=10.0000 UnitsInStock=3
ProductID=47 ProductName=Zaanse koeken Category=Confections UnitPrice=9.5000 UnitsInStock=36
ProductID=19 ProductName=Teatime Chocolate Biscuits Category=Confections UnitPrice=9.2000 UnitsInStock=25
ProductID=59 ProductName=Raclette Courdavault Category=Dairy Products UnitPrice=55.0000 UnitsInStock=79
ProductID=12 ProductName=Queso Manchego La Pastora Category=Dairy Products UnitPrice=38.0000 UnitsInStock=86
ProductID=69 ProductName=Gudbrandsdalsost Category=Dairy Products UnitPrice=36.0000 UnitsInStock=26
ProductID=72 ProductName=Mozzarella di Giovanni Category=Dairy Products UnitPrice=34.8000 UnitsInStock=14
ProductID=60 ProductName=Camembert Pierrot Category=Dairy Products UnitPrice=34.0000 UnitsInStock=19
ProductID=32 ProductName=Mascarpone Fabioli Category=Dairy Products UnitPrice=32.0000 UnitsInStock=9
ProductID=71 ProductName=Flotemysost Category=Dairy Products UnitPrice=21.5000 UnitsInStock=26
ProductID=11 ProductName=Queso Cabrales Category=Dairy Products UnitPrice=21.0000 UnitsInStock=22
ProductID=31 ProductName=Gorgonzola Telino Category=Dairy Products UnitPrice=12.5000 UnitsInStock=0
ProductID=33 ProductName=Geitost Category=Dairy Products UnitPrice=2.5000 UnitsInStock=112
ProductID=56 ProductName=Gnocchi di nonna Alice Category=Grains/Cereals UnitPrice=38.0000 UnitsInStock=21
ProductID=64 ProductName=Wimmers gute Semmelknödel Category=Grains/Cereals UnitPrice=33.2500 UnitsInStock=22
ProductID=22 ProductName=Gustaf's Knäckebröd Category=Grains/Cereals UnitPrice=21.0000 UnitsInStock=104
ProductID=57 ProductName=Ravioli Angelo Category=Grains/Cereals UnitPrice=19.5000 UnitsInStock=36
ProductID=42 ProductName=Singaporean Hokkien Fried Mee Category=Grains/Cereals UnitPrice=14.0000 UnitsInStock=26
ProductID=23 ProductName=Tunnbröd Category=Grains/Cereals UnitPrice=9.0000 UnitsInStock=61
ProductID=52 ProductName=Filo Mix Category=Grains/Cereals UnitPrice=7.0000 UnitsInStock=38
ProductID=29 ProductName=Thüringer Rostbratwurst Category=Meat/Poultry UnitPrice=123.7900 UnitsInStock=0
ProductID=9 ProductName=Mishi Kobe Niku Category=Meat/Poultry UnitPrice=97.0000 UnitsInStock=29
ProductID=17 ProductName=Alice Mutton Category=Meat/Poultry UnitPrice=39.0000 UnitsInStock=0
ProductID=53 ProductName=Perth Pasties Category=Meat/Poultry UnitPrice=32.8000 UnitsInStock=0
ProductID=55 ProductName=Pâté chinois Category=Meat/Poultry UnitPrice=24.0000 UnitsInStock=115
ProductID=54 ProductName=Tourtière Category=Meat/Poultry UnitPrice=7.4500 UnitsInStock=21
ProductID=51 ProductName=Manjimup Dried Apples Category=Produce UnitPrice=53.0000 UnitsInStock=20
ProductID=28 ProductName=Rössle Sauerkraut Category=Produce UnitPrice=45.6000 UnitsInStock=26
ProductID=7 ProductName=Uncle Bob's Organic Dried Pears Category=Produce UnitPrice=30.0000 UnitsInStock=15
ProductID=14 ProductName=Tofu Category=Produce UnitPrice=23.2500 UnitsInStock=35
ProductID=74 ProductName=Longlife Tofu Category=Produce UnitPrice=10.0000 UnitsInStock=4
ProductID=18 ProductName=Carnarvon Tigers Category=Seafood UnitPrice=62.5000 UnitsInStock=42
ProductID=10 ProductName=Ikura Category=Seafood UnitPrice=31.0000 UnitsInStock=31
ProductID=37 ProductName=Gravad lax Category=Seafood UnitPrice=26.0000 UnitsInStock=11
ProductID=30 ProductName=Nord-Ost Matjeshering Category=Seafood UnitPrice=25.8900 UnitsInStock=10
ProductID=36 ProductName=Inlagd Sill Category=Seafood UnitPrice=19.0000 UnitsInStock=112
ProductID=40 ProductName=Boston Crab Meat Category=Seafood UnitPrice=18.4000 UnitsInStock=123
ProductID=73 ProductName=Röd Kaviar Category=Seafood UnitPrice=15.0000 UnitsInStock=101
ProductID=58 ProductName=Escargots de Bourgogne Category=Seafood UnitPrice=13.2500 UnitsInStock=62
ProductID=46 ProductName=Spegesild Category=Seafood UnitPrice=12.0000 UnitsInStock=95
ProductID=41 ProductName=Jack's New England Clam Chowder Category=Seafood UnitPrice=9.6500 UnitsInStock=85
ProductID=45 ProductName=Rogede sild Category=Seafood UnitPrice=9.5000 UnitsInStock=5
ProductID=13 ProductName=Konbu Category=Seafood UnitPrice=6.0000 UnitsInStock=24

ThenByDescending - Comparer

This sample uses an OrderBy and a ThenBy clause with a custom comparer to sort first by word length and then by a case-insensitive descending sort of the words in an array.


     
       
  1. public void Linq38()
  2. {
  3.     string[] words = { "aPPLE""AbAcUs""bRaNcH""BlUeBeRrY""ClOvEr""cHeRry" };
  4.  
  5.     var sortedWords =
  6.         words.OrderBy(a => a.Length)
  7.              .ThenByDescending(a => a, new CaseInsensitiveComparer());
  8.  
  9.     ObjectDumper.Write(sortedWords);
  10. }
  11.  
  12. public class CaseInsensitiveComparer : IComparer<string>
  13. {
  14.     public int Compare(string x, string y)
  15.     {
  16.         return string.Compare(x, y, StringComparison.OrdinalIgnoreCase);
  17.     }
  18. }

Result

aPPLE
ClOvEr
cHeRry
bRaNcH
AbAcUs
BlUeBeRrY

Reverse

This sample uses Reverse to create a list of all digits in the array whose second letter is 'i' that is reversed from the order in the original array.


     
       
  1. public void Linq39()
  2. {
  3.     string[] digits = { "zero""one""two""three""four""five""six""seven""eight""nine" };
  4.  
  5.     var reversedIDigits = (
  6.         from d in digits
  7.         where d[1] == 'i'
  8.         select d)
  9.         .Reverse();
  10.  
  11.     Console.WriteLine("A backwards list of the digits with a second character of 'i':");
  12.     foreach (var d in reversedIDigits)
  13.     {
  14.         Console.WriteLine(d);
  15.     }
  16. }

Result

A backwards list of the digits with a second character of 'i':
nine
eight
six
five

你可能感兴趣的:(sample)