Linq101-Ordering
2014-12-30 16:32
141 查看
using System; using System.Collections.Generic; using System.Linq; namespace Linq101 { class Ordering { /// <summary> /// This sample uses orderby to sort a list of words alphabetically. /// </summary> 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 word in sortedWords) { Console.WriteLine(word); } } /// <summary> /// This sample uses orderby to sort a list of words by length. /// </summary> 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 word in sortedWords) { Console.WriteLine(word); } } /// <summary> /// This sample uses orderby to sort a list of products by name. /// </summary> public void Linq30() { var products = Data.GetProductList(); var query = from p in products orderby p.ProductName select p; ObjectDumper.Write(query); } /// <summary> /// This sample uses an OrderBy clause with a custom comparer to do a case-insensitive sort of the words in an array. /// </summary> public void Linq31() { string[] words = { "aPPLE", "AbAcUs", "bRaNcH", "BlUeBeRrY", "ClOvEr", "cHeRry" }; var query = words.OrderBy(w => w, new CaseInsensitiveComparer()); ObjectDumper.Write(query); } private class CaseInsensitiveComparer : IComparer<string> { public int Compare(string x, string y) { return string.Compare(x, y, StringComparison.InvariantCultureIgnoreCase); } } /// <summary> /// This sample uses orderby and descending to sort a list of doubles from highest to lowest. /// </summary> 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; ObjectDumper.Write(sortedDoubles); } /// <summary> /// This sample uses orderby to sort a list of products by units in stock from highest to lowest. /// </summary> public void Linq33() { var products = Data.GetProductList(); var query = from p in products orderby p.UnitsInStock descending select p; ObjectDumper.Write(query); } /// <summary> /// This sample uses an OrderBy clause with a custom comparer to do a case-insensitive descending sort of the words in an array. /// </summary> public void Linq34() { string[] words = { "aPPLE", "AbAcUs", "bRaNcH", "BlUeBeRrY", "ClOvEr", "cHeRry" }; var query = words.OrderByDescending(w => w, new CaseInsensitiveComparer()); ObjectDumper.Write(query); } /// <summary> /// 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. /// </summary> 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); } } /// <summary> /// 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. /// </summary> public void Linq36() { string[] words = { "aPPLE", "AbAcUs", "bRaNcH", "BlUeBeRrY", "ClOvEr", "cHeRry" }; var sortedWords = words .OrderBy(w => w.Length) .ThenBy(w => w, new CaseInsensitiveComparer()); ObjectDumper.Write(sortedWords); } /// <summary> /// This sample uses a compound orderby to sort a list of products, first by category, and then by unit price, from highest to lowest. /// </summary> public void Linq37() { var products = Data.GetProductList(); var query = from p in products orderby p.Category, p.UnitPrice descending select p; ObjectDumper.Write(query); } /// <summary> /// 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. /// </summary> public void Linq38() { string[] words = { "aPPLE", "AbAcUs", "bRaNcH", "BlUeBeRrY", "ClOvEr", "cHeRry" }; var sortedWords = words.OrderBy(w => w.Length).ThenByDescending(w => w, new CaseInsensitiveComparer()); ObjectDumper.Write(sortedWords); } /// <summary> /// 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. /// </summary> public void Linq39() { string[] digits = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" }; var sortedDigits = (from d in digits where d[1] == 'i' select d) .Reverse(); ObjectDumper.Write(sortedDigits); } } }
相关文章推荐
- Linq101-Quantifiers
- Linq101-Set
- Linq101-Element
- linq 的查询的学习 (google 中输入 101 linq) -- select的应用
- Linq101-Conversion Operators
- Linq101-Generation
- Linq101-CustomSequence
- Linq101-Restriction
- Linq101-Projection
- Linq Ordering Operator
- Linq101-Aggregate
- 101 LINQ Samples(from MSDN)
- Linq101-Miscellaneous
- Linq101-QueryExecution
- Linq101-Join
- Linq 101 工具和源码
- 101 LINQ Samples(from MSDN)
- 【编译】StreamInsight的LINQ示例101
- Linq101-Partitioning
- Linq101-Grouping Operators