public static bool AddTenant(string tenantName, string doorName) { try { using (var dataList = new Database.MyDatabase()) { var nameSplit = tenantName.Split(" "); const string tagList = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; var tagListArray = tagList.ToCharArray(); var i = 0; var emptyTagLetter = false; while ((i < tagListArray.Length) && (!emptyTagLetter)) { var tagToCheck = doorName + tagListArray[i]; var result = dataList.Tenants.Any(d => d.Tag == tagToCheck); if (!result) { var location = dataList.Locations.FirstOrDefault(d => d.LocationName == doorName); emptyTagLetter = true; dataList.Tenants.Add(new Models.Tenant { FirstName = nameSplit[0], LastName = nameSplit[1], Tag = tagToCheck, Location = location }); dataList.SaveChanges(); } i++; } } return(true); } catch (Exception) { return(false); } }
/// <summary> /// Saves a log entry to DB /// </summary> /// <param name="logEntry">String array containing test data for logging</param> /// <returns>bool true if transaction succeeded</returns> public static bool LogEntry(string[] logEntry) { //Creates a bool with value false. Only changes to true if transaction is complete var logAccepted = false; //Creates an instance of MyDatabase using (var db = new Database.MyDatabase()) //Try/catch-statement to stop transaction if any error occurs try { //Begins transaction db.Database.BeginTransaction(); //Adds a new object from LogEntry-class to LogEntries-table in DB db.LogEntries.Add(new Models.LogEntry { Date = logEntry[0], Door = logEntry[1], Tag = logEntry[2], Event = logEntry[3] }); //Saves changes to DB db.SaveChanges(); //Committs transaction to DB db.Database.CommitTransaction(); //Sets bool value to true logAccepted = true; } catch { //Rollbacks transaction db.Database.RollbackTransaction(); } return(logAccepted); }
public static List <Models.Log> FindEntriesByTenant(string tenantFirstName, string tenantLastName = null) { using (var dataList = new Database.MyDatabase()) { var tenant = dataList.Tenants.FirstOrDefault(d => d.FirstName + " " + d.LastName == tenantFirstName || d.FirstName + " " + d.LastName == tenantFirstName + " " + tenantLastName); return(dataList.Logs.Include("Door").Include("Event").Include("Tenant").Where(d => d.Tenant == tenant).OrderByDescending(d => d.Date).Take(MaxEntries).ToList()); } }
public static List <Models.Log> FindEntriesByTag(string tagName) { using (var dataList = new Database.MyDatabase()) { var tag = dataList.Tenants.FirstOrDefault(d => d.Tag == tagName); return(dataList.Logs.Include("Door").Include("Event").Include("Tenant").Where(d => d.Tenant == tag).OrderByDescending(d => d.Date).Take(MaxEntries).ToList()); } }
public static List <Models.Log> FindEntriesByLocation(string locationName) { using (var dataList = new Database.MyDatabase()) { var location = dataList.Doors.FirstOrDefault(d => d.DoorName == locationName); return(dataList.Logs.Include("Door").Include("Event").Include("Tenant").Where(d => d.Door == location).OrderByDescending(d => d.Date).Take(MaxEntries).ToList()); } }
public static List <string> ListTenantsAt(string doorName) { using (var dataList = new Database.MyDatabase()) { var door = dataList.Locations.FirstOrDefault(d => d.LocationName == doorName); return(dataList.Tenants.Where(d => d.Location == door).Select(d => d.FirstName + " " + d.LastName).ToList()); } }
/// <summary> /// Metod som kollar ifall alla tabeller är tomma när programmet startar. Och tillkallar metoderna som fyller på data till tabellerna ifall svaret är ja. /// </summary> public static void CheckTables() { using var db = new Database.MyDatabase(); if (!db.Door_Explanation.Any() && !db.Doors.Any() && !db.Event.Any() && !db.Logs.Any() && !db.Tenants.Any() && !db.Tenants_Info.Any()) { Helper.Seeder.TablesInsert(); Helper.Seeder.LogsInsert(); } }
/// <summary> /// Lists all tenants living in a specific location /// </summary> /// <param name="location">Name of location</param> /// <returns>List of all tenants living in searched location</returns> public List <Models.Tenant> ListTenantsAt(string location) { //Creates an empty list var list = new List <Models.Tenant>(); using var db = new Database.MyDatabase(); //Adds objects to list based on location search word list = db.Tenants.Where(n => n.Location.Equals(location)).ToList(); //returns filtered list return(list); }
/// <summary> /// Metod som lägger in objekt (data) in i alla tabeller som skapats för databasen (Förutom Logs). /// </summary> public static void TablesInsert() { using var db = new Database.MyDatabase(); db.Door_Explanation.AddRange(DoorExplanations); db.Doors.AddRange(Doors); db.Event.AddRange(EventExplanations); db.Tenants.AddRange(Tenants); db.Tenants_Info.AddRange(TenantsInfo); db.SaveChanges(); }
private static void StandardData() { using (var dataList = new Database.MyDatabase()) { var doorNames = new[] { "LGH0101", "LGH0102", "LGH0103", "LGH0201", "LGH0202", "LGH0301", "LGH0302", "BLK0101", "BLK0102", "BLK0103", "BLK0201", "BLK0202", "BLK0301", "BLK0302", "VAKT", "TVÄTT", "SOPRUM", "UT" }; var events = new[] { "FDIN", "FDUT", "DÖIN", "DÖUT" }; var locations = new[] { "0101", "0102", "0103", "0201", "0202", "0301", "0302", "VAKT", "TVÄTT", "SOPRUM" }; foreach (var doorName in doorNames) { dataList.Doors.Add(new Models.Door { DoorName = doorName }); dataList.SaveChanges(); } foreach (var @event in events) { dataList.Events.Add(new Models.Event { Code = @event }); dataList.SaveChanges(); } foreach (var location in locations) { dataList.Locations.Add(new Models.Location { LocationName = location }); dataList.SaveChanges(); } var tenants = new[, ] { { "Liam", "Jönsson", "0101A", "1" }, { "Elias", "Petterson", "0102A", "2" }, { "Wilma", "Johansson", "0102B", "2" }, { "Alicia", "Sanchez", "0103A", "3" }, { "Aaron", "Sanchez", "0103B", "3" }, { "Olivia", "Erlander", "0201A", "4" }, { "William", "Erlander", "0201B", "4" }, { "Alexander", "Erlander", "0201C", "4" }, { "Astrid", "Erlander", "0201D", "4" }, { "Lucas", "Adolfsson", "0202A", "5" }, { "Ebba", "Adolfsson", "0202B", "5" }, { "Lilly", "Adolfsson", "0202C", "5" }, { "Ella", "Ahlström", "0301A", "6" }, { "Alma", "Alfredsson", "0301B", "6" }, { "Elsa", "Ahlström", "0301C", "6" }, { "Maja", "Ahlström", "0301D", "6" }, { "Noah", "Almgren", "0302A", "7" }, { "Adam", "Andersen", "0302B", "7" }, { "Kattis", "Backman", "0302C", "7" }, { "Oscar", "Chen", "0302D", "7" }, { "Vaktmästare", "", "VAKT01", "8" } }; for (int i = 0; i < tenants.GetLength(0); i++) { dataList.Tenants.Add(new Models.Tenant { FirstName = tenants[i, 0], LastName = tenants[i, 1], Tag = tenants[i, 2], Location = dataList.Locations.FirstOrDefault(l => l.ID == int.Parse(tenants[i, 3])) }); dataList.SaveChanges(); } Console.WriteLine("Standard Data created."); } }
public static List <Tenants> ListTenantsAt(string apartment) // Söker hyresgäster från specifik lägenhet, listar upp deras namn och tagg-kod. returenera en List { using var db = new Database.MyDatabase(); var apartmentQuery2 = from t in db.Tenants where t.Location == apartment select t; List <Tenants> logList = new List <Tenants>(); foreach (var tenant in apartmentQuery2.Take(MaxEntries)) { logList.Add(tenant); } return(logList); }
public static List <Logs> FindEntriesByEvent(string evnt) // Sök de senaste loggar från en viss händelse, returnera en List { using var db = new Database.MyDatabase(); var eventQuery = from l in db.Logs.Include("Door").Include("Tenant").Include("Event") where l.Event.Code == evnt orderby l.Date descending select l; List <Logs> logList = new List <Logs>(); foreach (var log in eventQuery.Take(MaxEntries)) { logList.Add(log); } return(logList); }
public static List <Logs> FindEntriesByDoor(string door) // Sök de sensaste loggar från en viss dörr, returnerera en List { using var db = new Database.MyDatabase(); var doorQuery = from l in db.Logs.Include("Door").Include("Tenant").Include("Event") where l.Door.Door == door orderby l.Date descending select l; List <Logs> logList = new List <Logs>(); foreach (var log in doorQuery.Take(MaxEntries)) { logList.Add(log); } return(logList); }
public static List <Logs> FindEntriesByTag(string tag) // Sök de senaste loggar fårn en viss tagg, returnera en List { using var db = new Database.MyDatabase(); var tagQuery = from l in db.Logs.Include("Door").Include("Tenant").Include("Event") where l.Tenant.Tag == tag orderby l.Date descending select l; List <Logs> logList = new List <Logs>(); foreach (var log in tagQuery.Take(MaxEntries)) { logList.Add(log); } return(logList); }
public static List <Logs> FindEntriesByDate(string date) { using var db = new Database.MyDatabase(); var dateQuery = from l in db.Logs.Include("Door").Include("Tenant").Include("Event") where l.Date == date // Hitta något sätt att endast söka efter en dag. Inte exakta tidpunkten orderby l.Date descending select l; List <Logs> logList = new List <Logs>(); foreach (var log in dateQuery.Take(MaxEntries)) { logList.Add(log); } return(logList); }
public static List <Logs> FindEntriesByLocation(string location) // Sök de senaste loggar från en viss lägenhet/rum, returnera en List { using var db = new Database.MyDatabase(); var locationQuery = from l in db.Logs.Include("Door").Include("Tenant").Include("Event") where l.Door.Location == location orderby l.Date descending select l; List <Logs> logList = new List <Logs>(); foreach (var log in locationQuery.Take(MaxEntries)) { logList.Add(log); } return(logList); }
public static void CheckData() { using (var dataList = new Database.MyDatabase()) { var standardData = dataList.Tenants.Take(1).ToList(); var staticData = dataList.Logs.Take(1).ToList(); if (standardData.Count == 0) { StandardData(); } else if (staticData.Count == 0) { StaticData(); } } }
public static bool AddTenant(string newTenant) { try { using var db = new Database.MyDatabase(); db.Tenants.Add(new Tenants { Tenant = newTenant }); db.SaveChanges(); return(true); } catch (System.Exception) { return(false); } }
/// <summary> /// Searches DB to check if tenants name exists. /// </summary> /// <param name="tenant">Tenants name or tag</param> /// <returns>bool true if tenant exists in DB</returns> private static bool CheckIfTenantNameExists(string tenant) { //Creates a bool with value false var nameExists = false; using (var db = new Database.MyDatabase()) //Searches DB if tenant exists foreach (var item in db.Tenants) { //Sets bool value true if tenant exist in DB if (item.Name == tenant) { nameExists = true; } } //returns bool value return(nameExists); }
/// <summary> /// Metod som slumpgenererar och matar in data in i logs tabellen /// </summary> public static void LogsInsert() { Random rnd = new Random(); using var db = new Database.MyDatabase(); for (int pos = 0; pos < 200; pos++) { db.Add(new Models.Logs { Date = RandomDate(rnd), Door = db.Doors.ToList()[rnd.Next(0, db.Doors.Count())], // slumpar rader från tabellen Doors i databasen Event = db.Event.ToList()[rnd.Next(0, db.Event.Count())], Tenant = db.Tenants.ToList()[rnd.Next(0, db.Tenants.Count())] }); } db.SaveChanges(); }
/// <summary> /// Checks if tenant is already living in this location. If true, no transaction is committed /// </summary> /// <param name="tenant">Name of tenant</param> /// <param name="newLocation">Name of new location</param> /// <returns>bool true if tenant is already living in this location</returns> private static bool TenantAlreadyLivingInLocation(string tenant, string newLocation) { //Creates a bool and sets its value to false var tenantLivingInLocation = false; using (var db = new Database.MyDatabase()) { //Creates a list of all objects with same name AND location as user input var list = db.Tenants.Where(_ => _.Name.Equals(tenant) && _.Location.Equals(newLocation)).ToList(); //If list has any object, it means that tenant is already living here //and will reatur a true value if (list.Count > 0) { tenantLivingInLocation = true; } } return(tenantLivingInLocation); }
public static void LogEntry(string date, string door, string evnt, string tenant) // Informationen skickas in i textform och sparas i lämplig form och i lämpliga tabeller i databasen. { using var db = new Database.MyDatabase(); db.Add(new Logs { Date = date, Door = new Doors { Door = door }, Event = new Event { Code = evnt }, Tenant = new Tenants { Tenant = tenant } }); }
public static bool MoveTenant(string tenant, string doorName = null) { try { using (var dataList = new Database.MyDatabase()) { var identifyTenant = dataList.Tenants.Include("Location").FirstOrDefault(d => d.Tag == tenant || d.FirstName + " " + d.LastName == tenant); if (String.IsNullOrEmpty(doorName)) { identifyTenant.Location = null; identifyTenant.Tag = null; dataList.SaveChanges(); } else { const string tagList = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; var tagListArray = tagList.ToCharArray(); var i = 0; var emptyTagLetter = false; while ((i < tagListArray.Length) && (!emptyTagLetter)) { var tagToCheck = doorName + tagListArray[i]; var result = dataList.Tenants.Any(d => d.Tag == tagToCheck); if (!result) { var location = dataList.Locations.FirstOrDefault(d => d.LocationName == doorName); emptyTagLetter = true; identifyTenant.Tag = tagToCheck; identifyTenant.Location = location; dataList.SaveChanges(); } i++; } } } return(true); } catch (Exception) { return(false); } }
public static bool LogEntry(string date, string doorName, string eventCode, string tag) { try { using (var dataList = new Database.MyDatabase()) { var door = dataList.Doors.FirstOrDefault(d => d.DoorName == doorName.ToUpper()); var @event = dataList.Events.FirstOrDefault(d => d.Code == eventCode.ToUpper()); var tenant = dataList.Tenants.FirstOrDefault(d => d.Tag == tag.ToUpper()); dataList.Logs.Add(new Models.Log { Date = date, Door = door, Event = @event, Tenant = tenant }); dataList.SaveChanges(); } return(true); } catch (Exception) { return(false); } }
/// <summary> /// Joins LogEntry-table with Tenant-table to create a list of new objects /// containing necessary info from both tables /// </summary> /// <returns>List of objects</returns> private static List <OutputHelper.IOutput> GetListOfJoinedTables() { //Creates a list for objects from ConsoleOutput-class var list = new List <Helpers.OutputHelper.IOutput>(); //Creates a list for objects from LogEntry-class var logList = new List <Models.LogEntry>(); //Creates a list for objects from Tenant-class var tenantsList = new List <Models.Tenant>(); using (var db = new Database.MyDatabase()) { //Creates a list with objects from two joined tables. The Tag-value from //LogEntries is used to locate name of tenant i Tenants. var query = (from log in db.LogEntries.ToList() join ten in db.Tenants.ToList() on log.Tag equals ten.Tag orderby log.Date descending select new { log.Date, log.Door, log.Tag, log.Event, ten.Name }).ToList(); //Transfers all objects from query to a list of ConsoleOutput-objects foreach (var item in query) { list.Add(new Helpers.OutputHelper.ConsoleOutput { Date = item.Date, Door = item.Door, Tag = item.Tag, Event = item.Event, Tenant = item.Name }); } } return(list); }
/// <summary> /// Adds a tenant w/o tag or location to DB /// </summary> /// <param name="tenantName">Name of tenant</param> /// <returns>bool TRUE if transaction succeded</returns> public static bool AddTenant(string tenantName) { //Creates a bool with value false. Only changes to true if transaction is complete var addAccepted = false; using (var db = new Database.MyDatabase()) try { //Begins transaction to DB db.Database.BeginTransaction(); //Checks if tenant name already exists in DB. To prevent same tenant to be added again by mistake, //and to avoid mixup/confusion when searching for entries by tenant in logs if (!CheckIfTenantNameExists(tenantName)) { //Creates a new object from Tenant-class, with Name value from user input var newTenant = new Models.Tenant { Name = tenantName }; //Adds new tenant to DB db.Tenants.Add(newTenant); //Saves changes to DB db.SaveChanges(); //Committs transaction to DB db.Database.CommitTransaction(); //Sets bool value to true addAccepted = true; } } catch { //Rollbacks transaction db.Database.RollbackTransaction(); } //returns bool value return(addAccepted); }
/// <summary> /// Creates a uniqe tag for a specific location /// </summary> /// <param name="location">Location for the tag</param> /// <returns>First avaliable letter in alphabet</returns> /// private static char CreateNewTag(string location) { //Creates a var for the letter at the end of the new tag char letter; using (var db = new Database.MyDatabase()) { //Creates a new list with all tags from this location var locList = db.Tenants.Where(_ => _.Location.Equals(location)).ToList(); //Sets var letter to A letter = 'A'; foreach (var item in locList) { //If letter 'A' already exists, loops through all letters in alphabetical order //to find first uniqe number if (item.Tag[4] == letter) { letter = (char)(((int)letter) + 1); } } } //returns a uniqe letter to create a new tag return(letter); }
/// <summary> /// Moves a tenant to a new location, or no location (tenants moves out) /// </summary> /// <param name="tenant">Name or tag of tenant</param> /// <param name="newLocation">New location, or empty (tenant moves out)</param> /// <returns>bool true if transaction succeded</returns> public static bool MoveTenant(string tenant, string newLocation) { //Creates a bool with false-value. Only changes to true if transaction is complete var moveAccepted = false; //Checks it new location is existing in DB, or set to blank (tenants is moving out) //If not, bool is still set to false and no transaction is committed if (CheckIfLocationExists(newLocation) || newLocation == "") { //Checks if tenant is already living in this location //If true, no transaction is committed if (!TenantAlreadyLivingInLocation(tenant, newLocation)) { //Creates an instance of MyDatabase using var db = new Database.MyDatabase(); //Try/catch-statement to stop transaction if any errors occurs try { //Creates a new list for objects var findTenantsId = new List <Models.Tenant>(); //Begins transaction db.Database.BeginTransaction(); //Checks is user input is a name of a tenants stored in DB. If found, adds tenant to list if (CheckIfTenantNameExists(tenant)) { findTenantsId = db.Tenants.Where(_ => _.Name.Equals(tenant)).ToList(); } //If user input is a correct tag, tenant is added to list. If not correct, nothing is added to list else { findTenantsId = db.Tenants.Where(_ => _.Tag.Equals(tenant)).ToList(); } //Creates var with tenants ID. If list is empty (neither name or tag is correct), transaction breaks var tID = db.Tenants.Find(findTenantsId[0].ID); //Sets value of new location to tenant via tenants ID db.Entry(tID).Property(_ => _.Location).CurrentValue = newLocation; //Checks id tenant is moving out (no new location) if (newLocation == "") { //If new location is "", deletes value for tag as well (tenant has moved out) db.Entry(tID).Property(_ => _.Tag).CurrentValue = ""; } else { //Creates a new tag based on new location db.Entry(tID).Property(_ => _.Tag).CurrentValue = newLocation + CreateNewTag(newLocation); } //Saves changes to DB db.SaveChanges(); //Commits transaction db.Database.CommitTransaction(); //Sets bool to true moveAccepted = true; } catch { //Rollbacks transaction db.Database.RollbackTransaction(); } } } return(moveAccepted); }
/// <summary> /// Metod som skriver ut information till användaren (output) och tar emot data från användaren (input) och tillkallar metoderna med som ansvarar för det användaren bad om. /// </summary> public static void View() { using var db = new Database.MyDatabase(); int input = 11; while (input < 0 || input > 10) { Console.WriteLine("Ange vad du vill söka logsen efter. \n1. Door \n2. Location \n3. Event \n4. Tenant \n5. Tag \n6. Date \n7. Tenants in apartment \n8. Move tenant \n9. Add Tenant \n10. TextLog Input"); input = int.Parse(Console.ReadLine()); switch (input) { case 1: Console.WriteLine("Ange vilken dörr du vill söka efter (ex LGH0101, BLK0101)"); string door = Console.ReadLine(); foreach (var log in Models.DoorEventsLog.FindEntriesByDoor(door)) { Console.WriteLine(log.Date + "\t" + log.Door.Door + "\t" + log.Tenant.Tag + "\t" + log.Event.Code + "\t" + log.Door.Location + "\t" + log.Tenant.Tenant); } break; case 2: Console.WriteLine("Ange vilken plats du vill söka efter (ex 0101, TVÄTT)"); string location = Console.ReadLine(); foreach (var log in Models.DoorEventsLog.FindEntriesByLocation(location)) { Console.WriteLine(log.Date + "\t" + log.Door.Door + "\t" + log.Tenant.Tag + "\t" + log.Event.Code + "\t" + log.Door.Location + "\t" + log.Tenant.Tenant); } break; case 3: Console.WriteLine("Ange vilken händelse du vill söka efter (FDIN, FDUT, DÖIN, DÖUT)"); string evnt = Console.ReadLine(); foreach (var log in Models.DoorEventsLog.FindEntriesByEvent(evnt)) { Console.WriteLine(log.Date + "\t" + log.Door.Door + "\t" + log.Tenant.Tag + "\t" + log.Event.Code + "\t" + log.Door.Location + "\t" + log.Tenant.Tenant); } break; case 4: Console.WriteLine("Ange vilken inneboende du vill söka efter (ex Liam Jönsson)"); string tenant = Console.ReadLine(); foreach (var log in Models.DoorEventsLog.FindEntriesByTenant(tenant)) { Console.WriteLine(log.Date + "\t" + log.Door.Door + "\t" + log.Tenant.Tag + "\t" + log.Event.Code + "\t" + log.Door.Location + "\t" + log.Tenant.Tenant); } break; case 5: Console.WriteLine("Ange vilken tag du vill söka efter (ex 0101A)"); string tag = Console.ReadLine(); foreach (var log in Models.DoorEventsLog.FindEntriesByTag(tag)) { Console.WriteLine(log.Date + "\t" + log.Door.Door + "\t" + log.Tenant.Tag + "\t" + log.Event.Code + "\t" + log.Door.Location + "\t" + log.Tenant.Tenant); } break; case 6: Console.WriteLine("Ange vilket datum du vill söka logsen efter (YYYY-MM-DD hh:mm:ss)"); string date = Console.ReadLine(); foreach (var log in Models.DoorEventsLog.FindEntriesByDate(date)) { Console.WriteLine(log.Date + "\t" + log.Door.Door + "\t" + log.Tenant.Tag + "\t" + log.Event.Code + "\t" + log.Door.Location + "\t" + log.Tenant.Tenant); } break; case 7: Console.WriteLine("Ange vilken lägenhets för att se dess inneboende"); string apartment1 = Console.ReadLine(); foreach (var log in Models.DoorEventsLog.ListTenantsAt(apartment1)) { Console.WriteLine(log.Tenant); } break; case 8: Console.WriteLine("Ange den flyttandes namn eller tag: "); string oldTenant = Console.ReadLine(); Console.WriteLine("Ange lägenheten i fråga (ex. 0101) OBS. Lämnas blankt om personen i fråga flyttar ut)"); string apartment2 = Console.ReadLine(); Console.WriteLine(Models.DoorEventsLog.MoveTenant(oldTenant, apartment2)); // skriver ut true false ifall resulatatet lyckades eller inte break; case 9: Console.WriteLine("Ange den nya inneboendes namn"); string newTenant1 = Console.ReadLine(); Console.WriteLine(Models.DoorEventsLog.AddTenant(newTenant1)); // Skriver ut True om insättningen lyckades och false om den misslyckades break; case 10: Console.WriteLine("Ange datum: (yyyy-mm-dd hh:mm:ss"); string date1 = Console.ReadLine(); Console.WriteLine("Ange dörr:(ex LGH0101, BLK0101)"); string door1 = Console.ReadLine(); Console.WriteLine("Ange händelse: (FDIN, FDUT, DÖIN, DÖUT)"); string evnt1 = Console.ReadLine(); Console.WriteLine("Ange Inneboende (Ella Ahlström)"); string tenant1 = Console.ReadLine(); Models.DoorEventsLog.LogEntry(date1, door1, evnt1, tenant1); break; } } }
public static bool MoveTenant(string tenant, string apartment) { using var db = new Database.MyDatabase(); int count = 0; List <char> tagHelper = new List <char>() { 'A', 'B', 'C', 'D', 'E', 'F', 'G' }; if (apartment == string.Empty) { try { var moveQuery = from t in db.Tenants where t.Tenant == tenant select t; var moveQueryList = moveQuery.ToList()[0]; if (moveQuery != null) { moveQueryList.Tag = null; moveQueryList.Location = null; db.Tenants.Update(moveQueryList); db.SaveChanges(); } return(true); } catch (System.Exception) { return(false); } } else { try { bool tagChecker = false; var moveQuery = from t in db.Tenants where t.Tenant == tenant select t; var moveQueryList = moveQuery.ToList()[0]; if (moveQuery != null) { do { string tag = apartment + tagHelper[count]; var tagQuery = from t in db.Tenants where t.Tag == tag select t; var tagQueryList = tagQuery.ToList(); if (!tagQueryList.Any()) { moveQueryList.Tag = tag; tagChecker = true; } else { count++; tagChecker = false; } } while (!tagChecker); moveQueryList.Location = apartment; db.Tenants.Update(moveQueryList); db.SaveChanges(); } } catch (System.Exception) { return(false); } return(false); } }