当前位置: 首页>>代码示例>>C#>>正文


C# Student.OrderBy方法代码示例

本文整理汇总了C#中Student.OrderBy方法的典型用法代码示例。如果您正苦于以下问题:C# Student.OrderBy方法的具体用法?C# Student.OrderBy怎么用?C# Student.OrderBy使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Student的用法示例。


在下文中一共展示了Student.OrderBy方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Main

    // Using the extension methods OrderBy() and ThenBy() with lambda expressions
    // sort the students by first name and last name in descending order.
    // Rewrite the same with LINQ.
    private static void Main()
    {
        var students = new Student[5]
        {
            new Student("Ivo", "Georgiev"),
            new Student("Ani", "Zlateva"),
            new Student("Ivo", "Antonov"),
            new Student("Johnny", "Bravo"),
            new Student("Super", "Man"),
        };

        // Print the students
        Console.WriteLine("Unsorted:");
        PrintStudents(students);

        // Sort by first name and last name using extension methods and lambda expressions
        var sortedByMethods = students.OrderBy(student => student.FristName).ThenBy(student => student.LastName);

        // Print the sorted list
        Console.WriteLine("\nSorted using LINQ method syntax:");
        PrintStudents(sortedByMethods);

        // Sort by first name and last name using LINQ query
        var sortedByQuery = from student in students
                           orderby student.FristName, student.LastName
                           select student;

        // Print the sorted list
        Console.WriteLine("\nSorted using LINQ query syntax:");
        PrintStudents(sortedByQuery);
    }
开发者ID:MarKamenov,项目名称:TelerikAcademy,代码行数:34,代码来源:StudentsOrderBy.cs

示例2: Main

    static void Main()
    {
        Student[] students = new Student[]
        {
            new Student("Jim", "Raynor", 10),
            new Student("Sarah", "Kerrigan", 10),
            new Student("Arcturus", "Mengsk", 8),
            new Student("Valerian", "Mengsk", 12),
            new Student("Kate ", "Lockwell", 3),
            new Student("Edmund ", "Duke", 7),
            new Student("Samir", "Duran", 1),
            new Student("Alexei", "Stukov", 11),
            new Student("Tychus", "Findlay", 2),
            new Student("Nova", "Terra", 9),
        };

        Student[] sortedStudents = students.OrderBy(x => x.Grade).ToArray();

        Console.WriteLine("Students sorted by grade:");
        foreach (var student in sortedStudents)
        {
            Console.WriteLine(student);
        }
        Console.WriteLine();

        Worker[] workers = new Worker[]
        {
            new Worker("Michael", "Charles", 200M, 2),
            new Worker("James", "Gray", 430M, 8),
            new Worker("Ronald", "Frank", 500M, 6),
            new Worker("Harry", "Tyler", 320M, 4),
            new Worker("Steven", "Clarke", 800M, 12),
            new Worker("Mary", "Carter", 500M, 4),
            new Worker("Emily", "Richard", 800M, 8),
            new Worker("Linda", "Williams", 400M, 6),
            new Worker("Ashley", "Rose", 10M, 1),
            new Worker("Lisa", "Mark", 540M, 12),
        };

        Worker[] sortedWorkers = workers.OrderByDescending(x => x.MoneyPerHour()).ToArray();

        Console.WriteLine("Workers sorted by money per hour:");
        foreach (var worker in sortedWorkers)
        {
            Console.WriteLine(worker);
        }
        Console.WriteLine();

        List<Human> humans = new List<Human>(students);
        humans.AddRange(workers);
        List<Human> sortedHumans = humans.OrderBy(x => x.FirstName).ThenBy(x => x.LastName).ToList();

        Console.WriteLine("Humans sorted by name:");
        foreach (var human in sortedHumans)
        {
            Console.WriteLine(human);
        }
        Console.WriteLine();
    }
开发者ID:andon-andonov,项目名称:TelerikAcademy,代码行数:59,代码来源:TestApp.cs

示例3: Main

    static void Main()
    {
        Student[] students = new Student[10]
        {
            new Student("Martin", "Georgiev", 6),
            new Student("Ivan", "Petrov", 5),
            new Student("Pavel", "Kolev", 4),
            new Student("Svetlin", "Nakov", 2),
            new Student("Stefka", "Georgieva", 4),
            new Student("Nikolai", "Kostov", 6),
            new Student("Ina", "Dobrilova", 6),
            new Student("Johnny", "Bravo", 3),
            new Student("Iva", "Dragomirova", 2),
            new Student("Izabela", "Dimitrova", 5)
        };

        var orderedStudents = students.OrderBy(student => student.Grade); 
            //from student in students
            //orderby student.Grade ascending
            //select student;
        
        Console.WriteLine(students[0].GetName());
        Console.WriteLine();

        Worker[] workers = new Worker[10]
        {
            new Worker(250, 8, "Petyr", "Georgiev"),
            new Worker(300, 8, "Martin", "Ivanov"),
            new Worker(200, 6, "Dimityr", "Berbatov"),
            new Worker(600, 4, "Santa", "Claus"),
            new Worker(250, 12, "Bai", "Ivan"),
            new Worker(280, 16, "Mariq", "Pavlova"),
            new Worker(310, 8, "Metodi", "Vladigerov"),
            new Worker(420, 10, "Valentin", "Donchov"),
            new Worker(380, 8, "Kiril", "Minkov"),
            new Worker(250, 6, "Danail", "Yovchev")
        };

        var orderedWorkers = //workers.OrderBy(worker => worker.MoneyPerHour());
            from worker in workers
            orderby worker.MoneyPerHour() descending
            select worker;

        var merged = orderedStudents.Concat<Human>(orderedWorkers);
        merged =
            from item in merged
            orderby item.FirstName ascending, item.LastName
            select item;

        foreach (var item in merged)
        {
            Console.WriteLine(item.FirstName + " " + item.LastName);
        }

    }
开发者ID:ScreeM92,项目名称:Software-University,代码行数:55,代码来源:TestClass.cs

示例4: Main

    static void Main()
    {
        Student[] students = new Student[10];
        Worker[] workers = new Worker[10];

        for (int index = 0; index < 10; index++)
        {
            string studentFirstName = ((firstNames)randGenerator.Next(9)).ToString();
            string studentLastName = ((lastNames)randGenerator.Next(9)).ToString();
            string studentFacultyNumber = (randGenerator.Next(1000000000) + 1000000000).ToString();

            students[index] = new Student(studentFirstName, studentLastName, studentFacultyNumber);

            string workerFirstName = ((firstNames)randGenerator.Next(9)).ToString();
            string workerLastName = ((lastNames)randGenerator.Next(9)).ToString();
            double weekSalary = randGenerator.NextDouble() * 1000 + 1000;
            int workHours = randGenerator.Next(5) + 5;

            workers[index] = new Worker(workerFirstName, workerLastName, weekSalary, workHours);
        }

        workers = workers.OrderBy(w => w.MoneyPerHour()).ToArray();
        students = students.OrderBy(s => s.FacultyNumber).ToArray();

        Human[] humans = new Human[workers.Length + students.Length];
        int humansCounter = 0;

        Console.WriteLine("Workers sorted: ");
        for (int index = 0; index < workers.Length; index++)
        {
            humans[humansCounter] = workers[index];
            humansCounter++;
            Console.WriteLine(workers[index].FirstName + " " + workers[index].LastName + " --> " + workers[index].MoneyPerHour());
        }

        Console.WriteLine();
        Console.WriteLine("Students sorted:");
        for (int index = 0; index < students.Length; index++)
        {
            humans[humansCounter] = students[index];
            humansCounter++;
            Console.WriteLine(students[index].FirstName + " " + students[index].LastName + " --> " + students[index].FacultyNumber);
        }

        humans = humans.OrderBy(h => h.FirstName).ThenBy(h => h.LastName).ToArray();

        Console.WriteLine();
        Console.WriteLine("Humans sorted: ");
        for (int index = 0; index < humans.Length; index++)
        {
            Console.WriteLine(humans[index].FirstName + " " + humans[index].LastName);
        }
    }
开发者ID:shnogeorgiev,项目名称:Software-University-Courses,代码行数:53,代码来源:HumanWorkerStudentDemo.cs

示例5: Main

    static void Main()
    {
        Student[] listStudents = new Student[5];

        listStudents[0].FirstName = "Pesho";
        listStudents[0].LastName = "Peshev";
        listStudents[0].age = 19;

        listStudents[1].FirstName = "Kiro";
        listStudents[1].LastName = "Qnev";
        listStudents[1].age = 17;

        listStudents[2].FirstName = "Ivan";
        listStudents[2].LastName = "Ivanov";
        listStudents[2].age = 29;

        listStudents[3].FirstName = "Jeko";
        listStudents[3].LastName = "Iordanov";
        listStudents[3].age = 20;

        listStudents[4].FirstName = "Georgi";
        listStudents[4].LastName = "Penev";
        listStudents[4].age = 24;

        //with lambda functions and OrderBy(), ThenBy()
        Console.WriteLine("With lambda");

        var orderedList = listStudents.OrderBy((student) => student.FirstName).
                                        ThenBy((student) => student.LastName);

        foreach (var student in orderedList)
        {
            Console.WriteLine("{0} {1}", student.FirstName, student.LastName);
        }

        Console.WriteLine("\nWith LINQ");

        //with LINQ
        var sortedList =
            from student in listStudents
            orderby student.FirstName, student.LastName
            select student;

        foreach (var student in sortedList)
        {
            Console.WriteLine("{0} {1}", student.FirstName, student.LastName);
        }
    }
开发者ID:smihaylovit,项目名称:TelerikHW,代码行数:48,代码来源:OrderByThenBy.cs

示例6: Main

        public static void Main()
        {
            Student[] students = new Student[] {
                new Student("Ivan","Ivanov",1),
                new Student("Iva","Ivanova",5),
                new Student("Georgi","Georigev",4),
                new Student("Stefan","Koichev",2),
                new Student("Kolio","Koleshev",6),
                new Student("Deso","Kelesha",7),
                new Student("Donka","Koleva",8),
                new Student("Vanko","Hristov",9),
                new Student("Anatoli","Popov",3),
                new Student("Jivko","Ivanov",9),
                new Student("Toshko","Popov",2),
                new Student("Maria","Stefanova",6),
                new Student("Nikola","Nikolov",8),
                new Student("Eva","Malcheva",6),
            };

            Console.WriteLine("All students:");

            foreach (var student in students)
            {
                Console.WriteLine(student);
            }

            Console.WriteLine("\nTask 18");
            var groupByGroupNumber = from student in students
                                     orderby student.GroupNumber
                                     select student;
            foreach (var student in groupByGroupNumber)
            {
                Console.WriteLine(student);
            }

            Console.WriteLine("\nTask 19");
            var groupByGroupName = students.OrderBy(student => student.GroupName);

            foreach (var student in groupByGroupName)
            {
                Console.WriteLine(student);
            }
        }
开发者ID:RvnDrk,项目名称:C-OOP,代码行数:43,代码来源:GroupedByGroupNumber.cs


注:本文中的Student.OrderBy方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。