0

I have search functionality on my page, where I use multiple strings to filter search results. In this case, best way is using IQueryable, but I have to send my search result in view as List.

So problem comes when I try to convert my result .ToList(). I get System.NotSupportedException here, saing "LINQ to Entities does not recognize the method 'System.String ToString()' method, and this method cannot be translated into a store expression."

What can I do in this situation?

    public ActionResult Index(string name, string lastName, string bloodGrade)
    {
        if (Request.IsAjaxRequest())
        {
            IQueryable<User> result = db.Users;

            if (name != null)
            {
                result = result.Where(s => s.Name.Contains(name));
            }
            if (lastName != null)
            {
                result = result.Where(s => s.Lastname.Contains(lastName));
            }
            if (bloodGrade != null)
            {
                result = result.Where(s => s.BloodGrade.ToString() == bloodGrade);
            }

            return PartialView("_Wall", result.ToList());
        }
        return View(db.Users.ToList());
    }
michal.jakubeczy
  • 8,221
  • 1
  • 59
  • 63
Misho
  • 43
  • 4
  • 2
    What is the underlying type of bloodgrade in the database? If I'm not mistaken the problem is that the ToString method cannot be translated into a valid database query. – Gareth van der Berg Mar 20 '16 at 18:49
  • But all my search variables are string format and why I have to convert them ToString() ? – Misho Mar 20 '16 at 18:51
  • What is typeof `BloodGrade`? If its (say) `int`, then you parameter should be `int blodGrade` –  Mar 20 '16 at 21:21

0 Answers0