2024-02-26 09:32:12 -05:00
|
|
|
|
using System.Text;
|
2024-02-22 18:27:49 -05:00
|
|
|
|
|
|
|
|
|
namespace LinqActivity
|
|
|
|
|
{
|
2024-02-26 09:32:12 -05:00
|
|
|
|
internal class Program
|
2024-02-22 18:27:49 -05:00
|
|
|
|
{
|
2024-02-26 09:32:12 -05:00
|
|
|
|
private static void Main()
|
2024-02-22 18:27:49 -05:00
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("LINQ Activity - 2/23/2024");
|
2024-02-26 09:32:12 -05:00
|
|
|
|
Console.WriteLine("Name: Matthew Oros");
|
|
|
|
|
var patients = new List<Patient>
|
2024-02-22 18:27:49 -05:00
|
|
|
|
{
|
2024-02-26 09:32:12 -05:00
|
|
|
|
new("John", 25, 5, 8, 180, new DateTime(2022,2, 18)),
|
|
|
|
|
new("Jane", 30, 5, 5, 150, new DateTime(2023,1, 10)),
|
|
|
|
|
new("Joe", 28, 6, 0, 200, new DateTime(2020, 7, 23)),
|
|
|
|
|
new("Jill", 35, 5, 7, 160, new DateTime(2021, 3, 20)),
|
|
|
|
|
new("Jack", 40, 5, 10, 190, new DateTime(2022, 8, 1))
|
2024-02-22 18:27:49 -05:00
|
|
|
|
};
|
|
|
|
|
|
2024-02-26 09:32:12 -05:00
|
|
|
|
var transferPatients = new List<Patient>
|
2024-02-22 18:27:49 -05:00
|
|
|
|
{
|
2024-02-26 09:32:12 -05:00
|
|
|
|
new("Bobby", 35, 5, 8, 180, new DateTime(2022,2, 18)),
|
|
|
|
|
new("Jamison", 33, 5, 5, 150, new DateTime(2023,1, 10)),
|
|
|
|
|
new("Elijah", 48, 6, 0, 200, new DateTime(2020, 7, 23)),
|
|
|
|
|
new("Bill", 15, 5, 7, 160, new DateTime(2021, 3, 20)),
|
|
|
|
|
new("Jack", 40, 5, 10, 190, new DateTime(2022, 8, 1)),
|
|
|
|
|
new("Jack", 40, 5, 10, 190, new DateTime(2022, 8, 1))
|
2024-02-22 18:27:49 -05:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Console.WriteLine("\nAll Patients in the Patients list");
|
|
|
|
|
DisplayPatients(patients);
|
|
|
|
|
|
|
|
|
|
Console.WriteLine("\n1.) All Patients over 30 in the Patients list");
|
|
|
|
|
//1.) Write a LINQ query that returns all patients over the age of 30 in the "Patients" list
|
|
|
|
|
var test1 = AllPatientsOver30(patients);
|
|
|
|
|
DisplayPatients(test1);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//2.) Write a LINQ query that takes the patients in the Patients list, and returns a string of their names separated by a comma
|
|
|
|
|
Console.WriteLine("\n2.) All Patient Names");
|
|
|
|
|
Console.WriteLine(AllPatientNames(patients));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//3.) Write a LINQ query that returns the average weight of all patients in the Patients list
|
|
|
|
|
Console.WriteLine("\n3.) Average Weight of All Patients");
|
|
|
|
|
Console.WriteLine(AverageWeightOfAllPatients(patients));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//4.) Write a LINQ query that orders the list of patients in the Patients list by DateCreated and returns the most recent
|
|
|
|
|
Console.WriteLine("\n4.) Most Recent Patient Created");
|
|
|
|
|
var mostRecentPatient = MostRecentPatientCreated(patients);
|
|
|
|
|
Console.WriteLine(mostRecentPatient?.ToString() ?? "Patient is NULL");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//5.) Write a LINQ Query that concatenates the two lists of patients together
|
|
|
|
|
Console.WriteLine("\n5.) Concatenate Patient Lists");
|
|
|
|
|
var joinedPatientLists = ConcatPatientLists(patients, transferPatients);
|
|
|
|
|
DisplayPatients(joinedPatientLists);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//6.)Given the last of transferParentNames below, write a LINQ Query within DistinctTransferPatients that returns a list of distinct names
|
|
|
|
|
Console.WriteLine("\n6.) Distinct Transfer Patients");
|
|
|
|
|
var transferPatientNames = transferPatients.Select(p => p.Name).ToList();
|
|
|
|
|
var distinctTransferPatientsNames = DistinctTransferPatients(transferPatientNames);
|
2024-02-26 09:32:12 -05:00
|
|
|
|
if (distinctTransferPatientsNames != null)
|
2024-02-22 18:27:49 -05:00
|
|
|
|
Console.WriteLine(string.Join(", ", distinctTransferPatientsNames));
|
|
|
|
|
|
|
|
|
|
//7.) Write a LINQ Query that checks if the list of patients contains ANY patients with the name "Joe"
|
|
|
|
|
Console.WriteLine("\n7.) Contains Joe?");
|
|
|
|
|
Console.WriteLine(ContainsJoe(patients));
|
|
|
|
|
|
|
|
|
|
//8.) Write a LINQ Query that TAKEs the first 3 patients in the patient list
|
|
|
|
|
Console.WriteLine("\n8.) First 3 Patients");
|
|
|
|
|
var first3Patients = First3Patients(patients);
|
|
|
|
|
DisplayPatients(first3Patients);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}//END Main
|
|
|
|
|
|
2024-02-26 09:32:12 -05:00
|
|
|
|
public static void DisplayPatients(List<Patient>? patients)
|
2024-02-22 18:27:49 -05:00
|
|
|
|
{
|
|
|
|
|
if (patients == null)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("Patient List Provided is NULL :O");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
foreach (var patient in patients)
|
|
|
|
|
{
|
2024-02-26 09:32:12 -05:00
|
|
|
|
Console.WriteLine($"Name: {patient.Name}, Age: {patient.Age}, Height: {patient.HeightFeet}'{patient.HeightInches}\", Weight: {patient.Weight} lbs, DateCreated: {patient.DateCreated:MMddyyyy}.");
|
2024-02-22 18:27:49 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-22 18:29:51 -05:00
|
|
|
|
//1.) Write a LINQ query that returns all patients over the age of 30 in the "Patients" list
|
2024-02-26 09:32:12 -05:00
|
|
|
|
public static List<Patient>? AllPatientsOver30(List<Patient>? patients)
|
2024-02-22 18:27:49 -05:00
|
|
|
|
{
|
2024-02-26 09:32:12 -05:00
|
|
|
|
return patients?.Where(p => p.Age > 30).ToList();
|
2024-02-22 18:27:49 -05:00
|
|
|
|
}
|
|
|
|
|
|
2024-02-22 18:29:51 -05:00
|
|
|
|
//2.) Write a LINQ query that takes the patients in the Patients list, and returns a string of their names separated by a comma
|
2024-02-26 09:32:12 -05:00
|
|
|
|
public static string AllPatientNames(List<Patient>? patients)
|
2024-02-22 18:27:49 -05:00
|
|
|
|
{
|
2024-02-26 09:32:12 -05:00
|
|
|
|
var sb = new StringBuilder();
|
|
|
|
|
patients?.ForEach(p => sb.Append($"{p.Name}, "));
|
|
|
|
|
return sb.ToString();
|
2024-02-22 18:27:49 -05:00
|
|
|
|
}
|
2024-02-22 18:29:51 -05:00
|
|
|
|
|
|
|
|
|
//3.) Write a LINQ query that returns the average weight of all patients in the Patients list
|
2024-02-26 09:32:12 -05:00
|
|
|
|
public static decimal? AverageWeightOfAllPatients(List<Patient>? patients)
|
2024-02-22 18:27:49 -05:00
|
|
|
|
{
|
2024-02-26 09:32:12 -05:00
|
|
|
|
return patients?.Average(p => p.Weight);
|
2024-02-22 18:27:49 -05:00
|
|
|
|
}
|
|
|
|
|
|
2024-02-22 18:29:51 -05:00
|
|
|
|
//4.) Write a LINQ query that orders the list of patients in the Patients list by DateCreated and returns the most recent
|
2024-02-26 09:32:12 -05:00
|
|
|
|
public static Patient? MostRecentPatientCreated(List<Patient>? patients)
|
2024-02-22 18:27:49 -05:00
|
|
|
|
{
|
2024-02-26 09:32:12 -05:00
|
|
|
|
return patients?.OrderByDescending(p => p.DateCreated).First();
|
2024-02-22 18:27:49 -05:00
|
|
|
|
}
|
|
|
|
|
|
2024-02-22 18:29:51 -05:00
|
|
|
|
//5.) Write a LINQ Query that concatenates the two lists of patients together
|
2024-02-26 09:32:12 -05:00
|
|
|
|
public static List<Patient>? ConcatPatientLists(List<Patient>? patients, List<Patient>? transferPatients)
|
2024-02-22 18:27:49 -05:00
|
|
|
|
{
|
2024-02-26 09:32:12 -05:00
|
|
|
|
if (transferPatients != null) patients?.AddRange(transferPatients);
|
|
|
|
|
return transferPatients;
|
2024-02-22 18:27:49 -05:00
|
|
|
|
}
|
|
|
|
|
|
2024-02-22 18:29:51 -05:00
|
|
|
|
//6.)Given the last of transferParentNames below, write a LINQ Query within DistinctTransferPatients that returns a list of distinct names
|
2024-02-26 09:32:12 -05:00
|
|
|
|
public static List<string>? DistinctTransferPatients(List<string>? transferPatientNames)
|
2024-02-22 18:27:49 -05:00
|
|
|
|
{
|
2024-02-26 09:32:12 -05:00
|
|
|
|
return transferPatientNames?.Distinct().ToList();
|
2024-02-22 18:27:49 -05:00
|
|
|
|
}
|
|
|
|
|
|
2024-02-22 18:29:51 -05:00
|
|
|
|
//7.) Write a LINQ Query that checks if the list of patients contains ANY patients with the name "Joe"
|
2024-02-26 09:32:12 -05:00
|
|
|
|
public static bool? ContainsJoe(List<Patient>? patients)
|
2024-02-22 18:27:49 -05:00
|
|
|
|
{
|
2024-02-26 09:32:12 -05:00
|
|
|
|
return patients?.Select(p => p.Name).Contains("Joe");
|
2024-02-22 18:27:49 -05:00
|
|
|
|
}
|
|
|
|
|
|
2024-02-22 18:29:51 -05:00
|
|
|
|
//8.) Write a LINQ Query that TAKEs the first 3 patients in the patient list
|
2024-02-26 09:32:12 -05:00
|
|
|
|
public static List<Patient>? First3Patients(List<Patient>? patients)
|
2024-02-22 18:27:49 -05:00
|
|
|
|
{
|
2024-02-26 09:32:12 -05:00
|
|
|
|
return patients?.Take(3).ToList();
|
2024-02-22 18:27:49 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public class Patient
|
|
|
|
|
{
|
|
|
|
|
public Patient(string name, int age, int heightFeet, int heightInches, decimal weight, DateTime dateCreated)
|
|
|
|
|
{
|
|
|
|
|
Name = name;
|
|
|
|
|
Age = age;
|
|
|
|
|
HeightFeet = heightFeet;
|
|
|
|
|
HeightInches = heightInches;
|
|
|
|
|
Weight = weight;
|
|
|
|
|
DateCreated = dateCreated;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override string ToString()
|
|
|
|
|
{
|
2024-02-26 09:32:12 -05:00
|
|
|
|
return $"Name: {Name}, Age: {Age}, Height: {HeightFeet}'{HeightInches}\", Weight: {Weight} lbs, DateCreated: {DateCreated:MMddyyyy}.";
|
2024-02-22 18:27:49 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public string Name { get; set; }
|
|
|
|
|
public int Age { get; set; }
|
|
|
|
|
public int HeightFeet { get; set; }
|
|
|
|
|
public int HeightInches { get; set; }
|
|
|
|
|
public decimal Weight { get; set; }
|
|
|
|
|
|
|
|
|
|
public DateTime DateCreated { get; set; }
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|