2

Is it possible to filter view records via a search for a int? It words but a string but when I try any adapt it to draw all records that match an int I get error: DbComparisonExpression requires arguments with comparable types. in reference to the return view.

Controller:

 public ActionResult Index(string sortOrder,string search)
      {
        ViewBag.TimeSortParam = String.IsNullOrEmpty(sortOrder) ? "Time_desc" : "";
        ViewBag.PrioritySortParam = sortOrder == "Priority" ? "Priority_desc" : "";
        var incidents = from s in db.Incidents.Include(i => i.Defect).Include(i =>   i.Operator)
                        select s;

        if (!String.IsNullOrEmpty(search))
        {
            incidents = incidents.Where(s => s.IncidentID.Equals(search));
        }
 return View(incidents.ToList());

View

Find by id: @Html.TextBox("Search")

I can see the problem is I am using a string for but I cant find a solution for int

2
  • What type is Incident.IncidentID? Commented Feb 28, 2014 at 22:30
  • it is an INT. I've tried afew thing like solutions like Index(string sortOrder,int search) prehaps something like if (search == null) Commented Feb 28, 2014 at 22:46

2 Answers 2

1

If anyone is interested this worked, although I am not sure its the most efficient way to filter by an int:

public ActionResult Index(string sortOrder, int? search)
    {
        ViewBag.TimeSortParam = String.IsNullOrEmpty(sortOrder) ? "Time_desc" : "";
        ViewBag.PrioritySortParam = sortOrder == "Priority" ? "Priority_desc" : "";
        var incidents = from s in db.Incidents.Include(i => i.Defect).Include(i => i.Operator)
                        select s;
        if (search != null)
        {
            incidents = incidents.Where(s => s.IncidentID == (search));
        }}
 return View(incidents.ToList());
Sign up to request clarification or add additional context in comments.

1 Comment

Sorta works, I get this error: Unable to create a constant value of type 'System.Object'. Only primitive types or enumeration types are supported in this context. on this line: return View(await requests.ToListAsync());
0

In your statement, change to:

s.IncidentID==Convert.ToInt32(search)

1 Comment

got "LINQ to Entities does not recognize the method 'Int32 ToInt32(System.String)' method, and this method cannot be translated into a store expression." @ return View(incidents.ToList());

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.