public PhoneEntries RemoveContact(string searchName, string searchNumber, int phonebookId, int entryId) { _phonebookContext.ChangeTracker.LazyLoadingEnabled = false; var phoneEntry = _phonebookContext.PhoneBookEntries.Where(pe => pe.entry_id == entryId && pe.Phonebook_id == phonebookId).FirstOrDefault(); _phonebookContext.PhoneBookEntries.Remove(phoneEntry); var entry = _phonebookContext.Entries.Where(e => e.Id == entryId).FirstOrDefault(); _phonebookContext.Remove(entry); _phonebookContext.SaveChanges(); var phonebook = _phonebookContext.Phonebook.Where(ph => ph.Id == phonebookId).FirstOrDefault(); IEnumerable <Entry> phoneBookEntries = null; if (!string.IsNullOrEmpty(searchName) || !string.IsNullOrEmpty(searchNumber)) { phoneBookEntries = _phonebookContext.PhoneBookEntries.Where(pe => pe.Phonebook_id == phonebookId).Include(p => p.Entry) .Select(pe => pe.Entry).ToList().Where(e => e.Name.ToLower().Equals(searchName.ToLower()) || e.PhoneNumber.Equals(searchNumber)); } else { phoneBookEntries = _phonebookContext.PhoneBookEntries.Where(pe => pe.Phonebook_id == phonebookId).Include(p => p.Entry) .Select(pe => pe.Entry).ToList(); } var phoneEntries = new PhoneEntries { Id = phonebookId, Entries = phoneBookEntries, Name = phonebook.Name }; return(phoneEntries); }