zoukankan      html  css  js  c++  java
  • C# orderby子句

    注意:对联接运算的结果进行排序。 请注意,排序在联接之后执行。 虽然可以在联接之前将 orderby 子句用于一个或多个源序列,不过通常不建议这样做。 某些 LINQ 提供程序可能不会在联接之后保留该排序。

    在查询表达式中,orderby 子句可导致返回的序列或子序列(组)以升序或降序排序。 若要执行一个或多个次级排序操作,可以指定多个键。 元素类型的默认比较器执行排序。 默认排序顺序为升序。 

    以下示例对学生的姓氏进行主要排序,然后对其名字进行次要排序。

    class OrderbySample2
    {
        // The element type of the data source.
        public class Student
        {
            public string First { get; set; }
            public string Last { get; set; }
            public int ID { get; set; }
        }
    
        public static List<Student> GetStudents()
        {
            // Use a collection initializer to create the data source. Note that each element
            //  in the list contains an inner sequence of scores.
            List<Student> students = new List<Student>
            {
               new Student {First="Svetlana", Last="Omelchenko", ID=111},
               new Student {First="Claire", Last="O'Donnell", ID=112},
               new Student {First="Sven", Last="Mortensen", ID=113},
               new Student {First="Cesar", Last="Garcia", ID=114},
               new Student {First="Debra", Last="Garcia", ID=115} 
            };
    
            return students;
    
        }
        static void Main(string[] args)
        {
            // Create the data source.
            List<Student> students = GetStudents();
            
            // Create the query.
            IEnumerable<Student> sortedStudents =
                from student in students
                orderby student.Last ascending, student.First ascending
                select student;
    
            // Execute the query.
            Console.WriteLine("sortedStudents:");
            foreach (Student student in sortedStudents)
                Console.WriteLine(student.Last + " " + student.First);           
    
            // Now create groups and sort the groups. The query first sorts the names
            // of all students so that they will be in alphabetical order after they are
            // grouped. The second orderby sorts the group keys in alpha order.            
            var sortedGroups =
                from student in students
                orderby student.Last, student.First
                group student by student.Last[0] into newGroup
                orderby newGroup.Key
                select newGroup;
    
            // Execute the query.
            Console.WriteLine(Environment.NewLine + "sortedGroups:");
            foreach (var studentGroup in sortedGroups)
            {
                Console.WriteLine(studentGroup.Key);
                foreach (var student in studentGroup)
                {
                    Console.WriteLine("   {0}, {1}", student.Last, student.First);
                }
            }
    
            // Keep the console window open in debug mode
            Console.WriteLine("Press any key to exit.");
            Console.ReadKey();
        }
    }
    /* Output:  
    sortedStudents:
    Garcia Cesar
    Garcia Debra
    Mortensen Sven
    O'Donnell Claire
    Omelchenko Svetlana
    
    sortedGroups:
    G
       Garcia, Cesar
       Garcia, Debra
    M
       Mortensen, Sven
    O
       O'Donnell, Claire
       Omelchenko, Svetlana
    */
  • 相关阅读:
    Android安装apk
    Android获取应用程序版本信息
    Handler消息传递机制
    Activity的启动模式
    cocopods的使用
    ios9 的新特性
    静态库的制作详解
    真机调试
    时间差计算(给定两时间,转换为时间差)
    socket 通信机制的实现
  • 原文地址:https://www.cnblogs.com/dehuachenyunfei/p/7772188.html
Copyright © 2011-2022 走看看