Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 32 additions & 3 deletions lambda_practice/Program.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using lambda_practice.data;
using Microsoft.EntityFrameworkCore;
using System;
using System.Linq;

namespace lambda_practice
{
Expand All @@ -22,25 +23,53 @@ static void Main(string[] args)
using (var context = new DatabaseContext(options))
{
//You can erase the following 3 lines if needed
context.Cities.ForEachAsync(c => Console.WriteLine(c.Name));
context.Departments.ForEachAsync(d => Console.WriteLine(d.Name));
context.Employees.ForEachAsync(e => Console.WriteLine($"{e.FirstName} {e.LastName}"));
//context.Cities.ForEachAsync(c => Console.WriteLine(c.Name));
//context.Departments.ForEachAsync(d => Console.WriteLine(d.Name));
//context.Employees.ForEachAsync(e => Console.WriteLine($"{e.FirstName} {e.LastName}"));

//1. List all employees whose departament has an office in Chihuahua
var query = context.Employees.Where(e => e.Department.Cities.Where(c => c.Name == "Chihuahua").Count() > 0);

Console.WriteLine("List all employees whose departament has an office in Chihuahua\n\n");

query.ForEachAsync(e => Console.WriteLine($"{e.FirstName} {e.LastName}"));


//2. List all departaments and the number of employees that belong to each department.
var secondQuery = context.Departments.Select(d => new {
Name = d.Name,
Count = context.Employees.Where(e => e.Department == d).Count()
});

Console.WriteLine("\n\n2. List all departaments and the number of employees that belong to each department.\n\n");

secondQuery.ForEachAsync(d => Console.WriteLine($"{d.Name} {d.Count}"));


//3. List all remote employees. That is all employees whose living city is not the same one as their department's.
var thirdQuery = context.Employees.Where(e => e.Department.Cities.Where(c => c.Name == e.City.Name).Count() == 0);

Console.WriteLine("\n\n3. List all remote employees. That is all employees whose living city is not the same one as their department's.\n\n");

thirdQuery.ForEachAsync(e => Console.WriteLine($"{e.FirstName} {e.LastName}"));


//4. List all employees whose hire aniversary is next month.
var fourthQuery = context.Employees.Where(e => e.HireDate.Month == DateTime.Now.AddMonths(1).Month);
Console.WriteLine("\n\n4. List all employees whose hire aniversary is next month.\n\n");

fourthQuery.ForEachAsync(e => Console.WriteLine($"{e.FirstName} {e.LastName}"));


//5. List all 12 months of the year and the number of employees hired on each month.
var fifthQuery = context.Employees.GroupBy(e => e.HireDate.Month);

Console.WriteLine("\n\n5. List all 12 months of the year and the number of employees hired on each month.\n\n");

fifthQuery.ForEachAsync(m => Console.WriteLine($"{System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(m.Key)}: {m.Count()}"));
}

Console.WriteLine("\nDone.");
Console.ReadLine();
}
}
Expand Down