linq-assignment/LinqActivity/Program.cs
2024-02-26 09:32:12 -05:00

178 lines
7.3 KiB
C#

using System.Text;
namespace LinqActivity
{
internal class Program
{
private static void Main()
{
Console.WriteLine("LINQ Activity - 2/23/2024");
Console.WriteLine("Name: Matthew Oros");
var patients = new List<Patient>
{
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))
};
var transferPatients = new List<Patient>
{
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))
};
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);
if (distinctTransferPatientsNames != null)
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
public static void DisplayPatients(List<Patient>? patients)
{
if (patients == null)
{
Console.WriteLine("Patient List Provided is NULL :O");
return;
}
foreach (var patient in patients)
{
Console.WriteLine($"Name: {patient.Name}, Age: {patient.Age}, Height: {patient.HeightFeet}'{patient.HeightInches}\", Weight: {patient.Weight} lbs, DateCreated: {patient.DateCreated:MMddyyyy}.");
}
}
//1.) Write a LINQ query that returns all patients over the age of 30 in the "Patients" list
public static List<Patient>? AllPatientsOver30(List<Patient>? patients)
{
return patients?.Where(p => p.Age > 30).ToList();
}
//2.) Write a LINQ query that takes the patients in the Patients list, and returns a string of their names separated by a comma
public static string AllPatientNames(List<Patient>? patients)
{
var sb = new StringBuilder();
patients?.ForEach(p => sb.Append($"{p.Name}, "));
return sb.ToString();
}
//3.) Write a LINQ query that returns the average weight of all patients in the Patients list
public static decimal? AverageWeightOfAllPatients(List<Patient>? patients)
{
return patients?.Average(p => p.Weight);
}
//4.) Write a LINQ query that orders the list of patients in the Patients list by DateCreated and returns the most recent
public static Patient? MostRecentPatientCreated(List<Patient>? patients)
{
return patients?.OrderByDescending(p => p.DateCreated).First();
}
//5.) Write a LINQ Query that concatenates the two lists of patients together
public static List<Patient>? ConcatPatientLists(List<Patient>? patients, List<Patient>? transferPatients)
{
if (transferPatients != null) patients?.AddRange(transferPatients);
return transferPatients;
}
//6.)Given the last of transferParentNames below, write a LINQ Query within DistinctTransferPatients that returns a list of distinct names
public static List<string>? DistinctTransferPatients(List<string>? transferPatientNames)
{
return transferPatientNames?.Distinct().ToList();
}
//7.) Write a LINQ Query that checks if the list of patients contains ANY patients with the name "Joe"
public static bool? ContainsJoe(List<Patient>? patients)
{
return patients?.Select(p => p.Name).Contains("Joe");
}
//8.) Write a LINQ Query that TAKEs the first 3 patients in the patient list
public static List<Patient>? First3Patients(List<Patient>? patients)
{
return patients?.Take(3).ToList();
}
}
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()
{
return $"Name: {Name}, Age: {Age}, Height: {HeightFeet}'{HeightInches}\", Weight: {Weight} lbs, DateCreated: {DateCreated:MMddyyyy}.";
}
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; }
}
}