Search through list of objects.
-
I have a list that consists of 'customer'-objects. Every 'customer' has a full name, address, email, and phone. Now I want to be able to do a search that lets the user type in information in a form and then perform a search based on the information the user typed in. In the form the user can type in 'first name', 'last name', 'address' and so on. But how do I compare the information the user types in with the information stored in the 'customer' objects? If the user only types in the 'first name' and 'phone' for example the search must pick out all 'customers' that matches that. Thanks for help!
-
I have a list that consists of 'customer'-objects. Every 'customer' has a full name, address, email, and phone. Now I want to be able to do a search that lets the user type in information in a form and then perform a search based on the information the user typed in. In the form the user can type in 'first name', 'last name', 'address' and so on. But how do I compare the information the user types in with the information stored in the 'customer' objects? If the user only types in the 'first name' and 'phone' for example the search must pick out all 'customers' that matches that. Thanks for help!
So, you go through all your objects, and you match all with the same first name, and phone, if that's what they typed.
Christian Graus Driven to the arms of OSX by Vista. "I am new to programming world. I have been learning c# for about past four weeks. I am quite acquainted with the fundamentals of c#. Now I have to work on a project which converts given flat files to XML using the XML serialization method" - SK64 ( but the forums have stuff like this posted every day )
-
I have a list that consists of 'customer'-objects. Every 'customer' has a full name, address, email, and phone. Now I want to be able to do a search that lets the user type in information in a form and then perform a search based on the information the user typed in. In the form the user can type in 'first name', 'last name', 'address' and so on. But how do I compare the information the user types in with the information stored in the 'customer' objects? If the user only types in the 'first name' and 'phone' for example the search must pick out all 'customers' that matches that. Thanks for help!
Maybe you can modify this a bit to suit your own needs. I am searching on one given string in a text box and pull all data in the DB to a listbox.
private void btnSearch\_Click(object sender, EventArgs e) { FillListBox(textBox1.Text); } public void FillListBox(string searchValue) { try { dataTable.Clear(); DataAdapter = new OleDbDataAdapter ("Select \[PicId\], \[Path\] From \[Pics\] WHERE PicId = '" + searchValue + "'", conn); DataAdapter.Fill(dataTable); listBox1.DataSource = dataTable; listBox1.DisplayMember = "Path"; listBox1.ValueMember = "Path"; } catch (Exception ex) { MessageBox.Show(ex.Message.ToString()); } }
Excellence is doing ordinary things extraordinarily well.
-
Maybe you can modify this a bit to suit your own needs. I am searching on one given string in a text box and pull all data in the DB to a listbox.
private void btnSearch\_Click(object sender, EventArgs e) { FillListBox(textBox1.Text); } public void FillListBox(string searchValue) { try { dataTable.Clear(); DataAdapter = new OleDbDataAdapter ("Select \[PicId\], \[Path\] From \[Pics\] WHERE PicId = '" + searchValue + "'", conn); DataAdapter.Fill(dataTable); listBox1.DataSource = dataTable; listBox1.DisplayMember = "Path"; listBox1.ValueMember = "Path"; } catch (Exception ex) { MessageBox.Show(ex.Message.ToString()); } }
Excellence is doing ordinary things extraordinarily well.
Thanks for your replies. I came up with a solution that worked well but I don't know if it's optimal: I search through all the information stored in each customer in the list. If the information in the customer object matches the information the user typed in the textbox the result is stored in a new list. If the information in the search textbox is empty it's also seen as a match. See below: public int[] SearchCustomers(Customer searchCustomer) { //Create list for search results. searchResult = new List<Customer>(); //Loop through all information in the customer-objects. searchResult = customers.FindAll(delegate(Customer C) { return SearchFirstName(C, searchCustomer); }); searchResult = searchResult.FindAll(delegate(Customer C) { return SearchLastName(C, searchCustomer); }); searchResult = searchResult.FindAll(delegate(Customer C) { return SearchCity(C, searchCustomer); }); searchResult = searchResult.FindAll(delegate(Customer C) { return SearchCountry(C, searchCustomer); }); searchResult = searchResult.FindAll(delegate(Customer C) { return SearchStreet(C, searchCustomer); }); searchResult = searchResult.FindAll(delegate(Customer C) { return SearchZip(C, searchCustomer); }); searchResult = searchResult.FindAll(delegate(Customer C) { return SearchCellPhone(C, searchCustomer); }); searchResult = searchResult.FindAll(delegate(Customer C) { return SearchHomePhone(C, searchCustomer); }); searchResult = searchResult.FindAll(delegate(Customer C) { return SearchEmail(C, searchCustomer); }); The final result is a list containing the matches of all search words.