static void Main() { Console.WriteLine("Print all students"); for (int i = 1; i <= sampleStudents.Count; i++) { Console.WriteLine("Student " + i + ":\n"); Console.WriteLine(sampleStudents[i - 1]); Console.WriteLine(); } //problem 9 // Select only the students from group 2 var studentsFromGroup2 = from student in sampleStudents where student.GroupNumber == 2 select student; // Order the students from group 2 by first name var orderedStudentsFromGroup2 = from student in studentsFromGroup2 orderby student.FirstName select student; Console.WriteLine("Students from group 2, ordered by first name (using LINQ):\n"); foreach (Student student in orderedStudentsFromGroup2) { Console.WriteLine(student); Console.WriteLine(); } Console.WriteLine("Students from group 2, ordered by first name (using extension methods):\n"); var orderedStudents2 = sampleStudents.Where(x => x.GroupNumber == 2).OrderBy(x => x.FirstName); foreach (Student student in orderedStudents2) { Console.WriteLine(student); Console.WriteLine(); } // problem 11: extract all students w/ email in abv.bg using LINQ var studentsWithEmailsInAbv = from student in sampleStudents where student.Email.Contains("@abv.bg") select student; Console.WriteLine("Students with emails in abv.bg:\n"); foreach (Student student in studentsWithEmailsInAbv) { Console.WriteLine(student); Console.WriteLine(); } // problem 12: Extract all students w/ phones in Sofia, using LINQ var studentsWithPhonesInSofia = from student in sampleStudents where student.Phone.StartsWith("02") || student.Phone.StartsWith("+3592") select student; Console.WriteLine("Students with phones in Sofia:\n"); foreach (Student student in studentsWithPhonesInSofia) { Console.WriteLine(student); Console.WriteLine(); } // problem 13: Select all students that have at least one mark Excellent (6) // into a new anonymous class that has properties – FullName and Marks. var studentsWithExcellentMark = from student in sampleStudents where student.Marks.Contains((int)Marks.Excellent) select new { FullName = student.FirstName + " " + student.LastName, MarksList = student.Marks }; Console.WriteLine("Students that have at least one excellent mark, each in new anonymous class with " + "properties Fullname and MarksList:\n"); foreach (var student in studentsWithExcellentMark) { Console.WriteLine("Full name: {0}", student.FullName); Console.WriteLine("Marks: {0}", string.Join(", ", student.MarksList)); Console.WriteLine(); } // problem 14: Write down a similar program that extracts the students with exactly two marks "2". var studentsWithTwoTwos = sampleStudents.Where(x => x.Marks.FindAll(y => y == 2).Count == 2). Select(x => new { FullName = x.FirstName + " " + x.LastName, MarksList = x.Marks }); Console.WriteLine("Students that have exactly two poor marks, each in new anonymous class with " + "properties Fullname and MarksList:\n"); foreach (var student in studentsWithTwoTwos) { Console.WriteLine("Full name: {0}", student.FullName); Console.WriteLine("Marks: {0}", string.Join(", ", student.MarksList)); Console.WriteLine(); } // problem 15: Extract all Marks of the students that enrolled in 2006. // (The students from 2006 have 06 as their 5-th and 6-th digit in the FN). var studentsFrom2006 = sampleStudents.Where(x => x.FacultyNumber[4] == '0' && x.FacultyNumber[5] == '6'); var allMarksFrom2006 = new List<int>(); foreach (var student in studentsFrom2006) { allMarksFrom2006.AddRange(student.Marks); } Console.WriteLine("All marks of the students, enrolled in 2006:\n{0}", string.Join(", ", allMarksFrom2006)); Console.WriteLine(); // problem 16: Group group1 = new Group(1, "Developmental evolutionary biology"); Group group2 = new Group(2, "Science"); Group group3 = new Group(3, "Computer Science"); List<Group> groups = new List<Group> { group1, group2, group3 }; var studentsFromEvoDevo = from someGroup in groups where someGroup.GroupNumber == 1 join student in sampleStudents on someGroup.GroupNumber equals student.GroupNumber select new { Name = student.FirstName + " " + student.LastName, Department = someGroup.DepartmentName }; Console.WriteLine("All students from evo-devo, extracted as new anonymous classes," + " containing properties Name and Department:"); foreach (var student in studentsFromEvoDevo) { Console.WriteLine(student); } }
public static void Main() { List<Student> studentsList = new List<Student> { new Student("Gosho", "Petkov", "042000", "02203050", "*****@*****.**", new List<int> { 2, 3, 4 }, 2), new Student("Pesho", "Hubavecov", "042006", "25503250", "*****@*****.**", new List<int> { 2, 2, 2 }, 1), new Student("Gocho", "Gochev", "042001", "02202220", "*****@*****.**", new List<int> { 6, 6, 6 }, 2), new Student("Mmmm", "Aaaaa", "012006", "202210", "*****@*****.**", new List<int> { 6, 2, 2 }, 3), new Student("Aaaa", "Aaaaa", "012007", "20221550", "*****@*****.**", new List<int> { }, 3), new Student("bbb", "bb", "012007", "0220221550", "*****@*****.**", new List<int> { }, 2), new Student("ccc", "wwww", "012006", "0220221550", "*****@*****.**", new List<int> { 2, 2, 3 }, 2) }; Group groupMath = new Group(1, "Mathematics"); Group groupPhilosophy = new Group(2, "Philosophy"); Group groupLiterature = new Group(3, "Literature"); List<Group> listOfGroups = new List<Group> { new Group(1, "Mathematics"), new Group(2, "Philosophy"), new Group(3, "Literature") }; GroupMath(studentsList, listOfGroups); // P9 var group2LINQ = Group2LINQMethod(studentsList); Console.WriteLine("Sorted list using LINQ:"); Console.WriteLine(string.Join(", ", group2LINQ)); Console.WriteLine(); // P10 var group2Lambda = Group2LambdaMethod(studentsList); Console.WriteLine("Sorted list using lambda operations and extensions:"); Console.WriteLine(string.Join(", ", group2Lambda)); Console.WriteLine(); // P11 var emailGroup = EmailCheck(studentsList); Console.WriteLine("All students with abv.bg mail provider:"); Console.WriteLine(string.Join(", ", emailGroup)); Console.WriteLine(); // P12 var sofiaPhonesGroup = SofiaPhones(studentsList); Console.WriteLine("All students with Sofia phones:"); Console.WriteLine(string.Join(", ", sofiaPhonesGroup)); Console.WriteLine(); // P13 MarksSix(studentsList); // P14 MarksTwo(studentsList); // P15 List<int> extractedMarks = AllMarks(studentsList); Console.WriteLine("All marks of the students with FN 2006:"); Console.WriteLine(string.Join(", ", extractedMarks)); Console.WriteLine(); // P18 SortedByGroupsNumberLINQ(studentsList); // P19 SortedByGroupsNumberExtensionMethods(studentsList); }
static void Main() { //Using the class Student defined in 03-05.Students project. List<Student> classB = new List<Student>(); Student georgiStudent = new Student("Georgi", "Andonov", 20, "456009", 2, "8893434544", "*****@*****.**"); Student mihailStudent = new Student("Mihail", "Manov", 21, "123006", 1, "88933333", "*****@*****.**"); Student alexanderStudent = new Student("Alexander", "Primorski", 2, "466008", 2); Student ivoStudent = new Student("Ivo", "Antonov", 23, "555005", 3, "063 23434544", "*****@*****.**"); Student grishaStudent = new Student("Grisha", "Panchev", 20, "435005", 3, "02 44444", "*****@*****.**"); Student panchoStudent = new Student("Pancho", "Ganchev", 24, "231006", 2, "", "*****@*****.**"); classB.Add(georgiStudent); classB.Add(mihailStudent); classB.Add(alexanderStudent); classB.Add(ivoStudent); classB.Add(grishaStudent); classB.Add(panchoStudent); georgiStudent.Marks.Add(new Marks(4)); georgiStudent.Marks.Add(new Marks(6)); georgiStudent.Marks.Add(new Marks(6)); georgiStudent.Marks.Add(new Marks(5.5)); alexanderStudent.Marks.Add(new Marks(3.5)); ivoStudent.Marks.Add(new Marks(2)); ivoStudent.Marks.Add(new Marks(2)); grishaStudent.Marks.Add(new Marks(4.25)); grishaStudent.Marks.Add(new Marks(5.60)); panchoStudent.Marks.Add(new Marks(6)); //Problem 9 - using Linq only PrintSeparator("Problem 9 - Student groups"); var studentsInGroup2 = from student in classB where student.GroupNumber == 2 orderby student.FirstName select student; foreach (var student in studentsInGroup2) { Console.WriteLine("Name: {0} - Group: {1}", student.FirstName, student.GroupNumber); } //Problem 10 - using Linq and Lambda PrintSeparator("Problem 10 - Student groups extensions"); var studentsInGroup = classB .Where(st => st.GroupNumber == 2) .OrderBy(st => st.FirstName) .ToList(); foreach (var student in studentsInGroup) { Console.WriteLine("Name: {0} - Group: {1}",student.FirstName, student.GroupNumber); } //Problem 11 - Extract students by email PrintSeparator("Problem 11 - Extract students by email"); var studentsWithABVEmai = classB .Where(st => st.Email.EndsWith("abv.bg")) .ToList(); foreach (var student in studentsWithABVEmai) { Console.WriteLine("Name: {0} - Email: {1}",student.FirstName, student.Email); } //Problem 12 - Extract students by phone PrintSeparator("Problem 12 - Extract students by phone"); var studentsPhoneInSofia = classB .Where(st => st.Tel.StartsWith("02")) .ToList(); foreach (var student in studentsPhoneInSofia) { Console.WriteLine("Name: {0} - Tel: {1}", student.FirstName, student.Tel); } //Problem 13 - Extract students by marks PrintSeparator("Problem 13 - Extract students by marks"); var excellentStudents = classB .Where(st => st.Marks.Any(m => m.Mark==6)) .Select(s => new {FullName = s.FirstName+" "+s.LastName, allMarks = s.PrintMarks()}) .ToList(); foreach (var student in excellentStudents) { Console.WriteLine("Name: {0} - Marks: {1}", student.FullName, student.allMarks); } //Problem 14 - Extract students with two marks PrintSeparator("Problem 14 - Extract students with two marks"); var studentsTwoMarks = classB .Where(st => st.Marks.Count(x => x.Mark==2)==2) .Select(s => new { FullName = s.FirstName + " " + s.LastName, allMarks = s.PrintMarks() }) .ToList(); foreach (var student in studentsTwoMarks) { Console.WriteLine("Name: {0} - Marks: {1}", student.FullName, student.allMarks); } //Problem 15 - Extract marks PrintSeparator("Problem 15 - Extract marks"); var studentsFrom2006 = classB .Where(st => st.FN.Substring(4,2)=="06") .ToList(); foreach (var student in studentsFrom2006) { Console.WriteLine("Name: {0} - FN: {1}", student.FirstName, student.FN); } //Problem 16* - Groups PrintSeparator("Problem 16* - Groups"); List<Group> departments = new List<Group>(); Group math = new Group(2, "Mathemathics"); Group eng = new Group(1, "English"); Group chem = new Group(3, "Chemistry"); departments.Add(math); departments.Add(eng); departments.Add(chem); var studentsMathemathics = from student in classB join dep in departments on student.GroupNumber equals dep.GroupNumber where dep.DepartmentName == "Mathemathics" select new { Fullname = student.FirstName + " " + student.LastName, dep.DepartmentName }; foreach (var person in studentsMathemathics) { Console.WriteLine("Student: {0} ,Department: {1}", person.Fullname, person.DepartmentName); } //Problem 18 - Grouped by GroupNumber with Linq PrintSeparator("Problem 18 - Grouped by GroupNumber with Linq"); var groupedStudents2 = from students in classB group students by students.GroupNumber; foreach (var group in groupedStudents2) { Console.WriteLine("Group {0}", group.Key); foreach (var student in group) { Console.WriteLine("Student: {0} {1}", student.FirstName, student.LastName); } } //Problem 19 - Grouped by GroupNumber with extension methods PrintSeparator("Problem 19 - Grouped by GroupNumber with extension methods"); var groupedStudents = classB .GroupBy(st => st.GroupNumber) .ToList(); foreach (var group in groupedStudents) { Console.WriteLine("Group {0}", group.Key); foreach (var student in group) { Console.WriteLine("Student: {0} {1}", student.FirstName, student.LastName); } } }