示例#1
0
        private static IEnumerable<Worker> WorkersGenerator()
        {
            var result = new Worker[ARRAY_SIZE];
            for (int index = 0; index < ARRAY_SIZE; index++)
            {
                result[index] = SingleWorkerGenerator();
            }

            return result;
        }
        static void Main()
        {
            Random rand = new Random();
            Student[] students = new Student[10];
            string[] firstNames = new string[] { "Niki", "Doncho", "Pesho", "Maria", "Stamat", "Ivan", "Ivo", "Gosho", "Evlogi", "Svetlin" };
            string[] lastNames = new string[] { "Kostov", "Minkov", "Peshev", "Ivanov", "Stamatov", "Popov", "Kenov", "Goshev", "Hristov", "Nakov" };

            for (int i = 0; i < students.Length; i++)
            {
                string firstName = firstNames[rand.Next(0, 10)];
                string lastName = lastNames[rand.Next(0, 10)];
                uint grade = (uint)rand.Next(1, 5);
                Student student = new Student(firstName, lastName, grade);
                students[i] = student;
            }

            Console.WriteLine("STUDENTS");
            var studentsByGrade = students.OrderBy(st => st.Grade).ToList();
            foreach (var student in studentsByGrade)
            {
                Console.WriteLine("{0} {1}, grade --> {2}", student.FirstName, student.LastName, student.Grade);
            }

            Worker[] workers = new Worker[10];
            for (int i = 0; i < workers.Length; i++)
            {
                string firstName = firstNames[rand.Next(0, 10)];
                string lastName = lastNames[rand.Next(0, 10)];
                decimal weekSalary = (decimal)rand.Next(100, 200);
                Worker worker = new Worker(firstName, lastName, weekSalary, 8);
                workers[i] = worker;
            }

            Console.WriteLine("WORKERS");
            var workersByHourMoney = workers.OrderByDescending(w => w.MoneyPerHour()).ToList();
            foreach (var worker in workersByHourMoney)
            {
                Console.WriteLine("{0} {1} --> {2} money per hour", worker.FirstName, worker.LastName, worker.MoneyPerHour());
            }

            var mergedList = students.Union<Human>(workers).OrderBy(h => h.FirstName).ThenBy(h => h.LastName);
            Console.WriteLine("HUMANS LIST");
            foreach (var human in mergedList)
            {
                string type = human.GetType().Name;
                Console.WriteLine("{0} {1} type --> {2}", human.FirstName, human.LastName, type);
            }
        }
        static void Main()
        {
            List<Student> students = new List<Student>();

            Student student = new Student("Nikola", "Ivanov", 1);
            students.Add(student);
            student = new Student("Zdravka", "Ivanova", 2);
            students.Add(student);
            student = new Student("Dimana", "Todorova", 1);
            students.Add(student);
            student = new Student("Stanimir", "Krasenov", 3);
            students.Add(student);
            student = new Student("Stojan", "Nikolov", 5);
            students.Add(student);
            student = new Student("Zornica", "Petrova", 1);
            students.Add(student);
            student = new Student("Aleksandar", "Gorspodinov", 2);
            students.Add(student);
            student = new Student("Nikolaj", "Ivanov", 3);
            students.Add(student);
            student = new Student("Kiril", "Kirilov", 2);
            students.Add(student);
            student = new Student("Nikola", "Petrov", 5);
            students.Add(student);

            //sort them by grade in ascending order (use LINQ or  OrderBy()  extension method).
            var studentsSortedByGrade = students.OrderBy(stud => stud.Grade);

            Console.WriteLine("Students sorted by grade:");

            foreach (var item in studentsSortedByGrade)
            {
                Console.WriteLine(item);
            }

            Console.WriteLine();

            List<Worker> workers = new List<Worker>();

            Worker worker = new Worker("Petar", "Ivanov", 600m, 8, 5);
            workers.Add(worker);
            worker = new Worker("Petarana", "Nikolova", 300m, 8, 5);
            workers.Add(worker);
            worker = new Worker("Stojan", "Angelov", 400m, 8, 5);
            workers.Add(worker);
            worker = new Worker("Momchil", "Stojanov", 700m, 8, 5);
            workers.Add(worker);
            worker = new Worker("Dona", "Stoeva", 200m, 8, 5);
            workers.Add(worker);
            worker = new Worker("Todor", "Koev", 500m, 24, 2);
            workers.Add(worker);
            worker = new Worker("Kosta", "Ivanov", 700m, 8, 5);
            workers.Add(worker);
            worker = new Worker("Dimitar", "Angelov", 400m, 8, 5);
            workers.Add(worker);
            worker = new Worker("Angel", "Ivanov", 300m, 8, 5);
            workers.Add(worker);
            worker = new Worker("Petar", "Koev", 200.20m, 12, 2);
            workers.Add(worker);

            //sort them by money per hour in descending order.

            var workersSortedByMoneyPerHour = workers
                .Select(wr => new { Name = string.Format("{0} {1}", wr.FirstName, wr.LastName), MoneyPerHour = wr.GetMoneyPerHour() })
                .OrderByDescending(newwr => newwr.MoneyPerHour);

            Console.WriteLine("Workers sorted by money per hour");

            foreach (var item in workersSortedByMoneyPerHour)
            {
                Console.WriteLine(item);
            }

            Console.WriteLine();

            //Merge the lists and sort them by first name and last name.
            var humansList = students.Select(st => (Human)st)
                .Concat(workers.Select(wr => (Human)wr))
                .OrderBy(h => h.FirstName)
                .ThenBy(h => h.LastName);

            Console.WriteLine("Humans sorted by first name and last name");

            foreach (var item in humansList)
            {
                Console.WriteLine(item);
            }

            Console.WriteLine();
        }
示例#4
0
        static void Main(string[] args)
        {
            //Initialize some students
            var stud1 = new Student("Antoan", "Elenkov", 9);
            var stud2 = new Student("Georgi", "Ivanov", 8);
            var stud3 = new Student("PEtar", "Petrov", 12);
            var stud4 = new Student("Angel", "Angelov", 10);
            var stud5 = new Student("Ivan", "Nasran", 7);
            var stud6 = new Student("Dqdo", "Mraz", 9);
            var stud7 = new Student("Super", "Man", 8);
            var stud8 = new Student("Jelezniqt", "Chovek", 12);
            var stud9 = new Student("Telerik", "Ninja", 10);
            var stud10 = new Student("Daffy", "Duck", 7);

            //Initialize some workers
            var worker1 = new Worker("Gosho", "Zidarov", 1100, 40);
            var worker2 = new Worker("Petio", "Iordanov", 1200, 40);
            var worker3 = new Worker("Dqdo", "Koleda", 1500, 30);
            var worker4 = new Worker("Bai", "Ivan", 1500, 40);
            var worker5 = new Worker("Bai", "Kolio", 2500, 20);
            var worker6 = new Worker("Baba", "Koleda", 3000, 60);
            var worker7 = new Worker("Zidar", "Mazachov", 1020, 50);
            var worker8 = new Worker("Maistor", "Pesho", 1200, 25);
            var worker9 = new Worker("Master", "Petrov", 1020, 35.5f);
            var worker10 = new Worker("Number", "One", 1500, 28);

            //List of students
            var listOfStudents = new List<Student>();
            listOfStudents.Add(stud1);
            listOfStudents.Add(stud2);
            listOfStudents.Add(stud3);
            listOfStudents.Add(stud4);
            listOfStudents.Add(stud5);
            listOfStudents.Add(stud6);
            listOfStudents.Add(stud7);
            listOfStudents.Add(stud8);
            listOfStudents.Add(stud9);
            listOfStudents.Add(stud10);

            //The students ordered by grade in ascending order
            var orderByGrade = listOfStudents.OrderBy(x => x.Grade)
                .Select(x=>new{x.FirstName,x.LastName,x.Grade});
            Console.WriteLine("The students ordered by grade in ascending order:");
            Console.WriteLine(String.Join(Environment.NewLine,orderByGrade));
            Console.WriteLine();

            //List of workers
            var listOfWorkers= new List<Worker>();
            listOfWorkers.Add(worker1);
            listOfWorkers.Add(worker2);
            listOfWorkers.Add(worker3);
            listOfWorkers.Add(worker4);
            listOfWorkers.Add(worker5);
            listOfWorkers.Add(worker6);
            listOfWorkers.Add(worker7);
            listOfWorkers.Add(worker8);
            listOfWorkers.Add(worker9);
            listOfWorkers.Add(worker10);

            //The workers ordered by wage perh hour in descending order
            var orderByWage = listOfWorkers.OrderByDescending(x => x.CalculateMoneyPerHour())
                .Select(x => new { x.FirstName, x.LastName, wage = x.CalculateMoneyPerHour() });
            Console.WriteLine("The workers ordered by wage perh hour in descending order:");
            Console.WriteLine(String.Join(Environment.NewLine, orderByWage));
            Console.WriteLine();

            //The workers and students ordered by first and last name
            var orderAllByFirstAndLastName = listOfWorkers.Cast<Human>()
                .Union(listOfStudents.Cast<Human>())
                .OrderBy(x=>x.LastName)
                .OrderBy(x=>x.FirstName)
                .Select(x=>new{x.FirstName,x.LastName});
            Console.WriteLine("The workers and students ordered by first and last name:");
            Console.WriteLine(String.Join(Environment.NewLine, orderAllByFirstAndLastName));
        }
        static void Main()
        {
            double minGrade = 0;
            double maxGrade = 100;

            double minSalary = 350;
            double maxSalary = 2500;

            double minWorkHoursPerDay = 4;
            double maxWorkHoursPerDay = 10;

            List <Student> students = new List <Student>();

            for (int i = 0; i < 10; i++)
            {
                Student currentStudent =
                    new Student(GetRandomName(), GetRandomName(), GetRandomNumber(minGrade, maxGrade));
                students.Add(currentStudent);
            }

            var sortedStudents = students
                                 .OrderBy(st => st.Grade)
                                 .ToList();

            // Print Students
            //Console.WriteLine("Students:");
            //foreach (Student student in sortedStudents)
            //{
            //    Console.WriteLine(student);
            //}
            //Console.WriteLine();

            List <Worker> workers = new List <Worker>();

            for (int i = 0; i < 10; i++)
            {
                Worker currentWorker =
                    new Worker(GetRandomName(), GetRandomName(), GetRandomNumber(minSalary, maxSalary), GetRandomNumber(minWorkHoursPerDay, maxWorkHoursPerDay));
                workers.Add(currentWorker);
            }

            var sortedWorkers = workers
                                .OrderByDescending(w => w.MoneyPerHour())
                                .ToList();

            // Print Workers
            //Console.WriteLine("Workers:");
            //foreach (Worker worker in sortedWorkers)
            //{
            //    Console.WriteLine(worker);
            //}
            //Console.WriteLine();

            var mergedPeople = new List <Human>();

            mergedPeople.AddRange(sortedStudents);
            mergedPeople.AddRange(sortedWorkers);

            var result = mergedPeople
                         .OrderBy(x => x.FirstName)
                         .ThenBy(x => x.LastName)
                         .ToList();

            // Print All
            //Console.WriteLine("Merged:");
            //foreach (Human human in result)
            //{
            //    Console.WriteLine(human);
            //}
        }
示例#6
0
        static void Main()
        {
            Student[] students = new Student[]
            {
                new Student("Ivan", "Petrov", 5.50),
                new Student("Alex", "Simeonov", 2.75),
                new Student("Iskra", "Radeva", 5.84),
                new Student("Dani", "Kostov", 6.00),
                new Student("Dimo", "Dimov", 2.25),
                new Student("Silviya", "Eneva", 4.33),
                new Student("Oleg", "Harizanov", 3.44),
                new Student("Nikolay", "Tzonkov", 2.87),
                new Student("Viki", "Nikolova", 4.92),
                new Student("Yanko", "Yankov", 5.12)
            };

            Worker[] workers = new Worker[]
               {
               new Worker("Bistra", "Pavlova", 258.25m, 10),
               new Worker("Veni", "Tasheva", 1020.80m, 8),
               new Worker("Daniel", "Elenov", 140.65m, 6),
               new Worker("Angel", "Angelov", 940.48m, 7),
               new Worker("Jivko", "Denchev", 680.58m, 9),
               new Worker("Rosi", "Yankova", 240m, 14),
               new Worker("Rumen", "Zaimov", 315.68m, 18),
               new Worker("Georgi", "Borukov", 110.80m, 2),
               new Worker("Oleg", "Pavlov", 500m, 5),
               new Worker("Vanya", "Vencheva", 190.38m, 12),
               };

            //sorting students by marks ascending using LINQ
            var sortStudents = from st in students
                               orderby st.Grade
                               select st;

            foreach (var st in sortStudents)
            {
                Console.WriteLine(st);
            }

            Console.WriteLine();

            //the same using extension methods
            var sortedStud = students.OrderBy(st => st.Grade);

            foreach (var st in sortedStud)
            {
                Console.WriteLine(st);
            }

            Console.WriteLine();
            //sorting workers by money per hour in descending order
            var sortWorkers = from wks in workers
                              orderby wks.MoneyPerHour() descending
                              select wks;

            foreach (var wrk in sortWorkers)
            {
                Console.WriteLine(wrk);
            }

            Console.WriteLine();
            //the same task using extension methods
            var sortedWorkers = workers.OrderByDescending(wrk => wrk.MoneyPerHour());

            foreach (var worker in sortedWorkers)
            {
                Console.WriteLine(worker);
            }

            Console.WriteLine();
            //merging the lists and sorting them by first name and last name
            var merged = students.Concat<Human>(workers).OrderBy(hm => hm.FirstName).ThenBy(hm => hm.LastName);

            foreach (var human in merged)
            {
                Console.WriteLine(human.FirstName +" "+ human.LastName);
            }
        }
示例#7
0
        static void Main(string[] args)
        {
            List <Worker> workersList = new List <Worker>();
            Worker        w1          = new Worker("George", "Peterson", 280, 10);

            workersList.Add(w1);
            Worker w2 = new Worker("John", "Hamilton", 310, 12);

            workersList.Add(w2);
            Worker w3 = new Worker("Dave", "Maynard", 290, 11);

            workersList.Add(w3);
            Worker w4 = new Worker("Lloyd", "Mathinson", 310, 12);

            workersList.Add(w4);
            Worker w5 = new Worker("Gary", "Robinson", 400, 13);

            workersList.Add(w5);
            Worker w6 = new Worker("Douglas", "Harbin", 330, 12);

            workersList.Add(w6);
            Worker w7 = new Worker("Kenneth", "Hart", 320, 11);

            workersList.Add(w7);
            Worker w8 = new Worker("Denis", "Remy", 310, 11);

            workersList.Add(w8);
            Worker w9 = new Worker("Ken", "Copeland", 300, 11);

            workersList.Add(w9);
            Worker w10 = new Worker("Marilyn", "Chambers", 300, 10);

            workersList.Add(w10);


            List <Student> studentsList = new List <Student>();
            Student        s1           = new Student("William", "Matthers", 8);

            studentsList.Add(s1);
            Student s2 = new Student("Dan", "Smith", 7);

            studentsList.Add(s2);
            Student s3 = new Student("Rebecca", "Crowley", 9);

            studentsList.Add(s3);
            Student s4 = new Student("Keith", "Workman", 10);

            studentsList.Add(s4);
            Student s5 = new Student("Adrianna", "Avila", 6);

            studentsList.Add(s5);
            Student s6 = new Student("Carl", "Rayburn", 5);

            studentsList.Add(s6);
            Student s7 = new Student("Roger", "Le", 7);

            studentsList.Add(s7);
            Student s8 = new Student("Marvin", "Daniels", 9);

            studentsList.Add(s8);
            Student s9 = new Student("George", "Moore", 10);

            studentsList.Add(s9);
            Student s10 = new Student("Anne", "Delaney", 8);

            studentsList.Add(s10);

            Console.WriteLine("The students are the following:");
            PrintAllTheStudents(studentsList);
            Console.WriteLine();

            Console.WriteLine("The workers are the following:");
            PrintAllTheWorkers(workersList);
            Console.WriteLine();

            Console.WriteLine("The list of the students ordered ascending by their grades is:");
            List <Student> orderedStudentsList = studentsList.OrderBy(student => student.Grade).ToList();

            PrintAllTheStudents(orderedStudentsList);
            Console.WriteLine();

            Console.WriteLine("The list of the workers ordered descending by their income per hour is:");
            List <Worker> orderedWorkersList = workersList.OrderByDescending(worker => worker.MoneyPerHour()).ToList();

            PrintAllTheWorkers(orderedWorkersList);
            Console.WriteLine();

            Console.WriteLine("The list of the students and the list of the workers merged is:");
            List <Human> humansList = studentsList.Cast <Human>().Concat(workersList).ToList();

            PrintAllTheHumans(humansList);
            Console.WriteLine();

            Console.WriteLine("The list of the students and the list of the workers merged and sorted by name is:");
            List <Human> orderedHumansList = humansList.OrderBy(human => human.FirstName).ToList();

            PrintAllTheHumans(orderedHumansList);
            Console.WriteLine();
        }
        static void Main()
        {
            Student gosho = new Student("Gosho", "Goshov", 4);
            Student pesho = new Student("Pesho", "Peshov", 5);
            Student minka = new Student("Minka", "Goshova", 6);
            Student mariika = new Student("Mariq", "Toshova", 5);
            Student tosho = new Student("Todor", "Todorov", 6);
            Student ivancho = new Student("Ivan", "Kalpazanov", 8);
            Student miro = new Student("Miroslav", "Goshov", 3);
            Student stamat = new Student("Stamat", "Stamatov", 4);
            Student vasil = new Student("Vasil", "Petrov", 4);
            Student stoqn = new Student("Stoqn", "Hristov", 4);

            List<Student> studentsList = new List<Student>();
            studentsList.Add(gosho);
            studentsList.Add(pesho);
            studentsList.Add(minka);
            studentsList.Add(mariika);
            studentsList.Add(tosho);
            studentsList.Add(ivancho);
            studentsList.Add(miro);
            studentsList.Add(stamat);
            studentsList.Add(vasil);
            studentsList.Add(stoqn);

            var sortedStudentList = studentsList.OrderBy(s => s.Grade);

            Console.WriteLine("Students:");
            Console.WriteLine();

            foreach (var item in sortedStudentList)
            {
                Console.WriteLine("{0,-10} - grade: {1}", item.FirstName, item.Grade);
            }

            Console.WriteLine(new string('_', 20));
            Console.WriteLine();

            Worker filip = new Worker("Kristian", "Tumbev", 50, 8);
            Worker kolio = new Worker("Nikola", "Nikolov", 20, 4);
            Worker hristo = new Worker("Hristo", "Iordanov", 100, 8);
            Worker dancho = new Worker("Iordan", "Vylkov", 100, 8);
            Worker sasho = new Worker("Aleksandyr", "Uzunov", 100, 2);
            Worker georgi = new Worker("Georgi", "Georgiev", 60, 8);
            Worker ceco = new Worker("Cvetomir", "Nachev", 100, 8);
            Worker presiqn = new Worker("Presiqn", "Aleksandrov", 100, 4);
            Worker detelin = new Worker("Detelin", "Tumbev", 80, 8);
            Worker cveta = new Worker("Cvetomira", "Nacheva", 150, 8);

            List<Worker> listWorkers = new List<Worker>();

            listWorkers.Add(filip);
            listWorkers.Add(kolio);
            listWorkers.Add(hristo);
            listWorkers.Add(dancho);
            listWorkers.Add(sasho);
            listWorkers.Add(georgi);
            listWorkers.Add(ceco);
            listWorkers.Add(presiqn);
            listWorkers.Add(detelin);
            listWorkers.Add(cveta);

            var sortedListWorkers = listWorkers.OrderByDescending(w => w.MoneyPerHour());

            Console.WriteLine("Workers:");
            Console.WriteLine();

            foreach (var item in sortedListWorkers)
            {
                Console.WriteLine("{0,-10} - money per hour: {1}", item.FirstName, item.MoneyPerHour());
            }

            var both = new List<Human>();

            both.AddRange(sortedStudentList);
            both.AddRange(sortedListWorkers);

            var sortedBoth = both.OrderBy(b => b.FirstName).ThenBy(b => b.LastName);

            Console.WriteLine();
            Console.WriteLine(new string('-', 10));
            Console.WriteLine();
            Console.WriteLine("Students and workers:");
            Console.WriteLine();

            foreach (var item in sortedBoth)
            {
                Console.WriteLine("{0,-10} {1}", item.FirstName, item.LastName);
            }
        }