Last active
September 20, 2018 22:43
-
-
Save PhiHuyHoang/be3fe77a1fc81c0ec6028787bef64eff 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"); | |
} | |
} |
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
class Person | |
{ | |
public string Name { get; set; } | |
public string Email { get; set; } | |
public string Dept { get; set; } | |
public string Rank { get; set; } | |
public string Phone { get; set; } | |
public string Room { get; set; } | |
public static List<Person> Persons(string url) | |
{ | |
XDocument document = XDocument.Load(url); | |
return document.Descendants("person").Select(node => new Person() | |
{ | |
Name = node.Element("name")?.Value, | |
Email = node.Element("email")?.Value, | |
Dept = node.Element("dept")?.Value, | |
Rank = node.Element("rank")?.Value, | |
Phone = node.Element("phone")?.Value, | |
Room = node.Element("room")?.Value | |
}).ToList(); | |
} | |
} | |
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; | |
List<Person> People = Person.Persons("http://users.nik.uni-obuda.hu/prog3/_data/people.xml"); | |
People.Select(p => p.Name).ToConsole("ALL PEOPLE NAME"); | |
//Q1 - Determine the number of workers who work in the AII department! | |
var q1 = People.Where(person => person.Dept == "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 = People.Where(person => person.Room.StartsWith("BA.3")).Select(person => person.Name).Skip(current). | |
Take(pagesize); | |
q2.ToConsole("QUERY 2"); | |
current += pagesize; | |
} | |
//Q3 - Who have the shortest or the longest names? | |
var q3 = from person in People | |
let maxNameLength = People.Max(x => x.Name.Length) | |
let minNameLength = People.Min(x => x.Name.Length) | |
where person.Name.Length == minNameLength || | |
person.Name.Length == maxNameLength | |
select new { Name = person.Name, Length = person.Name.Length }; | |
q3.ToConsole("QUERY 3"); | |
//Q4 - Determine the number of workers for every department! | |
var q4 = from person in People | |
group person by person.Dept into g | |
select new { Dept = g.Key, NumberOfWorker = g.Count() }; | |
q4.ToConsole("QUERY 4"); | |
//Q5 - Determine the biggest department! | |
var q5 = q4.OrderByDescending(x => x.NumberOfWorker).FirstOrDefault(); | |
Console.WriteLine($"QUERY 5\nBiggest Department: {q5}"); | |
Console.ReadLine(); | |
//Q6 - List the workers for the biggest department! | |
var q6 = People.Where(person => person.Dept == q5.Dept).Select(person => person.Name); | |
q6.ToConsole("QUERY 6"); | |
//Q7 - List the people who work in the third biggest department ordered by their room number in descending order! | |
var thirdBiggest = q4.OrderByDescending(x => x.NumberOfWorker).Skip(2).FirstOrDefault(); | |
var q7 = People.Where(person => person.Dept == thirdBiggest.Dept).Select(person => new { person.Name, person.Room }).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