//Delete Person private static void DeletePerson() { GetPeople(); int PersonID; GetIntegerInput(" Enter ID: ", out PersonID); using (var context = new SuccinctlyExamplesEntities()) { Person P = context.People.FirstOrDefault(p => p.ID == PersonID); if (P != null) { context.People.Remove(P); if (context.SaveChanges() > 0) { Console.WriteLine("Delete Success"); } else { DisplayError("Error!"); } } else { DisplayError("Person does not exist."); } } }
/************************************** QUERY ***********************************/ //List Extract All People private static void GetPeople() { using (var context = new SuccinctlyExamplesEntities()) { List <Person> people = context.People.ToList(); if (people != null) { DisplayPersonTable(people); } } }
//Get Person By Last Name private static void GetPersonByLastName() { PromptInputMessage("Enter Last Name: "); string LastName = GetStringInput(); using (var context = new SuccinctlyExamplesEntities()) { IEnumerable <Person> query = context.People; Person person = query.SingleOrDefault(p => p.LastName == LastName); if (person != null) { DisplayPersonTable(null, person); } else { DisplayError("This person does not exist!"); } } }
//IQueryable Extract People by Gender private static void GetPeopleByGender() { int Gender; List <string> Menu = new List <string> { "MALE", "FEMALE" }; DisplayMenu("Select Gender", Menu, out Gender); using (var context = new SuccinctlyExamplesEntities()) { // Don't access the database just yet... IQueryable <Person> query = context.People .Where(p => p.GenderID == Gender) .OrderBy(p => p.LastName); // Access the database. List <Person> people = query.ToList(); DisplayPersonTable(people); } }
//Update Person Information private static void UpdatePersonInfo() { GetPeople(); int PersonID; GetIntegerInput(" Enter ID: ", out PersonID); using (var context = new SuccinctlyExamplesEntities()) { Console.Clear(); Console.WriteLine("Enter new data for this person below."); Person P = context.People.FirstOrDefault(p => p.ID == PersonID); if (P != null) { DisplayPersonTable(null, P); P.ID = PersonID; PromptInputMessage("Enter Last Name: "); P.LastName = GetStringInput(); do { PromptInputMessage("Enter First Name: "); P.FirstName = GetStringInput(); if (string.IsNullOrWhiteSpace(P.FirstName)) { DisplayError("First Name cannot be empty."); } } while (string.IsNullOrWhiteSpace(P.FirstName)); PromptInputMessage("Enter Middle Name: "); P.MiddleName = GetStringInput(); PromptInputMessage("Enter Date of Birth: "); DateTime DOB; bool isValidDOB = DateTime.TryParse(Console.ReadLine(), out DOB); if (isValidDOB) { P.DateOfBirth = DOB; } else { DisplayError("Invalid Date. It will be set to null."); P.DateOfBirth = null; } PromptInputMessage("Enter Gender (0 = Unknown, 1 = Male, 2 = Female): "); int GenderID; bool isValidGender = int.TryParse(Console.ReadLine(), out GenderID); if (isValidGender) { isValidGender = GenderID >= 0 && GenderID < 3 ? true : false; } if (isValidGender) { P.GenderID = GenderID; } else { DisplayError("Invalid Gender. It will be set to null."); P.GenderID = null; } if (context.SaveChanges() > 0) { Console.WriteLine("Update Success"); } else { DisplayError("Error!"); } } else { DisplayError("Person does not exist."); } } }
//Insert Person private static void InsertPerson() { using (var context = new SuccinctlyExamplesEntities()) { Console.Clear(); Console.WriteLine("Enter data for this person below."); Person P = new Person(); PromptInputMessage("Enter Last Name: "); P.LastName = GetStringInput(); do { PromptInputMessage("Enter First Name: "); P.FirstName = GetStringInput(); if (string.IsNullOrWhiteSpace(P.FirstName)) { DisplayError("First Name cannot be empty."); } } while (string.IsNullOrWhiteSpace(P.FirstName)); PromptInputMessage("Enter Middle Name: "); P.MiddleName = GetStringInput(); PromptInputMessage("Enter Date of Birth: "); DateTime DOB; bool isValidDOB = DateTime.TryParse(Console.ReadLine(), out DOB); if (isValidDOB) { P.DateOfBirth = DOB; } else { DisplayError("Invalid Date. It will be set to null."); P.DateOfBirth = null; } PromptInputMessage("Enter Gender (0 = Unknown, 1 = Male, 2 = Female): "); int GenderID; bool isValidGender = int.TryParse(Console.ReadLine(), out GenderID); if (isValidGender) { isValidGender = GenderID >= 0 && GenderID < 3 ? true : false; } if (isValidGender) { P.GenderID = GenderID; } else { DisplayError("Invalid Gender. It will be set to null."); P.GenderID = null; } context.People.Add(P); if (context.SaveChanges() > 0) { Console.WriteLine("Insert Success"); } else { DisplayError("Error!"); } } }
//Get People by Age private static void GetPeopleByAge() { string[] ops = new string[5] { "=", //is equal ">", //greater than "<", //less than ">=", //greater than or equal "<=" //less than or equal }; string[] words; do { words = null; Console.WriteLine("Input: = > < >= <= and the age. example: Age is > 10, Age is = 14, Age is >= 20."); PromptInputMessage(" Age is "); string input = GetStringInput(); words = input.Split(' '); } while (words.Length != 2); bool isValidOp = false; foreach (string o in ops) { if (words.First() == o) { isValidOp = true; } } if (isValidOp) { int Age; bool isValidAge = Int32.TryParse(words.Last(), out Age); if (isValidAge) { using (var context = new SuccinctlyExamplesEntities()) { List <Person> people = null; switch (words.First()) { case "=": //is equal { people = context.People.IsEqual(Age).ToList(); break; } case ">": //is greater than { people = context.People.IsGreaterThan(Age).ToList(); break; } case "<": //is less than { people = context.People.IsLessThan(Age).ToList(); break; } case ">=": //is greater than or equal { people = context.People.IsGreaterThanOrEqual(Age).ToList(); break; } case "<=": //is less than or equal { people = context.People.IsLessThanOrEqual(Age).ToList(); break; } default: { break; } } if (people != null) { DisplayPersonTable(people); } } } } }