Note that there are some explanatory texts on larger screens.

plurals
  1. POSearching Country by Country code
    text
    copied!<p>I am working on a search method, which will be called with Ajax, and updates a Webgrid in Mvc4. The search will go through a list of Project objects, which contains some fields.</p> <p>One of the fields is Country. And right now my code only checks if the input string contains the search string:</p> <pre><code>private bool StringStartWith(string input, string searchstring) { bool startwith = false; var inputlist = new List&lt;string&gt;(input.ToLower().Split(' ').Distinct()); var searchList = new List&lt;string&gt;(searchstring.ToLower().Split(' ')); var count = (from inp in inputlist from sear in searchList where inp.StartsWith(sear) select inp).Count(); if (count == searchList.Count) startwith = true; return startwith; } </code></pre> <p>But I also want to be able to search by country code. So if I write "DK", it should tell that it is equal to Denmark.</p> <p>I hope I can get some help for it. Thanks.</p> <p>//UPDATE!!</p> <p>iTURTEV answer helped me to make my method work as it should. I just had to update my method as shown here:</p> <pre><code> private bool InputStartWithSearch(string input, string searchstring) { if(searchstring[searchstring.Length-1].Equals(' ')) searchstring = searchstring.Substring(0,searchstring.Length-2); bool startwith = false; var inputlist = new List&lt;string&gt;(input.ToLower().Split(' ').Distinct()); var searchList = new List&lt;string&gt;(searchstring.ToLower().Split(' ')); if (searchstring.Length == 2) { var countryCode = new RegionInfo(searchstring.ToUpper()).EnglishName; if (inputlist.Any(country =&gt; country.ToLower().Equals(countryCode.ToLower()))) { return true; } } var count = (from inp in inputlist from sear in searchList where inp.StartsWith(sear) select inp).Count(); if (count == searchList.Count) startwith = true; return startwith; } </code></pre> <p>Thanks a lot.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload