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
    }
posted @ 2010-04-17 21:20  CityWalker  阅读(265)  评论(0编辑  收藏  举报
天天来