Пример #1
0
        public static void Run()
        {
            const string student_names_file = "Names.txt";

            var file_with_names = new FileInfo(student_names_file);

            var rnd     = new Random();
            var dekanat = new Dekanat();

            for (var i = 0; i < 10; i++)
            {
                dekanat.Add(new StudentsGroup {
                    Name = $"Группа {i + 1}"
                });
            }

            foreach (var line in file_with_names.GetLines())
            {
                if (string.IsNullOrWhiteSpace(line))
                {
                    continue;
                }

                var components = line.Split(' ');
                if (components.Length < 3)
                {
                    continue;
                }

                var student = new Student
                {
                    LastName   = components[0],
                    FirstName  = components[1],
                    Patronimyc = components[2],
                    Ratings    = rnd.GetRandomIntValues(20, 3, 6).ToList()
                };

                dekanat.Add(student);
            }


            //IEnumerable<Student> students_enum = dekanat;
            //IQueryable<Student> student_query = students_enum.AsQueryable();

            //foreach (var i in Enumerable.Range(0, 100))
            //{

            //}

            //for (var i = 0; i < 100; i++)
            //{

            //}

            //var simple_studetns = Enumerable.Range(1, 100)
            //   .Select(i => new Student { FirstName = $"Student {i}" })
            //   //.ToArray()
            //    ;
            //foreach (var student in simple_studetns)
            //    Console.WriteLine(student);

            var best_students = dekanat.Where(student => student.AverageRating >= 4);
            var last_students = dekanat.Where(student => student.AverageRating < 4);

            foreach (var best_student in best_students)
            {
                Console.WriteLine(best_student);
            }

            //Console.ReadLine();

            foreach (var last_student in last_students)
            {
                Console.WriteLine(last_student);
            }

            var best_count = best_students.Count();
            var last_count = last_students.Count();

            //var names_lengths = file_with_names.GetLines()
            //   .Select(str => str.Split(' '))
            //   .Select(strs => new KeyValuePair<string, int>(strs[1], strs[1].Length))
            //   .Where(v => v.Value > 4)
            //   .OrderBy(v => v.Value)
            //   .Sum(v => v.Key.Length);

            IEnumerable <Student>       students = dekanat;
            IEnumerable <StudentsGroup> groups   = dekanat.Groups;

            var students_groups = students.Join(
                groups,
                stud => stud.GroupId,
                group => group.Id,
                (stud, group) => new { Student = stud, Group = group })
                                  .Where(s => s.Student.AverageRating > 4);

            foreach (var students_group in students_groups)
            {
                Console.WriteLine("{0} == {1}", students_group.Student, students_group.Group);
            }

            var students_groups_dict = students_groups.GroupBy(g => g.Group.Name)
                                       .ToDictionary(g => g.Key, g => g.Select(v => v.Student.LastName).ToArray());
        }
Пример #2
0
        public static void Run()
        {
            const string student_name   = "Names.txt";
            var          file_with_name = new FileInfo(student_name);

            var rnd     = new Random();
            var dekanat = new Dekanat();

            for (var i = 0; i < 10; i++)
            {
                dekanat.Add(new StudentsGroup {
                    Name = $"Group {i + 1}"
                });
            }

            foreach (var line in file_with_name.GetLines())
            {
                if (string.IsNullOrEmpty(line))
                {
                    continue;
                }
                var components = line.Split(' ');
                if (components.Length < 2)
                {
                    continue;
                }

                var student = new Student
                {
                    LastName  = components[0],
                    FirstName = components[1],
                    Ratings   = rnd.GetRandomIntValues(20, 3, 6).ToList()
                };
                dekanat.Add(student);
            }

            //IEnumerable<Student> students_enum = dekanat;
            //IQueryable<Student> students_query = students_enum.AsQueryable();

            //var sStudents = Enumerable.Range(1, 100)
            //    .Select(i => new Student
            //    {
            //        FirstName = $"Student {i}"
            //    });
            //foreach(var student in sStudents)
            //{
            //    Console.WriteLine(student);
            //}

            var bStudents = dekanat.Where(s => s.AvgRating > 4);

            foreach (var bs in bStudents)
            {
                Console.WriteLine(bs);
            }
            var bCount = bStudents.Count();

            bStudents = dekanat.Where(s => s.AvgRating < 4);
            foreach (var bs in bStudents)
            {
                Console.WriteLine(bs);
            }
            var lCount = bStudents.Count();

            Console.WriteLine("{0} {1}", bCount, lCount);

            //var nameLengths = file_with_name.GetLines()
            //    .Select(str => str.Split(' '))
            //    .Select(strs => new KeyValuePair<string , int>(strs[1], strs[1].Length))
            //    .Where(v => v.Value > 4)
            //    .OrderBy(v => v.Value)
            //    .ToArray();

            IEnumerable <Student>       students = dekanat;
            IEnumerable <StudentsGroup> groups   = dekanat.Groups;

            var students_groups = students
                                  .Join(
                groups,
                stud => stud.GroupId,
                grp => grp.Id,
                (stud, grp) => new { Student = stud, Group = grp }
                )
                                  .Where(s => s.Student.AvgRating > 4);

            foreach (var stdGrp in students_groups)
            {
                Console.WriteLine("{0} == {1}", stdGrp.Student, stdGrp.Group);
            }

            var students_group_dict = students_groups
                                      .GroupBy(
                g => g.Group.Name
                )
                                      .ToDictionary(
                g => g.Key,
                g => g.Select(v => v.Student.LastName));
        }
Пример #3
0
        public static void Run()
        {
            const string student_names_file = "names.txt";

            var file_with_names = new FileInfo(student_names_file);

            var rnd     = new Random();
            var dekanat = new Dekanat();

            for (int i = 0; i < 10; i++)
            {
                dekanat.Add(new StudentsGroup {
                    Name = $"Группа {i + 1}"
                });
            }
            foreach (var line in file_with_names.GetLines())
            {
                if (string.IsNullOrWhiteSpace(line))
                {
                    continue;
                }
                var components = line.Split(' ');
                if (components.Length < 3)
                {
                    continue;
                }
                var student = new Student
                {
                    LastName   = components[0],
                    FirstName  = components[1],
                    Patronimyc = components[2],
                    Rating     = rnd.GetRandomIntValues(10, 3, 6).ToList()
                };

                dekanat.Add(student);
            }

            #region Enumerable.Range(1, 100)

            /*
             * //  перечисления студентов. можно работать с перечислениями.
             * IEnumerable<Student> students_enum = dekanat;
             * //   поддерживает запросы к себе. Оба интерфейсы.
             * IQueryable<Student> students_query = students_enum.AsQueryable();
             *
             * //  равносильные записи. Но основное предназначение Enumerable.Range() другое
             * foreach (var i in Enumerable.Range(0, 100))
             * {
             * }
             * for (int i = 0; i < 100; i++)
             * {
             * }
             *
             * var simple_students = Enumerable.Range(1, 100)
             *  .Select(i => new Student { FirstName = $"Student {i}" })
             *  .ToArray() ;   //  получили объект перечисления студентов
             *
             * foreach (var students in simple_students)
             * {
             *  Console.WriteLine(students);
             * }
             */
            #endregion

            var best_students  = dekanat.Where(student => student.AverageRating > 4);
            var loser_students = dekanat.Where(student => student.AverageRating < 4);
            foreach (var best_student in best_students)
            {
                Console.WriteLine(best_student);
            }
            foreach (var losers in loser_students)
            {
                Console.WriteLine(losers);
            }

            //  подсчет хорошистов и лузеров
            var best_count  = best_students.Count();
            var loser_count = loser_students.Count();

            #region .Select Where OrderBy Sum

            /*
             * //  var автоматическая типизация
             * var names_length = file_with_names.GetLines()
             *  .Select(str => str.Split(' '))  //  разделили по пробелу
             *  .Select(strs => new KeyValuePair<string, int>(strs[1], strs[1].Length)) //  выбрали второй элемент (имя)
             *  .Where(v => v.Value > 4)    //  выбрали те что больше 4 фильтрация
             *  .OrderBy(v => v.Value)  //  отсортировали
             *  .Sum(v => v.Key.Length);
             */
            #endregion

            IEnumerable <Student>       students = dekanat;
            IEnumerable <StudentsGroup> groups   = dekanat.Groups;

            var students_groups = dekanat.Join(
                groups,
                stud => stud.GroupId,
                group => group.Id,
                (stud, group) => new { Student = stud, Group = group })
                                  .Where(s => s.Student.AverageRating > 4);

            foreach (var students_group in students_groups)
            {
                Console.WriteLine($"{students_group.Student} == {students_group.Group}");
            }

            var students_group_dict = students_groups.GroupBy(g => g.Group.Name)
                                      .ToDictionary(g => g.Key, g => g.Select(v => v.Student.LastName).ToArray());
        }
        public static void Run()
        {
            const string student_names_file = "Names.txt";

            var file_with_names = new FileInfo(student_names_file);

            var rnd     = new Random();
            var dekanat = new Dekanat();

            for (var i = 0; i < 10; i++)
            {
                dekanat.Add(new StudentsGroup {
                    Name = $"Группа {i + 1}"
                });
            }
            foreach (var line in file_with_names.GetLines())
            {
                if (string.IsNullOrWhiteSpace(line))
                {
                    continue;
                }
                var components = line.Split(' ');
                if (components.Length < 3)
                {
                    continue;
                }

                var student = new Student
                {
                    LastName   = components[0],
                    FirstName  = components[1],
                    Patronymic = components[2],
                    Ratings    = rnd.GetRandomIntValues(20, 3, 5).ToList()
                };
                dekanat.Add(student);
            }
            //IEnumerable<Student> students_enum = dekanat;
            //IQueryable<Student> student_query = students_enum.AsQueryable();

            //foreach(var i in Enumerable.Range(0, 100))
            //{

            //}
            //for(var i = 0; i < 100; i++)
            //{

            //}

            //var simple_students = Enumerable.Range(1, 100)
            //    .Select(i => new Student { FirstName = $"Student {i}" });


            //foreach (var student in simple_students)
            //    Console.WriteLine(student);

            var best_students = dekanat.Where(student => student.AverageRating >= 4);

            var last_students = dekanat.Where(student => student.AverageRating < 4);


            foreach (var best_student in best_students)
            {
                Console.WriteLine(best_students);
            }
            foreach (var last_student in last_students)
            {
                Console.WriteLine(best_students);
            }

            var best_count = best_students.Count();
            var last_count = last_students.Count();

            var names_lengths = file_with_names.GetLines()
                                .Select(str => str.Split(' '))
                                .Select(strs => new KeyValuePair <string, int>(strs[1], strs[1].Length))
                                .Where(v => v.Value > 4)
                                .OrderBy(v => v.Value)
                                .ToArray();
        }