Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace LinqExercise
- {
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- public class LinqExercise
- {
- public static void Main()
- {
- const string FilePath = @"../../Students-data.txt";
- List<string> result = new List<string>();
- StreamReader readData = new StreamReader(FilePath);
- string headerLine = readData.ReadLine();
- string line;
- while ((line = readData.ReadLine()) != null)
- {
- result.Add(line);
- }
- List<Student> students = new List<Student>();
- foreach (string data in result)
- {
- string[] tokens = data.Split();
- int id = int.Parse(tokens[0]);
- string firstName = tokens[1];
- string lastName = tokens[2];
- string email = tokens[3];
- string gender = tokens[4];
- string studentType = tokens[5];
- int examResult = int.Parse(tokens[6]);
- int homeworksSent = int.Parse(tokens[7]);
- int homeworksEvaluated = int.Parse(tokens[8]);
- double teamworkScore = double.Parse(tokens[9]);
- int attendancesCount = int.Parse(tokens[10]);
- double bonus = double.Parse(tokens[11]);
- students.Add(new Student(
- id,
- firstName,
- lastName,
- email,
- gender,
- studentType,
- examResult,
- homeworksSent,
- homeworksEvaluated,
- teamworkScore,
- attendancesCount,
- bonus));
- }
- // TODO write LINQ queries here
- //1
- //var maleStudents = students
- // .Where(e => e.Gender == "Male");
- //foreach (var student in maleStudents)
- //{
- // Console.WriteLine(student.FirstName + " " + student.LastName + " - " + student.Gender);
- //}
- //2
- //var studentsFirstNameA = students
- // .Where(e => e.FirstName.StartsWith("A"));
- //foreach (var student in studentsFirstNameA)
- //{
- // Console.WriteLine(student.FirstName + " " + student.LastName);
- //}
- //3
- //var studentsOn = students
- // .Where(e => e.StudentType == "Online" && e.ExamResult >= 350);
- //foreach (var student in studentsOn)
- //{
- // Console.WriteLine("{0} {1} ({2}) - {3}",
- // student.FirstName, student.LastName, student.StudentType, student.ExamResult);
- //}
- //4
- //var studentsOn = students
- // .Where(e => e.StudentType == "Online" && e.ExamResult >= 300)
- // .OrderByDescending(e => e.ExamResult);
- //foreach (var student in studentsOn)
- //{
- // Console.WriteLine("{0} {1} ({2}) - {3}",
- // student.FirstName, student.LastName, student.StudentType, student.ExamResult);
- //}
- //5
- //var studentsHomework = students
- // .Where(e => e.HomeworksSent == 0)
- // .OrderBy(e => e.FirstName).ThenBy(e => e.LastName);
- //foreach (var student in studentsHomework)
- //{
- // Console.WriteLine("{0} {1} ({2} homeworks sent)",
- // student.FirstName, student.LastName, student.HomeworksSent);
- //}
- //6
- //var studentsOnSite = students
- // .Where(e => e.StudentType == "Onsite")
- // .Select(e => e.Email);
- //foreach (var studentEmail in studentsOnSite)
- //{
- // Console.WriteLine(studentEmail);
- //}
- //7
- //var studentsOnSite = students
- // .Where(e => e.AttendancesCount < 5)
- // .Select(e => new
- // {
- // e.ExamResult,
- // e.AttendancesCount
- // });
- //foreach (var student in studentsOnSite)
- //{
- // Console.WriteLine("Result: {0}, attendances: {1}", student.ExamResult, student.AttendancesCount);
- //}
- //8
- //var studentsBonus = students
- // .Where(e => e.Bonus >= 4)
- // .Count();
- //Console.WriteLine(studentsBonus);
- //9
- var avgExamStudents = students
- .Select(d => new
- {
- OnlineAverage = students.Where(s => s.StudentType == "Online").Select(s => s.ExamResult).Average(),
- OnsiteAverage = students.Where(s => s.StudentType == "Onsite").Select(s => s.ExamResult).Average()
- })
- .Distinct();
- foreach (var student in avgExamStudents)
- {
- Console.WriteLine("Online average: " + student.OnlineAverage);
- Console.WriteLine("Onsite average: " + student.OnsiteAverage);
- }
- //10
- var studentsTWScore = students
- .Select(d => new
- {
- Max = students
- .Where(s => s.TeamworkScore == students
- .Select(t => t.TeamworkScore).Max())
- .Count()
- })
- .Distinct();
- foreach (var student in studentsTWScore)
- {
- Console.WriteLine(student.Max);
- }
- //11
- //var groupStudents = students
- // .GroupBy(s => s.FirstName.First())
- // .OrderBy(g => g.Key);
- ////I set this because console window limits the lines of output and you see only part of the output, you need to manually modify it
- ////the other way is in console window -> properties -> layout -> screen buffer size -> height
- //Console.BufferHeight = 1021;
- //foreach (var groupName in groupStudents)
- //{
- // Console.WriteLine(groupName.Key);
- // foreach (var student in groupName)
- // {
- // Console.WriteLine(" {0} {1}", student.FirstName, student.LastName);
- // }
- //}
- //12
- var groupByType = students
- .OrderByDescending(s => s.ExamResult)
- .GroupBy(s => s.StudentType);
- Console.BufferHeight = 1021;
- foreach (var group in groupByType)
- {
- Console.WriteLine(group.Key);
- foreach (var student in group)
- {
- Console.WriteLine(" {0} {1} - {2}",
- student.FirstName, student.LastName, student.ExamResult);
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement