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
    }

转载于:https://www.cnblogs.com/Reborn/archive/2010/04/17/1714420.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值