Finding value in a list
-
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?
-
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?
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
-
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
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)
-
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)
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
-
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
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 -
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?
IEnumerable filteredCountry = countries.Where(f => f.GetAbbreviation == "EG"); if (filteredCountry != null) { Country countryOne = filteredCountry.First(); } try out the above code !!!! :)
-
IEnumerable filteredCountry = countries.Where(f => f.GetAbbreviation == "EG"); if (filteredCountry != null) { Country countryOne = filteredCountry.First(); } try out the above code !!!! :)
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
-
IEnumerable filteredCountry = countries.Where(f => f.GetAbbreviation == "EG"); if (filteredCountry != null) { Country countryOne = filteredCountry.First(); } try out the above code !!!! :)
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.
-
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?
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.
-
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?
countries.FirstOrDefault(q=>q.cName == country_search);
Regards
Christian Amado MCITP | MCTS | MOS | MTA Olimpia ☆ ★★★ Please mark as answer, if it helps.