Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Search through list of objects.

Search through list of objects.

Scheduled Pinned Locked Moved C#
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • X Offline
    X Offline
    xkrja
    wrote on last edited by
    #1

    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!

    C M 2 Replies Last reply
    0
    • X xkrja

      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!

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      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 )

      1 Reply Last reply
      0
      • X xkrja

        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!

        M Offline
        M Offline
        MumbleB
        wrote on last edited by
        #3

        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.

        X 1 Reply Last reply
        0
        • M MumbleB

          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.

          X Offline
          X Offline
          xkrja
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups