0

I have written an application in ASP.NET MVC and have implemented a search bar. This search bar is enabled for multiple fields. The problem I am having is enabling the search for an ID field of type int. It seems like I need to only have string values in my search field. Is there a way to convert the ID field to string type so I can also be able to search for that?

Here is the code i'm using. It works for first name and last name, but not when I have the ID part enabled.

Any help is appreciated!

 public ActionResult Index(string sortOrder, string currentFilter, string searchString, int? page)
    {
        ViewBag.LastSortParm = sortOrder == "LastName" ? "LastDesc" : "LastName";

        ViewBag.FirstSortParm = sortOrder == "FirstName" ? "FirstDesc" : "FirstName";

        ViewBag.IDSortParm = sortOrder == "ID" ? "RVHDesc" : "ID";

  if (!String.IsNullOrEmpty(searchString)) //creates the code for the search bar
        {
            doctors = doctors.Where(d => d.Last_Name.ToUpper().Contains(searchString.ToUpper()) //insert to upper or lower so that case is ignored
                                   || d.First_Name.ToUpper().Contains(searchString.ToUpper()));
             || d.RVH_ID_.ToUpper().Contains(searchString.ToUpper()));
        }
}
5
  • What error are you getting? If d.RVH_ID_ is an integer, then you will need to call ToString() before the ToUpper() or you will get an error about no such method exists for integer. Commented Apr 2, 2015 at 16:54
  • Also, you have an extra ; in your .Where() before the last line. Commented Apr 2, 2015 at 16:55
  • It says "int does not contain a definition for ToUpper". So I should write something like d.RVH_ID_.ToString(Contains(searchString.ToUpper())))); ? Commented Apr 2, 2015 at 16:59
  • Why are we calling ToUpper on a string containing an integer? Is lowercase 1 different from uppercase 1? Am I getting that out of touch? Commented Apr 2, 2015 at 17:05
  • @wgraham should I just cut out the ToUpper parts? Commented Apr 2, 2015 at 17:08

1 Answer 1

1

If your d.RVH_ID_ field is an int then you will need to call ToString() on it before your ToUpper() call. Try this:

doctors = doctors.Where(d => d.Last_Name.ToUpper().Contains(searchString.ToUpper())
         || d.First_Name.ToUpper().Contains(searchString.ToUpper())
         || d.RVH_ID_.ToString().ToUpper().Contains(searchString.ToUpper()));

As @wgraham mentions in his comment above, since an int has no uppercase/lowercase, the ToUpper() call is not necessary. However, there is no ToString() overload for int in Linq, so you have to cast it to double or decimal according to this answer.

Example: SqlFunctions.StringConvert((double)d.RVH_ID_).Contains(searchString)

Sign up to request clarification or add additional context in comments.

5 Comments

Ah this makes sense. I added in the ToString, but it is putting out the error : LINQ to Entities does not recognize the method 'System.String ToString()' method, and this method cannot be translated into a store expression.
Perhaps you can use type casting, like so: ((string)d.RVH_ID_).ToUpper().Contains(searchString.ToUpper()));
It says "cannot convert type int to string". Is it not possible to do?
Please see my edit. It looks like you will have to use SqlFunctions.StringConvert to be able to get a string out of an int
thank you so much! I added this code and then added using System.Data.Objects.SqlClient;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.