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. Finding value in a list

Finding value in a list

Scheduled Pinned Locked Moved C#
questiontutorial
10 Posts 7 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.
  • N Offline
    N Offline
    Nathaniel Sumaya
    wrote on last edited by
    #1

    public Country(string cAbs, string cName)
    {
    this._country = cName;
    this._abs = cAbs;
    }

    private void btnOk_Click(object sender, EventArgs e)
    {
    Country myCountry = new Country();

            CountryList countries = new CountryList();
    
            countries.Add(new Country("AE", "United Arab Emirates"));
            countries.Add(new Country("AF", "Afghanistan"));
            countries.Add(new Country("AQ", "Antarctica"));
            countries.Add(new Country("AR", "Argentina"));
            countries.Add(new Country("AT", "Austria"));
            countries.Add(new Country("AU", "Australia"));
            countries.Add(new Country("BD", "Bangladesh"));
            countries.Add(new Country("BE", "Belgium"));
            countries.Add(new Country("BR", "Brazil"));
            countries.Add(new Country("BS", "Bahamas"));
            countries.Add(new Country("CA", "Canada"));
            countries.Add(new Country("CH", "Switzerland"));
            countries.Add(new Country("CL", "Chilie"));
            countries.Add(new Country("CM", "Cameroon"));
            countries.Add(new Country("CL", "Chilie"));
            countries.Add(new Country("CN", "China"));
            countries.Add(new Country("CO", "Columbia"));
            countries.Add(new Country("CR", "Costa Rica"));
            countries.Add(new Country("DK", "Denmark"));
            countries.Add(new Country("DM", "Dominica"));
            countries.Add(new Country("DO", "Domican Republic"));
            countries.Add(new Country("EC", "Ecuador"));
            countries.Add(new Country("EE", "Estonia"));
            countries.Add(new Country("EG", "Egypt"));
            countries.Add(new Country("ES", "Spain"));
            countries.Add(new Country("ET", "Ethiopia"));
            countries.Add(new Country("EU", "European Union"));
    
            //CountryList country = countries.Where(f=> f.CountryAbreviation == this.textBox1.Text)  
    
            //Country country = countries.Where(f => f.CountryAbreviation == this.textBox1.Text).OrderByDescending(f => f.CountryAbreviation).FirstOrDefault();
                       
        }
    

    Question: 1. How to find the a country in a list?

    E R L C 4 Replies Last reply
    0
    • N Nathaniel Sumaya

      public Country(string cAbs, string cName)
      {
      this._country = cName;
      this._abs = cAbs;
      }

      private void btnOk_Click(object sender, EventArgs e)
      {
      Country myCountry = new Country();

              CountryList countries = new CountryList();
      
              countries.Add(new Country("AE", "United Arab Emirates"));
              countries.Add(new Country("AF", "Afghanistan"));
              countries.Add(new Country("AQ", "Antarctica"));
              countries.Add(new Country("AR", "Argentina"));
              countries.Add(new Country("AT", "Austria"));
              countries.Add(new Country("AU", "Australia"));
              countries.Add(new Country("BD", "Bangladesh"));
              countries.Add(new Country("BE", "Belgium"));
              countries.Add(new Country("BR", "Brazil"));
              countries.Add(new Country("BS", "Bahamas"));
              countries.Add(new Country("CA", "Canada"));
              countries.Add(new Country("CH", "Switzerland"));
              countries.Add(new Country("CL", "Chilie"));
              countries.Add(new Country("CM", "Cameroon"));
              countries.Add(new Country("CL", "Chilie"));
              countries.Add(new Country("CN", "China"));
              countries.Add(new Country("CO", "Columbia"));
              countries.Add(new Country("CR", "Costa Rica"));
              countries.Add(new Country("DK", "Denmark"));
              countries.Add(new Country("DM", "Dominica"));
              countries.Add(new Country("DO", "Domican Republic"));
              countries.Add(new Country("EC", "Ecuador"));
              countries.Add(new Country("EE", "Estonia"));
              countries.Add(new Country("EG", "Egypt"));
              countries.Add(new Country("ES", "Spain"));
              countries.Add(new Country("ET", "Ethiopia"));
              countries.Add(new Country("EU", "European Union"));
      
              //CountryList country = countries.Where(f=> f.CountryAbreviation == this.textBox1.Text)  
      
              //Country country = countries.Where(f => f.CountryAbreviation == this.textBox1.Text).OrderByDescending(f => f.CountryAbreviation).FirstOrDefault();
                         
          }
      

      Question: 1. How to find the a country in a list?

      E Offline
      E Offline
      Ed Hill _5_
      wrote on last edited by
      #2

      Country foundCountry = CountryList.FirstOrDefault(c=>c.CountryAbreviation == this.textBox1.Text);
      if (foundCountry==null)
      {
      //invalid search text
      }

      Have you considered Using a

      Dictionary

      that would allow

      CountryList.ContainsKey(this.textBox1.Text)

      and

      Country foundCountry = CountryList[this.textBox1.Text]

      What you want to do will depend on if you have more than one item with the same abreviation

      A 1 Reply Last reply
      0
      • E Ed Hill _5_

        Country foundCountry = CountryList.FirstOrDefault(c=>c.CountryAbreviation == this.textBox1.Text);
        if (foundCountry==null)
        {
        //invalid search text
        }

        Have you considered Using a

        Dictionary

        that would allow

        CountryList.ContainsKey(this.textBox1.Text)

        and

        Country foundCountry = CountryList[this.textBox1.Text]

        What you want to do will depend on if you have more than one item with the same abreviation

        A Offline
        A Offline
        AmitGajjar
        wrote on last edited by
        #3

        Just want to point out

        Quote:

        Country foundCountry = CountryList[this.textBox1.Text]

        in above code there may be chances of KeyNotFound Exception[^] Anyway 5+

        Thanks -Amit Gajjar (MinterProject)

        E 1 Reply Last reply
        0
        • A AmitGajjar

          Just want to point out

          Quote:

          Country foundCountry = CountryList[this.textBox1.Text]

          in above code there may be chances of KeyNotFound Exception[^] Anyway 5+

          Thanks -Amit Gajjar (MinterProject)

          E Offline
          E Offline
          Ed Hill _5_
          wrote on last edited by
          #4

          thanks, i should have been a little more clear, the CountryList.ContainsKey check should be called first before attempting access via CountryList[this.textBox1.Text]. Thanks for the 5

          B 1 Reply Last reply
          0
          • E Ed Hill _5_

            thanks, i should have been a little more clear, the CountryList.ContainsKey check should be called first before attempting access via CountryList[this.textBox1.Text]. Thanks for the 5

            B Offline
            B Offline
            BobJanova
            wrote on last edited by
            #5

            You can use TryGetValue (in recent .Net, but if you're already suggesting Linq then it is also available) which will do the contains check and the lookup in a single call.

            Country c;
            if(dict.TryGetValue(text, ref c)){
            // do stuff here
            } // otherwise, it wasn't in the dict

            1 Reply Last reply
            0
            • N Nathaniel Sumaya

              public Country(string cAbs, string cName)
              {
              this._country = cName;
              this._abs = cAbs;
              }

              private void btnOk_Click(object sender, EventArgs e)
              {
              Country myCountry = new Country();

                      CountryList countries = new CountryList();
              
                      countries.Add(new Country("AE", "United Arab Emirates"));
                      countries.Add(new Country("AF", "Afghanistan"));
                      countries.Add(new Country("AQ", "Antarctica"));
                      countries.Add(new Country("AR", "Argentina"));
                      countries.Add(new Country("AT", "Austria"));
                      countries.Add(new Country("AU", "Australia"));
                      countries.Add(new Country("BD", "Bangladesh"));
                      countries.Add(new Country("BE", "Belgium"));
                      countries.Add(new Country("BR", "Brazil"));
                      countries.Add(new Country("BS", "Bahamas"));
                      countries.Add(new Country("CA", "Canada"));
                      countries.Add(new Country("CH", "Switzerland"));
                      countries.Add(new Country("CL", "Chilie"));
                      countries.Add(new Country("CM", "Cameroon"));
                      countries.Add(new Country("CL", "Chilie"));
                      countries.Add(new Country("CN", "China"));
                      countries.Add(new Country("CO", "Columbia"));
                      countries.Add(new Country("CR", "Costa Rica"));
                      countries.Add(new Country("DK", "Denmark"));
                      countries.Add(new Country("DM", "Dominica"));
                      countries.Add(new Country("DO", "Domican Republic"));
                      countries.Add(new Country("EC", "Ecuador"));
                      countries.Add(new Country("EE", "Estonia"));
                      countries.Add(new Country("EG", "Egypt"));
                      countries.Add(new Country("ES", "Spain"));
                      countries.Add(new Country("ET", "Ethiopia"));
                      countries.Add(new Country("EU", "European Union"));
              
                      //CountryList country = countries.Where(f=> f.CountryAbreviation == this.textBox1.Text)  
              
                      //Country country = countries.Where(f => f.CountryAbreviation == this.textBox1.Text).OrderByDescending(f => f.CountryAbreviation).FirstOrDefault();
                                 
                  }
              

              Question: 1. How to find the a country in a list?

              R Offline
              R Offline
              Ramesh Muthiah
              wrote on last edited by
              #6

              IEnumerable filteredCountry = countries.Where(f => f.GetAbbreviation == "EG"); if (filteredCountry != null) { Country countryOne = filteredCountry.First(); } try out the above code !!!! :)

              E L 2 Replies Last reply
              0
              • R Ramesh Muthiah

                IEnumerable filteredCountry = countries.Where(f => f.GetAbbreviation == "EG"); if (filteredCountry != null) { Country countryOne = filteredCountry.First(); } try out the above code !!!! :)

                E Offline
                E Offline
                Ed Hill _5_
                wrote on last edited by
                #7

                if (filteredCountry != null)

                this will not ever be null as it is assigned on the line above, but it may contain no results if there are no matches, causing First to return an InvalidOperationException. FirstOrDefault then a null check would be safer

                1 Reply Last reply
                0
                • R Ramesh Muthiah

                  IEnumerable filteredCountry = countries.Where(f => f.GetAbbreviation == "EG"); if (filteredCountry != null) { Country countryOne = filteredCountry.First(); } try out the above code !!!! :)

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #8

                  Ramesh Muthiah wrote:

                  IEnumerable filteredCountry = countries.Where(f => f.GetAbbreviation == "EG");
                   
                  if (filteredCountry != null)
                  {
                  Country countryOne = filteredCountry.First();

                  }

                  Its actually simplier.

                  var result = countries.SingleOrDefault(c => c.GetAbbreviation() == "EG");

                  //Check for null on result

                  Computers have been intelligent for a long time now. It just so happens that the program writers are about as effective as a room full of monkeys trying to crank out a copy of Hamlet.

                  1 Reply Last reply
                  0
                  • N Nathaniel Sumaya

                    public Country(string cAbs, string cName)
                    {
                    this._country = cName;
                    this._abs = cAbs;
                    }

                    private void btnOk_Click(object sender, EventArgs e)
                    {
                    Country myCountry = new Country();

                            CountryList countries = new CountryList();
                    
                            countries.Add(new Country("AE", "United Arab Emirates"));
                            countries.Add(new Country("AF", "Afghanistan"));
                            countries.Add(new Country("AQ", "Antarctica"));
                            countries.Add(new Country("AR", "Argentina"));
                            countries.Add(new Country("AT", "Austria"));
                            countries.Add(new Country("AU", "Australia"));
                            countries.Add(new Country("BD", "Bangladesh"));
                            countries.Add(new Country("BE", "Belgium"));
                            countries.Add(new Country("BR", "Brazil"));
                            countries.Add(new Country("BS", "Bahamas"));
                            countries.Add(new Country("CA", "Canada"));
                            countries.Add(new Country("CH", "Switzerland"));
                            countries.Add(new Country("CL", "Chilie"));
                            countries.Add(new Country("CM", "Cameroon"));
                            countries.Add(new Country("CL", "Chilie"));
                            countries.Add(new Country("CN", "China"));
                            countries.Add(new Country("CO", "Columbia"));
                            countries.Add(new Country("CR", "Costa Rica"));
                            countries.Add(new Country("DK", "Denmark"));
                            countries.Add(new Country("DM", "Dominica"));
                            countries.Add(new Country("DO", "Domican Republic"));
                            countries.Add(new Country("EC", "Ecuador"));
                            countries.Add(new Country("EE", "Estonia"));
                            countries.Add(new Country("EG", "Egypt"));
                            countries.Add(new Country("ES", "Spain"));
                            countries.Add(new Country("ET", "Ethiopia"));
                            countries.Add(new Country("EU", "European Union"));
                    
                            //CountryList country = countries.Where(f=> f.CountryAbreviation == this.textBox1.Text)  
                    
                            //Country country = countries.Where(f => f.CountryAbreviation == this.textBox1.Text).OrderByDescending(f => f.CountryAbreviation).FirstOrDefault();
                                       
                        }
                    

                    Question: 1. How to find the a country in a list?

                    L Offline
                    L Offline
                    Lost User
                    wrote on last edited by
                    #9

                    Using LINQ it is quite simple.

                    var result = countries.SingleOrDefault(c => c.GetAbbreviation() == "EG");

                    //Check for null on result

                    Computers have been intelligent for a long time now. It just so happens that the program writers are about as effective as a room full of monkeys trying to crank out a copy of Hamlet.

                    1 Reply Last reply
                    0
                    • N Nathaniel Sumaya

                      public Country(string cAbs, string cName)
                      {
                      this._country = cName;
                      this._abs = cAbs;
                      }

                      private void btnOk_Click(object sender, EventArgs e)
                      {
                      Country myCountry = new Country();

                              CountryList countries = new CountryList();
                      
                              countries.Add(new Country("AE", "United Arab Emirates"));
                              countries.Add(new Country("AF", "Afghanistan"));
                              countries.Add(new Country("AQ", "Antarctica"));
                              countries.Add(new Country("AR", "Argentina"));
                              countries.Add(new Country("AT", "Austria"));
                              countries.Add(new Country("AU", "Australia"));
                              countries.Add(new Country("BD", "Bangladesh"));
                              countries.Add(new Country("BE", "Belgium"));
                              countries.Add(new Country("BR", "Brazil"));
                              countries.Add(new Country("BS", "Bahamas"));
                              countries.Add(new Country("CA", "Canada"));
                              countries.Add(new Country("CH", "Switzerland"));
                              countries.Add(new Country("CL", "Chilie"));
                              countries.Add(new Country("CM", "Cameroon"));
                              countries.Add(new Country("CL", "Chilie"));
                              countries.Add(new Country("CN", "China"));
                              countries.Add(new Country("CO", "Columbia"));
                              countries.Add(new Country("CR", "Costa Rica"));
                              countries.Add(new Country("DK", "Denmark"));
                              countries.Add(new Country("DM", "Dominica"));
                              countries.Add(new Country("DO", "Domican Republic"));
                              countries.Add(new Country("EC", "Ecuador"));
                              countries.Add(new Country("EE", "Estonia"));
                              countries.Add(new Country("EG", "Egypt"));
                              countries.Add(new Country("ES", "Spain"));
                              countries.Add(new Country("ET", "Ethiopia"));
                              countries.Add(new Country("EU", "European Union"));
                      
                              //CountryList country = countries.Where(f=> f.CountryAbreviation == this.textBox1.Text)  
                      
                              //Country country = countries.Where(f => f.CountryAbreviation == this.textBox1.Text).OrderByDescending(f => f.CountryAbreviation).FirstOrDefault();
                                         
                          }
                      

                      Question: 1. How to find the a country in a list?

                      C Offline
                      C Offline
                      Christian Amado
                      wrote on last edited by
                      #10

                      countries.FirstOrDefault(q=>q.cName == country_search);

                      Regards

                      Christian Amado MCITP | MCTS | MOS | MTA Olimpia ☆ ★★★ Please mark as answer, if it helps.

                      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