Created
September 20, 2018 22:17
-
-
Save PhiHuyHoang/ca643ff43323bf85c2c663ad34e73a9a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class ConsoleExtension | |
{ | |
public static void ToConsole<T>(this IEnumerable<T> s, string header) | |
{ | |
Console.WriteLine($"*********BEGIN {header}**************"); | |
foreach (T item in s) | |
{ | |
Console.WriteLine(item); | |
} | |
Console.WriteLine($"*********END {header}**************"); | |
Console.ReadLine(); | |
} | |
} | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
Console.InputEncoding = Encoding.Unicode; | |
Console.OutputEncoding = Encoding.Unicode; | |
XDocument doc = XDocument.Load("http://users.nik.uni-obuda.hu/prog3/_data/people.xml"); | |
var AllPeople = doc.Descendants("person").Select(person => person.Element("name")?.Value); | |
AllPeople.ToConsole("ALL PEOPLE NAME"); | |
//Q1 - Determine the number of workers who work in the AII department! | |
var q1 = doc.Descendants("person").Where(person => person.Element("dept")?.Value == "Alkalmazott Informatikai Intézet").Count(); | |
Console.WriteLine($"QUERY 1\nNumber of worker in 'All Department': {q1}"); | |
Console.ReadLine(); | |
//Q2 - Display those who live in the third floor in a “paginated” list, we should press ENTER after every 15 elements | |
int current = 0, pagesize = 15; | |
while (current < q1) | |
{ | |
var q2 = doc.Descendants("person").Where(person => person.Element("room").Value.StartsWith("BA.3")).Select(person => | |
person.Element("name")?.Value).Skip(current).Take(15); | |
q2.ToConsole("QUERY 2"); | |
current += pagesize; | |
} | |
//Q3 - Who have the shortest or the longest names? | |
var q3 = from person in doc.Descendants("person") | |
let minNameLength = doc.Descendants("person").Min(x => x.Element("name")?.Value.Length) | |
let maxNameLength = doc.Descendants("person").Max(x => x.Element("name")?.Value.Length) | |
where person.Element("name")?.Value.Length == minNameLength || | |
person.Element("name")?.Value.Length == maxNameLength | |
select new { Name = person.Element("name")?.Value, Length = person.Element("name")?.Value.Length }; | |
q3.ToConsole("QUERY 3"); | |
// Q4 - Determine the number of workers for every department! | |
var q4 = from person in doc.Descendants("person") | |
group person by person.Element("dept")?.Value into g | |
select new { Dept = g.Key, NumberOfWorker = g.Count() }; | |
q4.ToConsole("QUERY 4"); | |
// Q5 - Determine the biggest department! | |
var q5 = q4.OrderByDescending(person => person.NumberOfWorker).FirstOrDefault(); | |
Console.WriteLine($"QUERY 5 \n{q5}"); | |
Console.ReadLine(); | |
//Q6 - List the workers for the biggest department! | |
var q6 = doc.Descendants("person").Where(person => person.Element("dept")?.Value == q5.Dept).Select(person => person.Element("name")?.Value); | |
q6.ToConsole("QUERY 6"); | |
//Q7 - List the people who work in the third biggest department ordered by their room number in descending order! | |
var thirdBiggestDept = q4.OrderByDescending(person => person.NumberOfWorker).Skip(2).FirstOrDefault(); | |
var q7 = doc.Descendants("person").Where(person => person.Element("dept")?.Value == thirdBiggestDept.Dept).Select(person => new { Name = person.Element("name")?.Value, Room = person.Element("room")?.Value }).OrderByDescending(person => person.Room); | |
q7.ToConsole("QUERY 7"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment