3. 排序操作符—【LINQ标准查询操作符】

public class OrderBy_LINQ
    {
        static  string ContextString = System.Configuration.ConfigurationSettings.AppSettings["ContextString"].ToString();
        static DataContext context = new DataContext(ContextString);
        static Table<Contact> contact = context.GetTable<Contact>();

        #region OrderBy
        public static void Print_OrderBy()
        {
            var orderByQuery = from c in contact
                               where c.FirstName.StartsWith("S")
                               orderby c.LastName
                               select new { c.LastName, c.FirstName };
            // 方法语法
            var orderByMethodQuery = contact.Select(c => new { c.LastName, c.FirstName }).Where(c => c.FirstName.StartsWith("S")).OrderBy(c => c.LastName);

            foreach (var item in orderByQuery.Take(10))
            {
                Console.WriteLine(item);
            }

            Console.ReadKey();
        }
        #endregion

        #region OrderByDescending
        public static void Print_OrderByDescending()
        {
            var orderByDescendingQuery = from c in contact
                               where c.FirstName.StartsWith("S")
                               orderby c.LastName descending
                               select new { c.LastName, c.FirstName };
            //方法语法
            var orderByDescendingMethod = contact.Select(c => new { c.LastName, c.FirstName }).Where(c => c.FirstName.StartsWith("S")).OrderByDescending(c => c.LastName);
            foreach (var item in orderByDescendingQuery.Take(10))
            {
                Console.WriteLine(item);
            }

            Console.ReadKey();
        }
        #endregion

        #region ThenBy
        public static void Print_ThenBy()
        {
            var thenByQuery = from c in contact
                                              where c.LastName.StartsWith("E")
                                              orderby c.FirstName, c.LastName                                              
                                              select new { c.FirstName, c.LastName, c.EmailAddress };
            // 方法语法
            var thenByMethodQuery = contact.Select(c => new { c.FirstName, c.LastName, c.EmailAddress }).Where(c => c.LastName.StartsWith("S")).OrderBy(c => c.FirstName).ThenBy(c => c.LastName);

            foreach (var item in thenByMethodQuery.Take(10))
            {
                Console.WriteLine(item);
            }

            Console.ReadKey();
        }
        #endregion

        #region ThenByDescending
        public static void Print_ThenByDescending()
        {
            var thenByDescendingQuery = from c in contact
                                        where c.LastName.StartsWith("B")
                                        orderby c.LastName descending, c.FirstName descending
                                        select new { c.FirstName, c.LastName, c.EmailAddress };
            // 方法语法
            var thenByDescendingMethod = contact.Select(c => new { c.FirstName, c.LastName, c.EmailAddress }).Where(c => c.LastName.StartsWith("B")).OrderByDescending(c => c.LastName).ThenByDescending(c => c.FirstName);

            foreach (var item in thenByDescendingQuery.Take(10))
            {
                Console.WriteLine(item);
            }

            Console.ReadKey();
        }
        #endregion

        #region Reverse
        // reverse()操作符的使用是有限制的,LINQ To SQL 并不支持它,因为LINQ To SQL仅对无序的数据集或多个数据集对应的数据表进行操作.
        public static void Print_Reverse()
        {
            string[] names = { "Oscar", "Mae", "Sabria", "Hannah", "Tom", "Michael" };
            string[] reverse = names.Reverse().ToArray();

            foreach (string item in reverse)
            {
                Console.WriteLine(item);
            }

            Console.ReadKey();
        }
        #endregion
    }
天天来(http://www.daydaycome.com)】- 精选折扣商品,爆料精选,九块九白菜底价尽在天天来!是一个中立的,致力于帮助广大网友买到更有性价比网购产品的分享平台,每天为网友们提供最受追捧 最具性价比 最大幅降价潮流新品资讯。我们的信息大部分来自于网友爆料,如果您发现了优质的产品或好的价格,不妨给我们爆料(谢绝商家)
原文地址:https://www.cnblogs.com/Reborn/p/1714420.html