Ejemplo n.º 1
0
        private void UpdateList()
        {
            db.Dispose();
            db = new ContactContext();

            var selectedIndex = lstPeople.SelectedIndex;

            lstPeople.Items.Clear();
            dgPhone.Rows.Clear();
            dgAddress.Rows.Clear();

            txtName.Text = "";

            var people = db.Contacts.OrderBy(s => s.Name).ToList();

            if (txtSearch.Text.Length > 0 && txtSearch.Text != "Search")
            {
                var searchText = txtSearch.Text.Trim().ToLower();
                people = db.Contacts.Where(s =>
                                           s.Name.ToLower().Contains(searchText) ||
                                           s.Addresses.Any(a => a.Address1.Contains(searchText)) ||
                                           s.Addresses.Any(a => a.City.Contains(searchText)) ||
                                           s.Addresses.Any(a => a.Country.Contains(searchText)) ||
                                           s.Addresses.Any(a => a.ZipCode.Contains(searchText)) ||
                                           s.Addresses.Any(a => a.Type.Type.Contains(searchText)) ||
                                           s.PhoneNumbers.Any(a => a.Type.Type.Contains(searchText))
                                           ).OrderBy(s => s.Name).ToList();
            }

            foreach (var item in people)
            {
                lstPeople.Items.Add(item);
            }

            if (lstPeople.Items.Count == 0)
            {
                dgAddress.Rows.Clear();
                dgPhone.Rows.Clear();
            }

            if (txtSearch.Text.Length == 0)
            {
                lstPeople.SelectedIndex = selectedIndex;
            }
        }